Home / Forums / Lafka – Fast Food Ordering WooCommerce Theme / ResolvedFunction for if user forgot order Drink + Code
ResolvedFunction for if user forgot order Drink + Code
Tagged: cart
- This topic has 2 replies, 2 voices, and was last updated 2 years, 9 months ago by
01.mrlast.
Viewing 3 posts - 1 through 3 (of 3 total)
- AuthorPosts
- December 14, 2020 at 4:19 pm #27772
01.mrlast
ParticipantHi
I wrote a code and said maybe someone needs it, and because it was a good template, I said I would write it for you too
You must add this code inside the functionAnd if the user forgets to order something like a drink, he will tell them that you have forgotten
add_action('woocommerce_before_cart', 'bbloomer_check_category_in_cart'); function bbloomer_check_category_in_cart() { // Set $cat_in_cart to false $cat_in_cart = true; // Loop through all products in the Cart foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { // If Cart has category "download", set $cat_in_cart to true if ( has_term( 'drink', 'product_cat', $cart_item['product_id'] ) ) { $cat_in_cart = false; break; } } // Do something if category "download" is in the Cart if ( $cat_in_cart ) { // For example, print a notice echo "<h1 style='background-color:red'>"; wc_print_notice( 'You forgot to order a drink!', 'notice' ); echo "</h1>"; // Or maybe run your own function... // .......... } } add_action( 'woocommerce_checkout_before_customer_details', 'webroom_check_if_product_category_is_in_cart' ); function webroom_check_if_product_category_is_in_cart() { $cat_in_cart = true; foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { if ( has_term( 'drink', 'product_cat', $cart_item['product_id'] ) ) { $cat_in_cart = false; break; } } // Do something if the category is in the Cart if ( $cat_in_cart ) { echo "<h1 style='background-color:red'>"; wc_print_notice( 'You forgot to order a drink!', 'notice' ); echo "</h1>"; } }
December 15, 2020 at 2:36 pm #27784Althemist
KeymasterHello 01.mrlast,
Thanks for sharing.
December 15, 2020 at 8:16 pm #2782101.mrlast
ParticipantYou’re welcome <3
- AuthorPosts
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in and have valid license to reply to this topic.