WooCommerce Conditional Logic – Tags, Examples & PHP

The conditional tags of WooCommerce and WordPress (also “WooCommerce and WordPress Conditional Logic”) can be used in your functions.php to display content based on certain conditions. For example, you could display different content for different categories within a single PHP function.

You can find the list of WooCommerce conditional tags here and WordPress conditional tags here. I don’t think it’s worth it to paste them here, so please use those two links as a reference – in fact in this blog post I would like to give you EXAMPLES, probably the best way for you to learn WooCommerce customization.

1. Are you working on the WooCommerce Single Product Page?

Great thing about single product pages in WooCommerce is that WordPress knows they are “posts”. So, you can use is_single. The list of hooks for the single product page can be found here.

PHP: do something on single product pages only


add_action( 'woocommerce_before_main_content', 'bbloomer_single_product_pages' );

function bbloomer_single_product_pages() {

if ( is_product() ) {
echo 'Something';
} else {
echo 'Something else';
}

}

PHP: do something if product ID = XYZ


add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_product_ID' );

function bbloomer_single_product_ID() {

if ( is_single( '17' ) ) {
echo 'Something';
} elseif ( is_single( '56' ) ) {
echo 'Something else';
}

}

PHP: do something if product belongs to a category


add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_category_slug' );

function bbloomer_single_category_slug() {

if ( has_term( 'chairs', 'product_cat' ) ) {
echo 'Something';
} elseif ( has_term( 'tables', 'product_cat' ) ) {
echo 'Something else';
}

}

PHP: do something if product belongs to a tag


add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_tag_slug' );

function bbloomer_single_tag_slug() {

if ( has_term( 'blue', 'product_tag' ) ) {
echo 'Something';
} elseif ( has_term( 'red', 'product_tag' ) ) {
echo 'Something else';
}

}

PHP: do something if product is on sale


add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_on_sale' );

function bbloomer_single_on_sale() {

if ( $product->is_on_sale() ) {
 // do something
}

}

PHP: do something if product is simple, variable, external…


add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_product_type' );

function bbloomer_single_product_type() {

if( $product->is_type( 'simple' ) ){
 // do something
} elseif( $product->is_type( 'variable' ) ){
 // do something
} elseif( $product->is_type( 'external' ) ){
 // do something
} elseif( $product->is_type( 'grouped' ) ){
 // do something
} 

}

PHP: do something if product is virtual


add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_virtual' );

function bbloomer_single_virtual() {

if( $product->is_virtual() ){
 // do something
} 

}

PHP: do something if product is downloadable


add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_downloadable' );

function bbloomer_single_downloadable() {

if( $product->is_downloadable() ){
 // do something
} 

}

PHP: do something on the related products only

Related products are generated by a “loop”. Sometimes you might want to use your PHP on the single product page only (and excluding the related ones) or viceversa.

The snippet below hides the price only on the single product page and only on the related products section.


add_filter( 'woocommerce_variable_price_html', 'bbloomer_remove_variation_price', 10, 2 );
 
function bbloomer_remove_variation_price( $price ) {

global $woocommerce_loop;

if ( is_product() && $woocommerce_loop['name'] == 'related' ) {
$price = '';
}

return $price;
}

2. Are you working on the WooCommerce Shop/Category Page?

You can find all the shop/archive WooCommerce hooks here. Let’s see how to use conditional logic on these “loop” pages:

PHP: do something on the Shop page only


add_action( 'woocommerce_before_main_content', 'bbloomer_loop_shop' );

function bbloomer_loop_shop() {

if ( is_shop() ) {
echo 'This will show on the Shop page';
} else {
echo 'This will show on all other Woo pages';
}

}

PHP: do something on each product on the loop pages


add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_loop_per_product' );
 
function bbloomer_loop_per_product() {
 
if ( has_term( 'chairs', 'product_cat' ) ) {
echo 'Great chairs!';
} elseif ( has_term( 'tables', 'product_cat' ) ) {
echo 'Awesome tables!';
}
 
}

PHP: do something on category pages only


add_action( 'woocommerce_before_main_content', 'bbloomer_loop_cat' );

