Custom product price suffix on based on product categories in Woocommerce











up vote
1
down vote

favorite












I need to add 'per metre' to the price on most of my online catalogue, I tried the code on this thread in my finctions.php but I cannot get it to omit/include particular categories- it seems to be all or nothing. What am I doing wrong?



I have edited the code as such:



/*add 'per metre' after selected items*/
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
// HERE define your product categories (can be IDs, slugs or names)
$product_categories = array('fabric','haberdashery', 'lining',);

if( ! has_term( $product_categories, 'fasteners', 'patches', 'remnnants', $product->get_id() ) )
$price .= ' ' . __('per metre');

return $price;
}


I want 'fabrics', 'haberdashery', 'lining' to show per metre, and 'fasteners', 'patches', 'remnants' to NOT show the suffix.



I have tried variations of the code -my exclusions in the top bit and the inclusions in the second part, and with/without the "( ! has term" section, but whichever I do takes all the suffix messages away, or applies to all categories.



It would be amazing if I could get this to work as have previously been using a very bloated plug-in. I'm only basically capable in this stuff so please feel free to talk me through it as if I am an idiot.










share|improve this question
























  • There is a comment zone below my answer where you can add a comment related to my answer and I will be notified (instead of making a comment in an answer). My code is tested and works perfectly. Note that has_term() function doesn't work on parent product categories or child subcategories. You need to specify the product categories set in your related products. Now 20 is the hook priority and 2 the number of arguments (variables) in the function, and there is nothing wrong with it.
    – LoicTheAztec
    Nov 23 at 22:23












  • This doesn't work for me and I suspect it is to do with parent/child categories, if it doesn't work on them what does it work on? My parent categories are 'Fabric', 'Haberdashery', all other categories I have are beneath them - eg 'jersey' (sold per metre) and 'buttons (sold individually).
    – bessworks
    Nov 24 at 8:19












  • If the has_term ( ) function doesn't work on my categories is there another function that does?
    – bessworks
    Nov 24 at 8:30










  • First as a new user you should[ take the quick tour (30 seconds)](stackoverflow.com/tour) to better understand how StackOverFlow works basically. Now as I have told you, you should add those comments below my answer in the comment zone. I have updated the answer and it should work now with your parent categories. If this answer is answering your question, please don't forget to accept the answer, Thank you.
    – LoicTheAztec
    Nov 24 at 10:45















up vote
1
down vote

favorite












I need to add 'per metre' to the price on most of my online catalogue, I tried the code on this thread in my finctions.php but I cannot get it to omit/include particular categories- it seems to be all or nothing. What am I doing wrong?



I have edited the code as such:



/*add 'per metre' after selected items*/
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
// HERE define your product categories (can be IDs, slugs or names)
$product_categories = array('fabric','haberdashery', 'lining',);

if( ! has_term( $product_categories, 'fasteners', 'patches', 'remnnants', $product->get_id() ) )
$price .= ' ' . __('per metre');

return $price;
}


I want 'fabrics', 'haberdashery', 'lining' to show per metre, and 'fasteners', 'patches', 'remnants' to NOT show the suffix.



I have tried variations of the code -my exclusions in the top bit and the inclusions in the second part, and with/without the "( ! has term" section, but whichever I do takes all the suffix messages away, or applies to all categories.



It would be amazing if I could get this to work as have previously been using a very bloated plug-in. I'm only basically capable in this stuff so please feel free to talk me through it as if I am an idiot.










share|improve this question
























  • There is a comment zone below my answer where you can add a comment related to my answer and I will be notified (instead of making a comment in an answer). My code is tested and works perfectly. Note that has_term() function doesn't work on parent product categories or child subcategories. You need to specify the product categories set in your related products. Now 20 is the hook priority and 2 the number of arguments (variables) in the function, and there is nothing wrong with it.
    – LoicTheAztec
    Nov 23 at 22:23












  • This doesn't work for me and I suspect it is to do with parent/child categories, if it doesn't work on them what does it work on? My parent categories are 'Fabric', 'Haberdashery', all other categories I have are beneath them - eg 'jersey' (sold per metre) and 'buttons (sold individually).
    – bessworks
    Nov 24 at 8:19












  • If the has_term ( ) function doesn't work on my categories is there another function that does?
    – bessworks
    Nov 24 at 8:30










  • First as a new user you should[ take the quick tour (30 seconds)](stackoverflow.com/tour) to better understand how StackOverFlow works basically. Now as I have told you, you should add those comments below my answer in the comment zone. I have updated the answer and it should work now with your parent categories. If this answer is answering your question, please don't forget to accept the answer, Thank you.
    – LoicTheAztec
    Nov 24 at 10:45













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I need to add 'per metre' to the price on most of my online catalogue, I tried the code on this thread in my finctions.php but I cannot get it to omit/include particular categories- it seems to be all or nothing. What am I doing wrong?



