My Courses > CustomizeWoo > Module 5 > Lesson 02: Conditional Logic
Conditional Logic
Conditional logic allow you to display content or run PHP functions on a particular WP/WC page depending on what conditions that page matches.
Video
Please note: English captions are enabled by default (click on “CC” in the video player to disable). Also, you can click on the “gear” icon to manage quality and playback speed. Enjoy!
Sorry, this video is only visible to logged in Business Bloomer Club members with a PRO PASS.
If you joined already, please log in.
Otherwise, here is why you should join the Club.
If you joined already, please log in.
Otherwise, here is why you should join the Club.
Useful Links
WordPress Conditional Tags: https://codex.wordpress.org/Conditional_Tags
WooCommerce Conditional Tags: https://docs.woocommerce.com/document/conditional-tags/
WooCommerce Conditional Logic β Tags, Examples & PHP: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/
Hello, Rodolfo
I am wondering is an if condition depend on screen width or device type?
I know that I can using CSS/Javascript to show/hide some elements using media query. but the elements still loading on the DOM but hidden.
So for the best performance it will be better to control it using the PHP if we can handle this.
Thanks in advance
Hey Hesham, not really, no. The only conditional available to you is https://developer.wordpress.org/reference/functions/wp_is_mobile/
Hello Rodolfo,
I try to translate some strings the customer receives by email. But I want these strings to be translated conditionally, based on the product category in order.
So, I wrote this snippet, but nothing happens:
If I use only function custom_translate_woocommerce_strings alone it is working, but I want to use it conditionally. Your advice, please?
Hey Marcel! So, your code is wrong. The controlling function is always “custom_translate_woocommerce_strings” so the conditional should be inside that function, not elsewhere. Besides, you don’t need the “woocommerce_thankyou” hook.
Also, the “gettext” filter does not allow you to use many conditionals, I mean you can use is_home() or something like that but there is no way to detect whether this is an email and which category is in the order I’m afraid.
WooCommerce “hardcoded” those strings without giving us additional filters or actions, which is bad from a customization point of view.
In this case, you have no other option: duplicate the email template and place it in the /woocommerce/emails folder of your child theme, and write your conditional inside the template itself. Source: https://docs.woocommerce.com/document/template-structure/
Hello, Rodolfo.
I followed how to add content to a specific order email here: https://www.businessbloomer.com/woocommerce-add-extra-content-order-email/.
Please guide me on how I can add specific content to the email if one of the products purchased belongs to a specific product category.
Hi Marcel! You have access to the $order variable, therefore check https://www.businessbloomer.com/woocommerce-check-product-category-order/ and let me know
I managed to add a custom note in the email depending on the product category. Please check this code, I tried and it works:
The problem is that I don’t know where or how I should add:
so that these messages appear only in the processing order email.
Thanks.
Marcel
I found this solution:
and it is working.
Teacher Rodolfo, your thoughts, please?
This would exit from the function and not execute it if the email is “processing order” (it would work for ALL other customer emails). If that’s what you wanted, then it’s perfect
Hello, Rodolfo!
Case:
I need to get the following result: I want to display a notification in the cart about the amount remaining to reach the free shipping threshold, which should be visible ONLY IF product “X” has been added to the cart.
If product “X” has not been added to the cart, I do not want a notification.
The scenario is based on your snippet presented here: https://www.businessbloomer.com/woocommerce-add-need-spend-x-get-free-shipping-cart-page/, but I can’t set the right condition. Can you please guide me?
Here is the snippet I tried to write:
Hi MArcel, you can’t nest functions like that – you need to rewrite the code and place it inside a single function.
Should be like this:
I suggest using this for checking amount in case you change it in future. Also consider this only working if your free shipping is set to world wide.
Thanks!
Hello Rodolfo!
I would like t know if a conditional logic applies to a product category, then all subcategories that belong to that parent category will get the same treatment?
Good question Marcel! No, conditional logic usually targets a category ID or category slug or category name, hence it doesn’t know and doesn’t care whether a category is a parent or a child one. You’ll need to specify a parent category and all its subcategories in case you want to apply a conditional rule to a “group” of categories… otherwise you could study https://codex.wordpress.org/Function_Reference/cat_is_ancestor_of, which should help you target “all subcategories” of a category ID. Hope this helps!
Ok. It seems to be with an advanced grade of difficulty… What about assigning a “tag” to the (sub) categories in discussion and make the function based on that “tag”?
Hey Marcel π If you require to target a set of subcategories, no, you can’t add a “tag” to them. Unless you assign them to an additional product category e.g.:
subcat1
subcat2
subcat3 (cat 1)
subcat4 (cat 1)
Let me know π
I found out if both “parent” and “child” categories are ticked on the product page, the conditional logic rule applies even the PHP code includes just the “parent” category. But if just “child” category is ticked, the conditional rule does not apply anymore for the “parent” category.
https://www.screencast.com/t/80I1oImc
Excellent!