function bbloomer_loop_cat() {

if ( is_product_category() ) {
echo 'This will show on every Cat pages';
} else {
echo 'This will show on all other Woo pages';
}

}

PHP: do something based on category name


add_action( 'woocommerce_before_main_content', 'bbloomer_loop_cat_slug' );

function bbloomer_loop_cat_slug() {

if ( is_product_category( 'books' ) ) {
echo 'This will show on the Books Cat page';
} elseif ( is_product_category( 'chairs' ) ) {
echo 'This will show on the Chairs Cat page';
}

}

PHP: do something on tag pages only


add_action( 'woocommerce_before_main_content', 'bbloomer_loop_tag' );

function bbloomer_loop_tag() {

if ( is_product_tag() ) {
echo 'This will show on every Cat pages';
} else {
echo 'This will show on all other Woo pages';
}

}

PHP: do something based on tag name


add_action( 'woocommerce_before_main_content', 'bbloomer_loop_tag_slug' );

function bbloomer_loop_tag_slug() {

if ( is_product_tag( 'red' ) ) {
echo 'This will show on the Red Tag page';
} elseif ( is_product_tag( 'yellow' ) ) {
echo 'This will show on the Yellow Tag page';
}

}

3. Are you working on WooCommerce Pages?

PHP: do something if on a WooCommerce page (excluding cart/checkout)


add_action( 'woocommerce_before_main_content', 'bbloomer_woo_page' );

function bbloomer_woo_page() {

if ( is_woocommerce() ) {
echo 'This will show on Woo pages';
} else {
echo 'This will show on WP pages';
}

}

PHP: do something if on Cart/Checkout


add_action( 'woocommerce_sidebar', 'bbloomer_cart_checkout' );

function bbloomer_cart_checkout() {

if ( is_cart() ) {
echo 'This will show on the Cart sidebar';
} elseif ( is_checkout() ) {
echo 'This will show on the Checkout sidebar';
}

}

4. Are you working on the WooCommerce Cart/Checkout?

PHP: do something if WooCommerce Cart/Checkout has Product ID


function bbloomer_find_id_in_cart() {
global $woocommerce;     
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
          
    $product = $values['data'];
  
    if ( $product->id == 123 ) {
        // do something
    }
  
}
}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