I have edited the code as such:



/*add 'per metre' after selected items*/
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
// HERE define your product categories (can be IDs, slugs or names)
$product_categories = array('fabric','haberdashery', 'lining',);

if( ! has_term( $product_categories, 'fasteners', 'patches', 'remnnants', $product->get_id() ) )
$price .= ' ' . __('per metre');

return $price;
}


I want 'fabrics', 'haberdashery', 'lining' to show per metre, and 'fasteners', 'patches', 'remnants' to NOT show the suffix.



I have tried variations of the code -my exclusions in the top bit and the inclusions in the second part, and with/without the "( ! has term" section, but whichever I do takes all the suffix messages away, or applies to all categories.



It would be amazing if I could get this to work as have previously been using a very bloated plug-in. I'm only basically capable in this stuff so please feel free to talk me through it as if I am an idiot.










share|improve this question















I need to add 'per metre' to the price on most of my online catalogue, I tried the code on this thread in my finctions.php but I cannot get it to omit/include particular categories- it seems to be all or nothing. What am I doing wrong?



I have edited the code as such:



/*add 'per metre' after selected items*/
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
// HERE define your product categories (can be IDs, slugs or names)
$product_categories = array('fabric','haberdashery', 'lining',);

if( ! has_term( $product_categories, 'fasteners', 'patches', 'remnnants', $product->get_id() ) )
$price .= ' ' . __('per metre');

return $price;
}


I want 'fabrics', 'haberdashery', 'lining' to show per metre, and 'fasteners', 'patches', 'remnants' to NOT show the suffix.



I have tried variations of the code -my exclusions in the top bit and the inclusions in the second part, and with/without the "( ! has term" section, but whichever I do takes all the suffix messages away, or applies to all categories.



It would be amazing if I could get this to work as have previously been using a very bloated plug-in. I'm only basically capable in this stuff so please feel free to talk me through it as if I am an idiot.







php wordpress woocommerce custom-taxonomy price






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 22:12









LoicTheAztec

82.9k125993




82.9k125993










asked Nov 22 at 16:44









bessworks

62




62












  • There is a comment zone below my answer where you can add a comment related to my answer and I will be notified (instead of making a comment in an answer). My code is tested and works perfectly. Note that has_term() function doesn't work on parent product categories or child subcategories. You need to specify the product categories set in your related products. Now 20 is the hook priority and 2 the number of arguments (variables) in the function, and there is nothing wrong with it.
    – LoicTheAztec
    Nov 23 at 22:23












  • This doesn't work for me and I suspect it is to do with parent/child categories, if it doesn't work on them what does it work on? My parent categories are 'Fabric', 'Haberdashery', all other categories I have are beneath them - eg 'jersey' (sold per metre) and 'buttons (sold individually).
    – bessworks
    Nov 24 at 8:19












  • If the has_term ( ) function doesn't work on my categories is there another function that does?
    – bessworks
    Nov 24 at 8:30










  • First as a new user you should[ take the quick tour (30 seconds)](stackoverflow.com/tour) to better understand how StackOverFlow works basically. Now as I have told you, you should add those comments below my answer in the comment zone. I have updated the answer and it should work now with your parent categories. If this answer is answering your question, please don't forget to accept the answer, Thank you.
    – LoicTheAztec
    Nov 24 at 10:45


















  • There is a comment zone below my answer where you can add a comment related to my answer and I will be notified (instead of making a comment in an answer). My code is tested and works perfectly. Note that has_term() function doesn't work on parent product categories or child subcategories. You need to specify the product categories set in your related products. Now 20 is the hook priority and 2 the number of arguments (variables) in the function, and there is nothing wrong with it.
    – LoicTheAztec
    Nov 23 at 22:23












  • This doesn't work for me and I suspect it is to do with parent/child categories, if it doesn't work on them what does it work on? My parent categories are 'Fabric', 'Haberdashery', all other categories I have are beneath them - eg 'jersey' (sold per metre) and 'buttons (sold individually).
    – bessworks
    Nov 24 at 8:19












  • If the has_term ( ) function doesn't work on my categories is there another function that does?
    – bessworks
    Nov 24 at 8:30










  • First as a new user you should[ take the quick tour (30 seconds)](stackoverflow.com/tour) to better understand how StackOverFlow works basically. Now as I have told you, you should add those comments below my answer in the comment zone. I have updated the answer and it should work now with your parent categories. If this answer is answering your question, please don't forget to accept the answer, Thank you.
    – LoicTheAztec
    Nov 24 at 10:45
















