Change text based on product category for Woocommerce

I’m trying to adjust backorder message for users who are purchasing products from my matresses product category.

  • Adam Czajczyk
    • Support Gorilla

    Hello Bryan Clark

    I hope you’re well today and thank you for your question!

    I checked your site and I can see this code currently in the functions.php file of an active child theme:

    //Backorder text editing for WooCommerce <https://woosnip.com/snips/change-backorder-text/>
    function change_backorder_message( $text, $product ){
    if ( $product->managing_stock() && $product->is_on_backorder( 1 ) && $product->get_shipping_class('malouf-accessories')) {
    $text = __( 'This Malouf Accessory ships FREE with 2 day shipping!', 'your-textdomain' );
    }
    return $text;
    }
    add_filter( 'woocommerce_get_availability_text', 'change_backorder_message', 10, 2 );

    The site seems to be running without any errors but the code is a bit different to what was discussed during the chat. It’s similar but doesn’t take category into account so I’m slightly confused: does this code work for you and you managed to solve the issue meanwhile or it still needs to be changed to the “category based” as mentioned during the chat?

    Let me know, please.

    Kind regards,

    Adam

  • Bryan Clark
    • New Recruit

    That’s a good question. You see that code because I am trying to use the same type of function twice. We sell some products (Malouf Sleep Accessories) that are online only and only available for ship. I’m using the shipping class definition to pull the custom text for backorders on those products only. Because it is not a true backorder but we don’t have them in stock at our store – they are fulfilled by our manufacturer. This function is working beautifully and doesn’t need to change.

    We also sell mattresses which is the point of the function in question. Because we sell more than just mattresses under the “delivery” shipping class, it is not an appropriate backorder text change. I want to display a message to customers who select a mattress from our website that is not “in stock” that it is a 7-10 day turn around because those mattresses are manufactured as soon as they put the order in. We carry mattresses both in available stock and only on order with the manufacturer. This is why I wanted to display this message to only those products in the “mattress” category for those products set to “backorder”. There is also the option on one of our mattresses in that catagory that has a turn around time of only 2-3 days because of a different manufacturer. I’m not sure the best way to proceed.

    Please let me know if I can be more clear or if I can answer any other questions.

    Thank you.

    Bryan

  • Adam Czajczyk
    • Support Gorilla

    Hi Bryan Clark

    Thanks for explanation!

    In this case, you would want to actually modify your existing function rather than add another “copy” of it. There can’t be two “instances” of the same function (function of the same name) used as this would cause the conflict and fatal error and there’s also no need to user redundant code while an “if…” condition can be just expanded a bit :slight_smile:

    This is a code that you’re already using but slightly modified:

    //Backorder text editing for WooCommerce <https://woosnip.com/snips/change-backorder-text/>
    function change_backorder_message( $text, $product ){
    if ( $product->managing_stock() && $product->is_on_backorder( 1 ) && ( $product->get_shipping_class('malouf-accessories') || is_product_category( 'mattresses') ) ) {
    $text = __( 'This Malouf Accessory ships FREE with 2 day shipping!', 'your-textdomain' );
    }
    return $text;
    }
    add_filter( 'woocommerce_get_availability_text', 'change_backorder_message', 10, 2 );

    The change is in this line:

    if ( $product->managing_stock() && $product->is_on_backorder( 1 ) && ( $product->get_shipping_class('malouf-accessories') || is_product_category( 'mattresses') ) ) {

    Originally (your current code) it was checking if “product is stock managed AND is on backorder AND the shipping class is malouf-accessories”.

    With this modification it should be checking if “Product is stock managed AND is on backorded AND (either shipping class is malouf-accessories OR product category is mattresses)”.

    So it should display your custom message in three cases, only for products that are stock-managed and on backorder:

    – if shipping class is “malouf-accessories”, regardless of the product category

    – if product category is “mattresses”, regardless of the shipping class

    – if shipping class is “malouf-accessories” and product category is “mattresses”.

    Give it a try please and let me know if it works for you. To use it on site, simply replace the code you are currently using (the one that I quoted in my previous post) with the one I shared above.

    Best regards,

    Adam

  • Bryan Clark
    • New Recruit

    Hi there,

    I’m sorry for the delay. I did not see this response till right now. The Malouf Accessory back order message is different than the mattresses backorder message. For mattresses I’d like to say “This mattresses is made just for you. This mattresses is available to order and will arrive to our store within 7-10 business days.

    Can I have a condition that will show one of the two messages? The Malouf Accessory message should be shown from the shipping class whereas the mattress message should be shown specially by the category only when it’s on “backorder”.

    Thanks so much,

    Bryan Clark

  • Adam Czajczyk
    • Support Gorilla

    Hi Bryan Clark

    Thank you for response and additional information.

    In that case, we just need to change conditions a bit. This should do the job:

    function change_backorder_message( $text, $product ){
    if ( $product->managing_stock() && $product->is_on_backorder( 1 ) ) {
    if ( $product->get_shipping_class('malouf-accessories') ) {
    $text = __( 'SHIPPING CLASS MESSAGE', 'your-textdomain' );
    }
    if ( is_product_category( 'mattresses') ) {
    $text = __( 'CATEGORY MESSAGE', 'your-textdomain' );
    }
    }
    return $text;
    }
    add_filter( 'woocommerce_get_availability_text', 'change_backorder_message', 10, 2 );

    Just replace SHIPPING CLASS MESSAGE and CATEGORY MESSAGE with relevant messages.

    Let me know if it works for you, please.

    Best regards,

    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.