61 thoughts on “WooCommerce Conditional Logic – Tags, Examples & PHP

  1. Hello Rodolfo –

    At first, a big “THANK YOU” for this great article. It helped me a lot to understand a little more of these php-actions, though I’m a WP-newbie. Unfortunately I wasn’t able to solve my problem, of creating a php-snippet that hides the banner and the title ONLY on the Shop-categories- and subcategories-pages, WITHOUT hiding them on the Shop-Starting-page.

    Although this code-example from above, works successfully

    add_action( 'woocommerce_before_main_content', 'bbloomer_loop_cat' );
     
    function bbloomer_loop_cat() {
    if ( is_product_category( 'segel' ) ) {
    echo 'This will show on the Segel Cat page';
    } else {
    echo 'This will show on all other Woo pages';
    }
    }
    
    

    And though I’ve found this code that effectively hides the banner and title on EACH Woocommerce page,

    add_filter( 'woocommerce_show_page_title', '__return_false' );

    I’m constantly failing to combine these 2 codes. I guess I have tried dozens of combinations but nothing seems to work. Therefore I desperately need some support and would appreciate any help.

    With kind regards
    Reca

    1. Hello Reca, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  2. I’m using your code to remove a wishlist button from up sell products as follows

    function ssheds_remove_upsell_wishlist() {
    global $woocommerce_loop;
    if ( is_product() && $woocommerce_loop['name'] == 'up-sells' ) {
    remove_action('woocommerce_after_shop_loop_item','tinvwl_view_addto_htmlloop',10);
    }
    }add_action ('woocommerce_after_shop_loop_item','ssheds_remove_upsell_wishlist');

    This works fine for all the up-sell products all display without the wishlist button except for the first product which still has it showing, any ideas why this might be happening?

    1. Hi Ben, had a similar issue yesterday with a client. Instead, try to remove_action for all products, and then add_action to re-add them if it’s not the up-sells (basically the opposite of what you’re doing). Let me know

  3. Hi,
    Is that possible to add a hook of “Estimated Delivery Time: 5 Days” for a particular shipping class (like ‘excessweight’) in Cart/Checkout page. Can you please post the hook on this?
    Thanks

    1. Hello Fawazk, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  4. Great read and checked out the plugins etc but still can’t find (or figure out) what to do.

    On my page that shows my products (product image, title, price, cart link and email link) I want to add some custom text (between the title and price). I know how to hack the files to add custom text but this would apply it to all products. I just want to add some text for a handful of products.

    Can anyone help with this?

    1. Hi Nathaniel, if those products have a tag or category in common, there are examples in this article – let me know

  5. Hello Rodolfo,

    How can I use a social share locker shorcode like this [shortcode]…[/shortcode] for lock add cart button in specific product ID ?

    Thanks you for your help.

    1. Hey Stan, thanks for your comment! You can “print” a shortcode via PHP: https://developer.wordpress.org/reference/functions/do_shortcode/ – this means you can then also use conditional logic. Hope this helps

  6. Ho Rodolfo and thanks for all the work you do.

    I am using 2 of your code snippets – single page with a defined product tag and I am trying to call your:

    “add_action( ‘woocommerce_single_product_summary’, ‘bbloomer_custom_action_above_title’, 5 );” hook conditionally using this.

    I am obviously missing a piece of the puzzle – I must be calling it wrong. I’m not asking you to code it for me, rather just point me in the right direction with respects to how to conditionally call this new function/hook.

    Any help much appreciated

    thanks

    1. Hey Simon, thanks for your comment! The conditional check goes inside the “bbloomer_custom_action_above_title” function – did you place it there correctly?

  7. Hi!

    I had a question about something that you didn’t cover in this post. How can you echo text based on the price?

    Ex:
    if price =<$20 then echo "x"
    if price =<$40 then echo "y"
    if price =<$60 then echo "z"

  8. Great resource, thank you.

    I’m banging my head against the wall on something though and wondered if you’d be willing to help me.

    In a nutshell, I need to display a text note on a single product pages for downloadable products. This should be pretty straightforward using “if( $product->is_downloadable() )” and it works with simple products but I can’t get it to work on any of my downloadable products that are also variable (which is all of them!).

    I’ve tried every approach and alternative I can think of but am completely stumped. Maybe you can spot where I’ve gone wrong:

    //add text note to product description page for all downloadable products
    add_action( 'woocommerce_before_add_to_cart_button' , 'append_download_note' );
    function append_download_note() {
    	global $product;
    	if ( $product->is_downloadable() ) {	
            echo '<p>Please note: a link to your downloadable product will be available on the purchase confirmation page.</p>';
        }
    }

    I can get this to work if I use

    if ( is_product() ) 

    or

     if ( $product->is_type( 'variable' ) ) 

    .

    I’ve also tried using the product category

    if ( has_term( 'Knitting Patterns' ) ) 

    but that doesn’t work.

    And tried

    if ( $product->is_type( 'variable' ) &&  $product->is_downloadable() ) 

    but no joy there either.

    Really scratching my head here. Any advice appreciated!

    1. Hey there 🙂 I guess it’s not the product that is downloadable – it’s the single “variation” i.e. the one that is selected from the dropdown. This is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  9. Thanks a lot! Exactly what I needed.

    1. Bummer that you wouldn’t share the solution… I get it though, have the same situation with a project that’s run way over budget and need to hide some stuff based on it being variable and virtual. Because the project’s over budget I can’t get anyone to pay for what I wouldn’t think would be an extraordinary solution. If anyone else has any ideas, I would love to hear them!

  10. Hi, thanks for all this info. What I’m trying to do is get the price to change on the product page when specific variables are selected. Right now, it keeps just showing the range and only shows the actual price on the checkout page. Please assist in how I should go about this or where I should look for more info.

    1. Hey Ditshego, thanks for your comment! That to me sounds like a theme bug / plugin conflict. Take a look at this tutorial to see how to troubleshoot: https://businessbloomer.com/woocommerce-troubleshooting-mistakes-to-avoid/

  11. Hi, Rodolfo, thanks for your great post.
    I have a woocmmerce website, there has two difference category and sub category for product. and two custom header image for different category and its sub category, I used divi theme.

    Category group like WFCA, Franklin

    I want a function for single product page, if the product from WFCA then header image should be WFCA header image otherwise it should be Franklin header image,

    would you please help on this by a example functions

    thanks

    1. Faruk, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  12. Hi I have used some conditional logic throughout my site to alter the display of the Price. Particularly to show variation prices as ‘From: 0.000’ in some places and ‘4.00 – 5-00’ in others.

    One of the conditions I’m using is the related products one from this article:
    if ( is_product() && $woocommerce_loop[‘name’] == ‘related’ ) {

    My question is, is there a similar conditional that exists for upsells and crosssells? Because I can’t seem to target those prices.

    1. Hey Craig, thanks for your comment! Try with:

      
      $woocommerce_loop['name'] == 'up-sells'
      
      $woocommerce_loop['name'] == 'cross-sells'
      
      
    2. Thank you! Got that to work nicely in a conditional along with the related. Happy days!

  13. hi rodolfo –

    thank you for all the great work you’re doing to help!

    i have a scenario which i need some help with…

    for customers who need to purchase service “X” for a number of named people, i need to restrict them to making multiple individual purchases of service “X” – within the same transaction.

    so tom needs to purchase a total of 3 of service “X” for john, jane and megan. so i need to restrict him to place 3 individual orders within the same transaction.

    the way i need to do this is simply to remove the quantities option from the product page of service “X”.

    i tried adding the following css code, but it removes the quantities option from ALL services – and i only want it removed from the product page of service “X”

    .quantity.buttons_added {
    display: none;
    }

    i am not a programmer, and came across your site via a google search…and feel there’s something on this page here:

    https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/

    …but just don’t quite know what it is that could be used to help.

    i would dearly appreciate any guidance you can provide.

    many thanks.

  14. Hi dear Rodolfo,

    What if I want a scenario where if product(s) having tag “A” is added to the cart, then product(s) having tag “B” should not be able to be added to the cart together. And vise versa; if product(s) added to cart has tag “B” then it should prevent products having tag “A” from being added to the tag.

    I appreciate all your efforts, expecting to hear from you. Thanks again

    1. Hey Donald, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  15. Hi,

    Thank you for the article. I’ve bookmarked it for future reference. Unfortunately, I can’t seem to get the function to modify the cart based on contents to work. As a test, I have this:

    //if cart contains product id 84
    function bbloomer_find_id_in_cart() {
    global $woocommerce;     
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
               
        $product = $values['data'];
       
        if ( $product->id == 84 ) {
            echo "hooplah!";
        } else {
    		echo "garbanzo";
    	}
       
    }
    }
    

    I added to the end of functions.php. I added the “else” operator just to see if I could get any output. I don’t really need to echo text, but rather I’m going to run some javascript. I suppose I could just use javascript , but I would prefer a server-side option to keep the end user’s experience clean and snappy.

    I’m still in development, and I’m just using a child theme of twentyseventeen. I know there have been some major updates to WooCommerce which might have changed terminology. Could this be the case? Thank you for your guidance!

    1. Uhm, just wondering if you’re calling the function bbloomer_find_id_in_cart() via a hook? Otherwise it’s not going to echo anything

  16. Hi Rodolfo,

    Just wanted to say a quick thanks for all the content you put out around Woocommerce it definitely helps a lot.
    My question is I’m new to PHP and I’m trying to organise my affiliate site so when someone clicks on the external product image/add to cart button/title on the shop page, it opens up the affiliate link in a new tab.

    I’ve been using the below code for just the add to cart button.
    Do you know what I could do to make it applicable to the product image/title/add to cart all the same time?

     
    // Product Loop Add to cart target blank
    add_filter( 'woocommerce_loop_add_to_cart_link', 'add_target_blank', 10, 2 );
     
    function add_target_blank( $link, $product ){
     
    // I simply added target="_blank" in the line below
    $link = sprintf( '<a href="%s" target="_blank" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button product_type_%s">%s</a>',
            esc_url( $product->add_to_cart_url() ),
            esc_attr( $product->id ),
            esc_attr( $product->get_sku() ),
            esc_attr( isset( $quantity ) ? $quantity : 1 ),
            esc_attr( $product->product_type ),
            esc_html( $product->add_to_cart_text() )
        );
    return $link;
    

    Thanks Heaps

    1. Jesse, thank you for your comment! You will need to basically change the “loop” product image and product title link – I recommend you study the “hooks” on the loop pages so that you can find out how to remove the existing and replacing it with yours: https://businessbloomer.com/woocommerce-visual-hook-guide-archiveshopcat-page/

  17. Hi,

    How I can do to retreive the attribute of product ?

    there is any way with has_term ?

    thanks

  18. Hi,
    Could you give another example, but for the single product page sidebar? I’ve tried adding the code below to the “PHP Code Widget” in the sidebar, but it doesn’t work properly as it returns “test6” in the sidebar for every single product!

    Thanks!

    <?php
    if (is_product_tag( 'adexample1' )) {
    echo "test1";
    } elseif (is_product_tag( 'adexample2' )){
    echo "test2";    
    } elseif (is_product_tag( 'adexample3' )){
    echo "test3";  
    } elseif (is_product_tag( 'adexample4' )){
    echo "test4";  
    } elseif (is_product_tag( 'adexample5' )){
    echo "test5";  
    } else {
    echo "<p>test6</p>";
    }
    ?>
    
    1. Hey Ben, thanks for your comment! Pay attention at is_product_tag: that’s a conditional tag that only works on a tag page, and not on the single product page. Instead, you should use this: “PHP: do something if product belongs to a tag”, I think it’s the 5th or 6th example. Let me know!

  19. Dear sir, i want postocode based simple static delivery time for each postcode. if you have any ideal please help me.

    1. Hey Kedar, thanks for your comment! You’ll need a plugin for that, sorry 🙂

  20. Thanks for your great tutorial. I want to do a different thing but don’t know how to do it?

    There is a ‘verified buyer’ options in woocommerce plugin that is applicable only for them who buy something and leave their review they are only be considered as a verified buyer and there is a text shown after commenter/reviewer name as ‘verified buyer’.

    I want to show ‘verified buyer’ text even if customer doesen’t buy anything and there might be a option in backend to allow admin to enable verified buyer or disable it for any customer. If enable the option customer will be considered as verified buyer otherwise no. Is that possible? Can you help me please?

    Sorry for my poor English.

    1. Emran, thanks so much for your comment! Unfortunately I have no idea in this case, and I’d suggest you talk to the plugin developers to see if they can help (you’re paying them for this sort of help!). Let me know how it goes 🙂

  21. Hi Rodolfo,
    Your tutorials are great! I have one issue that has been difficult in resolving, and wondered whether you could help?
    It is concerning conditional logic of product attributes and variations. As a photographer I want users to be able to select a photo size, and then a display option (I have these all set up and working OK). Next, I want two other select boxes to show only when certain selections are made.

    ie. If the display option is ‘Print Only’ – nothing need happen. However, if the option is ‘Framed Print’, then I would like two more options (Mount Colour and Frame Style) to show beneath.

    Can this be done programmatically as I can’t find a way to do it without having to purchase the likes of Gravity forms?

    Regards
    Rob

    1. Hiya Rob, thanks for your comment! I was going to suggest Gravity Forms until I read your last sentence 🙂 Either way, this is way more efficient and cost effective than trying to do it and develop it in JQuery, which would take you time and money anyway in my opinion. Hope this helps!

  22. Hi
    I also had problems with the above displayed examples.
    It all depends on the theme. I made one myself and
    changed the loop in woocommerce.php
    With this code the archive-product.php can finally be reached and altered.
    Thanks for all your tutorials.
    regards
    theo

    1. Thanks for your feedback Theo!

  23. Hello, thank you so much for your helpful website. I’m learning PHP and struggling to add handling fees to my cart, but only for certain product categories (the ones that are actually physical and to be shipped, not the digital ones). The category slugs are ‘printed’ and ‘imprimee’, or their ID numbers are 57 and 77. I tried many things but can’t figure out how to correctly add the product category condition to the php code:

    
    add_action( ‘woocommerce_cart_calculate_fees’,’endo_handling_fee’ );
    function endo_handling_fee() {
     global $woocommerce;
    
    if ( is_admin() &amp;&amp; ! defined( ‘DOING_AJAX’ ))
    return;
    
    $fee = 3.00;
     $woocommerce-&gt;cart-&gt;add_fee( ‘Handling’, $fee, true, ‘standard’ );
    }
    
    
    1. Hey Johanna, thanks for your comment! You can see an example of conditional “add fee” here: https://businessbloomer.com/woocommerce-add-fee-to-cart/

      It’s not the same example you’re referring to but combining the info in this tutorial and the snippet I referenced should get you closer 🙂

      Let me know!

  24. Hi Rodolfo!

    I tried out the snippit for the single product page. I only want to show a message before the add to cart button on variation products.
    Question is, can I use a add action inside the snippit? Because it does not work.

    Here’s my code.

     // Laat de tekst voor de voorraadstatus alleen zien op variable producten
    add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_product_type' );
     
    function bbloomer_single_product_type() {
     
    if( $product->is_type( 'variable' ) ){
    	// Voegt een tekst toe voor de voorraadstatus
     add_action( 'woocommerce_before_add_to_cart_button', 'return_policy', 10 );
    function return_policy() {
        echo '<h2 class="quick-overview">Voorraadstatus: Kies eerst een uitvoering.</h2>';
    }
     
    } elseif( $product->is_type( 'external' ) ){
     // do something
    } elseif( $product->is_type( 'grouped' ) ){
     // do something
    } 
     
    }
    
    1. Hey Sander, thanks for your message! There is something wrong in your code, and it can simply be fixed by removing the first action, using your “inside-the-function” action instead, and use the conditional logic inside the function, without the need of calling a new add_action. Not sure if this is clear enough, but hope it helps a little 🙂

    2. Thanks for your reply Rodolfo. So i tried out some different things, from which all ended in disaster 😉

      I removed the first action and used the action i had inside the conditional logic. I also tried using only the function but that gave errors.

       
      // Laat de tekst voor de voorraadstatus alleen zien op variable producten
       add_action( 'woocommerce_before_add_to_cart_button', 'voorraad', 10 );
      function voorraad() {
        
      if( $product->is_type( 'variable' ) ){
      
          echo '<h2 class="quick-overview">Voorraadstatus: Kies eerst een uitvoering.</h2>';
        
      } elseif( $product->is_type( 'external' ) ){
       // do something
      } elseif( $product->is_type( 'grouped' ) ){
       // do something
      } 
        
      }
      
    3. This function should work. What error are you getting?

    4. I’m getting this error:

       
      Fatal error: Call to a member function is_type() on null in E:\xampp\htdocs\global\wp-content\themes\furnicom-child-theme\functions.php on line 177 
    5. Line 177:

      if( $product->is_type( ‘variable’ ) ){

    6. Hey Sander, thanks for that! I’d say you need to declare the global $product as the snippet doesn’t know what it’s inside. Add this inside the function and let me know:

      global $product;
      
    7. Thanks Rodolfo! That was it. Is that something that was missing in the original snippet, or is this only for my example?

    8. Not sure 🙂 Let’s see if some other reader reports the same error!

  25. If I have a subscription, the reoccurring totals section for shipping is always listed as: Shipping Via “whatever method name” then “Price(or “Free” if price is 0.) I wanted to know how do I remove the stupid Shipping via hook, because it is extremely redundant because I offer a subscription with free shipping. So in the order details it literally says “Shipping via Free Shipping” “Free.”

    Thanks.

    1. Hey Adam, thanks for your comment! Your issue is a little off-topic… however I happen to have a resource that might be of help: https://businessbloomer.com/woocommerce-remove-shipping-labels-cart-checkout-page-e-g-flat-rate/. Let me know 🙂

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.