There is a comment zone below my answer where you can add a comment related to my answer and I will be notified (instead of making a comment in an answer). My code is tested and works perfectly. Note that has_term() function doesn't work on parent product categories or child subcategories. You need to specify the product categories set in your related products. Now 20 is the hook priority and 2 the number of arguments (variables) in the function, and there is nothing wrong with it.
– LoicTheAztec
Nov 23 at 22:23






There is a comment zone below my answer where you can add a comment related to my answer and I will be notified (instead of making a comment in an answer). My code is tested and works perfectly. Note that has_term() function doesn't work on parent product categories or child subcategories. You need to specify the product categories set in your related products. Now 20 is the hook priority and 2 the number of arguments (variables) in the function, and there is nothing wrong with it.
– LoicTheAztec
Nov 23 at 22:23














This doesn't work for me and I suspect it is to do with parent/child categories, if it doesn't work on them what does it work on? My parent categories are 'Fabric', 'Haberdashery', all other categories I have are beneath them - eg 'jersey' (sold per metre) and 'buttons (sold individually).
– bessworks
Nov 24 at 8:19






This doesn't work for me and I suspect it is to do with parent/child categories, if it doesn't work on them what does it work on? My parent categories are 'Fabric', 'Haberdashery', all other categories I have are beneath them - eg 'jersey' (sold per metre) and 'buttons (sold individually).
– bessworks
Nov 24 at 8:19














If the has_term ( ) function doesn't work on my categories is there another function that does?
– bessworks
Nov 24 at 8:30




If the has_term ( ) function doesn't work on my categories is there another function that does?
– bessworks
Nov 24 at 8:30












First as a new user you should[ take the quick tour (30 seconds)](stackoverflow.com/tour) to better understand how StackOverFlow works basically. Now as I have told you, you should add those comments below my answer in the comment zone. I have updated the answer and it should work now with your parent categories. If this answer is answering your question, please don't forget to accept the answer, Thank you.
– LoicTheAztec
Nov 24 at 10:45




First as a new user you should[ take the quick tour (30 seconds)](stackoverflow.com/tour) to better understand how StackOverFlow works basically. Now as I have told you, you should add those comments below my answer in the comment zone. I have updated the answer and it should work now with your parent categories. If this answer is answering your question, please don't forget to accept the answer, Thank you.
– LoicTheAztec
Nov 24 at 10:45












1 Answer
1






active

oldest

votes

















up vote
0
down vote













There is a little mistake in your code in the has_term() function.



To handle parent product categories, we will use a custom conditional function instead of has_tem().



I have also added some code to handle the product variation selected price of variable products, So try this instead:



// Custom conditional function that checks for parent product categories
function has_product_categories( $categories, $product_id ) {
// Initializing
$parent_term_ids = $categories_ids = array();
$taxonomy = 'product_cat';

// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
if( is_numeric( $category ) ) {
$categories_ids = (int) $category;
} else {
$categories_ids = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
}
}

// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids = $term->parent; // Set the parent product category
$parent_term_ids = $term->term_id; // (and the child)
} else {
$parent_term_ids = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 10, 2 );
function conditional_price_suffix( $price, $product ) {
// Handling product variations
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

// HERE define your product categories (can be IDs, slugs or names)
$product_categories = array('fabric','haberdashery', 'lining');

if( has_product_categories( $product_categories, $product_id ) )
$price .= ' ' . __('per metre');

return $price;
}


Code goes in function.php file of your active child theme (or active theme). tested and works.



