Support » Developing with WordPress » Try to create an new plugins but i have problem

  • Hello guys i have code below the code is running if I assign id=1, etc. and repeat my codes, i want to happen is it will automatically post the code if the category name in my table and categoryname in woocommerce are the same.,

    for example ihave


    id categoryname imageurl
    1 wedding
    2 birthday


    id name
    1 rings
    2 wedding

    what will happen is if then the imageurl will display to wedding category only.can you read my code if this is correct?ThankYou.

    add_action( 'woocommerce_before_add_to_cart_button', 'size_guide_data' );
    function size_guide_data() {
      global $wpdb;
          $table_name = $wpdb->prefix . "sizeguide";
     $datas = $wpdb->get_results("SELECT * from $table_name where id=1");
      foreach ($datas as $data) { 
    if ( has_term( $data->category, 'product_cat' ) && has_term( $data->prodtype, 'product_type' )) {
            <div class="adm-sizeguide" ">
              <table id="sgtable" style="border:none;">
                  <td style="border:none; width: 10px;"><a style="display: inline; " class="size-guide" href="<?php echo $data->imageurl; ?>" target="_blank"><?php echo $data->name; ?><img style="margin-left: 5px; display: inline-block;" id="icon" src="<?php echo WP_PLUGIN_URL; ?>/wp-knaven/images/sg-icon.png"  width="24" height="24"></a></td>
                <?php }
    • This topic was modified 9 months, 1 week ago by  kr0128.
    • This topic was modified 9 months, 1 week ago by  kr0128.
    • This topic was modified 9 months, 1 week ago by  kr0128.
    • This topic was modified 9 months, 1 week ago by  kr0128.
    • This topic was modified 9 months, 1 week ago by  kr0128.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    Without knowing the nature of the data returned by the query, I couldn’t say.

    Why only id=1? You’ll get the same answer every time. There’s no point in having a conditional, unless the values for id=1 are always changing. Are you really getting a array of objects returned? $data->category will be invalid if it’s not for an object, you might need $data[‘category’] if it’s another array.

    When you use has_term(), it checks if the current post has the terms you are passing from $data. This function must be used within “The Loop”. Be sure the WC action is within The Loop. Even if that works out, you are checking if two terms exist with the post, not if the names are equal. Unless the nature of $data is such that category and prodtype are always the same.

    Despite that, I could not say for sure if your code is correct or not as I cannot test it because of your custom data structure. Why don’t you test it yourself? If it does not throw errors and does what you want, then it’s correct 🙂

    Of course your test data must be such that it will expose logic failures.

    the id=1 is temporary. what i want is the array[category] & [prodtype] from my table and if its the same in woocommerce category name and product type it will automatically place my sizeguide code in that category page.

    for example

    category data in my table category data in woocommerce
    1 Rings 1 Birth Day
    2 Weddings 2 Party
    3 Party 3 Weddings

    if the category data in my table = category data in woocommerce.
    for example: i have Weddings in my table and in the woocommerce. then if they have the same data i will place my code to that Weddings category. Can you provide me or explain what is the right code for this Logic?ThankYou.

    Moderator bcworkz


    OK, so about the snippet you posted: you were not asking “what’s wrong with this”, you are saying “this is the best I can do, please help!”? Fair enough, sorry for my confusion. I’ll try to help with a reasonable explanation, but I’m no WC expert. I do have some experience.

    We should back up a little and evaluate the best approach. We need to know what category the current page is displaying. The category is certainly assigned to the current product, possibly along with several other categories. So the current product cannot always tell us what category the page is, only that it’s one of several possibilities. We don’t even know if the current page is a category list at all, based on the current product alone.

    Unless the template has been customized with a new query, we should be able to determine if the page is a category list (and for what category) from the current queried object. Since we would get the same answer for every product in the list, it’s not very efficient to do this evaluation for every product, especially when DB queries are involved.

    I don’t know offhand what WP or WC hook would fire only once at the beginning of the page, I’m fairly certain at least one is available. This one time determination is independent of your choice of the before_add_to_cart_button action for where the sizing guide should appear. That can still happen, though I wonder if a single instance of the guide on the page would suffice, instead of before every product’s add_to_cart button.

    Or is it your intention that the guide should appear based on product category alone, regardless of the category for the page or if it’s a category page at all? This is what you get when evaluating only the current product, but it doesn’t sound to me like that is what you really want. Once I have a clear understanding of what you really want, I can offer more precise recommendations.

    I’m also wondering about the choice of a table for this reference data. It’s not much data to justify their own table. Have you considered other storage options? The choice involves considering the volume of data and how often it changes, plus what UI (if any) is involved in making such changes. A table will work fine, but it may not be the ideal choice.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Try to create an new plugins but i have problem’ is closed to new replies.