• Resolved cbraun8675309

    (@cbraun8675309)


    I have this little line of code that I use to add the words “pair” next to my product quantity for a specific category. I am trying to understand how I could define multiple categories that this would affect, rather then just one. Ive tried several different ways of modifying it, but no luck. TIA

    add_action('woocommerce_after_quantity_input_field', 'wc_text_after_quantity');
    
    function wc_text_after_quantity() {
        if ( is_product() && has_term( 'your_category', 'product_cat' ) ) {
            echo 'pair';
        }
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support con

    (@conschneider)

    Engineer

    Hi there,

    I would try the following:

    add_action('woocommerce_after_quantity_input_field', 'wc_text_after_quantity');
    
    function wc_text_after_quantity() {
        if ( is_product() && has_term( 'your_category', 'product_cat' ) OR is_product() && has_term( 'your_category_2', 'product_cat' ) ) {
            echo 'pair';
        }
    }

    Ive tried several different ways of modifying it, but no luck.

    What have you tried so far?

    Kind regards,

    Plugin Support Stuart Duff – a11n

    (@stuartduff)

    Automattic Happiness Engineer

    Hey @cbraun8675309,

    To include multiple categories you can include those category names within an array().

    add_action('woocommerce_after_quantity_input_field', 'wc_text_after_quantity');
    
    function wc_text_after_quantity() {
        if ( is_product() && has_term( array( 'your_category_1', 'your_category_2' ), 'product_cat' ) ) {
            echo 'pair';
        }
    }

    I hope this helps.

    Thread Starter cbraun8675309

    (@cbraun8675309)

    Awesome thank you both very much.

    I had tried adding commas in between, and also OR in between, along with ;, and |. I tried the second option offered by Stuart first and that worked perfectly.

    Hi @cbraun8675309

    Glad to hear the suggested code worked! If you have any further questions, feel free to open a new topic.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘theme functions php code question’ is closed to new replies.