enter image description here






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435276%2fcustom-product-price-suffix-on-based-on-product-categories-in-woocommerce%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    There is a little mistake in your code in the has_term() function.



    To handle parent product categories, we will use a custom conditional function instead of has_tem().



    I have also added some code to handle the product variation selected price of variable products, So try this instead:



    // Custom conditional function that checks for parent product categories
    function has_product_categories( $categories, $product_id ) {
    // Initializing
    $parent_term_ids = $categories_ids = array();
    $taxonomy = 'product_cat';

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
    if( is_numeric( $category ) ) {
    $categories_ids = (int) $category;
    } else {
    $categories_ids = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
    }
    }

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
    if( $term->parent > 0 ){
    $parent_term_ids = $term->parent; // Set the parent product category
    $parent_term_ids = $term->term_id; // (and the child)
    } else {
    $parent_term_ids = $term->term_id; // It is the Main category term and we set it.
    }
    }
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
    }

    add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 10, 2 );
    function conditional_price_suffix( $price, $product ) {
    // Handling product variations
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    // HERE define your product categories (can be IDs, slugs or names)
    $product_categories = array('fabric','haberdashery', 'lining');

    if( has_product_categories( $product_categories, $product_id ) )
    $price .= ' ' . __('per metre');

    return $price;
    }


    Code goes in function.php file of your active child theme (or active theme). tested and works.



    enter image description here






    share|improve this answer



























      up vote
      0
      down vote













      There is a little mistake in your code in the has_term() function.



      To handle parent product categories, we will use a custom conditional function instead of has_tem().



      I have also added some code to handle the product variation selected price of variable products, So try this instead:



      // Custom conditional function that checks for parent product categories
      function has_product_categories( $categories, $product_id ) {
      // Initializing
      $parent_term_ids = $categories_ids = array();
      $taxonomy = 'product_cat';

      // Convert categories term names and slugs to categories term ids
      foreach ( $categories as $category ){
      if( is_numeric( $category ) ) {
      $categories_ids = (int) $category;
      } else {
      $categories_ids = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
      }
      }

      // Loop through the current product category terms to get only parent main category term
      foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
      if( $term->parent > 0 ){
      $parent_term_ids = $term->parent; // Set the parent product category
      $parent_term_ids = $term->term_id; // (and the child)
      } else {
      $parent_term_ids = $term->term_id; // It is the Main category term and we set it.
      }
      }
      return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
      }

      add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 10, 2 );
      function conditional_price_suffix( $price, $product ) {
      // Handling product variations
      $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

      // HERE define your product categories (can be IDs, slugs or names)
      $product_categories = array('fabric','haberdashery', 'lining');

      if( has_product_categories( $product_categories, $product_id ) )
      $price .= ' ' . __('per metre');

      return $price;
      }


      Code goes in function.php file of your active child theme (or active theme). tested and works.



      enter image description here






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        There is a little mistake in your code in the has_term() function.



        To handle parent product categories, we will use a custom conditional function instead of has_tem().



        I have also added some code to handle the product variation selected price of variable products, So try this instead:



        // Custom conditional function that checks for parent product categories
        function has_product_categories( $categories, $product_id ) {
        // Initializing
        $parent_term_ids = $categories_ids = array();
        $taxonomy = 'product_cat';

        // Convert categories term names and slugs to categories term ids
        foreach ( $categories as $category ){
        if( is_numeric( $category ) ) {
        $categories_ids = (int) $category;
        } else {
        $categories_ids = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
        }
        }

        // Loop through the current product category terms to get only parent main category term
        foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
        $parent_term_ids = $term->parent; // Set the parent product category
        $parent_term_ids = $term->term_id; // (and the child)
        } else {
        $parent_term_ids = $term->term_id; // It is the Main category term and we set it.
        }
        }
        return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
        }

        add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 10, 2 );
        function conditional_price_suffix( $price, $product ) {
        // Handling product variations
        $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

        // HERE define your product categories (can be IDs, slugs or names)
        $product_categories = array('fabric','haberdashery', 'lining');

        if( has_product_categories( $product_categories, $product_id ) )
        $price .= ' ' . __('per metre');

        return $price;
        }


        Code goes in function.php file of your active child theme (or active theme). tested and works.



        enter image description here






        share|improve this answer














        There is a little mistake in your code in the has_term() function.



        To handle parent product categories, we will use a custom conditional function instead of has_tem().



        I have also added some code to handle the product variation selected price of variable products, So try this instead:



        // Custom conditional function that checks for parent product categories
        function has_product_categories( $categories, $product_id ) {
        // Initializing
        $parent_term_ids = $categories_ids = array();
        $taxonomy = 'product_cat';

        // Convert categories term names and slugs to categories term ids
        foreach ( $categories as $category ){
        if( is_numeric( $category ) ) {
        $categories_ids = (int) $category;
        } else {
        $categories_ids = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
        }
        }

        // Loop through the current product category terms to get only parent main category term
        foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
        $parent_term_ids = $term->parent; // Set the parent product category
        $parent_term_ids = $term->term_id; // (and the child)
        } else {
        $parent_term_ids = $term->term_id; // It is the Main category term and we set it.
        }
        }
        return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
        }

        add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 10, 2 );
        function conditional_price_suffix( $price, $product ) {
        // Handling product variations
        $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

        // HERE define your product categories (can be IDs, slugs or names)
        $product_categories = array('fabric','haberdashery', 'lining');

        if( has_product_categories( $product_categories, $product_id ) )
        $price .= ' ' . __('per metre');

        return $price;
        }


        Code goes in function.php file of your active child theme (or active theme). tested and works.



        enter image description here







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 at 10:41

























        answered Nov 22 at 22:09









        LoicTheAztec

        82.9k125993




        82.9k125993






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435276%2fcustom-product-price-suffix-on-based-on-product-categories-in-woocommerce%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo