Identifying product categories inside if else statements
-
Hi there,
I am looking to create a custom function for woocommerce. The desired scenario is to be able to empty & skip the checkout cart, to go direct to checkout, only if the product belongs to a specific product category. I have done so using the following add_filters
add_filter('woocommerce_add_to_cart_redirect', 'cus_skip_cart_redirect_checkout', 1000); add_filter('woocommerce_add_to_cart_validation', 'cus_clear_cart_before_adding');
Initially, this was working fine for all products, using the following functions:
function cus_skip_cart_redirect_checkout($url) { return wc_get_checkout_url(); } function cus_clear_cart_before_adding($cart_item_data) { global $woocommerce; $woocommerce->cart->empty_cart(); return true; }
However, the functions broke when I tried to include an if statement to make these functions run only if a product in a specific product category was selected:
function cus_skip_cart_redirect_checkout($url) { global $product_category; $id = $product_category->get_id(); if(get_id() == 59) { global $woocommerce; $cus_redirect_url_checkout = $woocommerce->cart->wc_get_checkout_url(); return $cus_redirect_url_checkout; } } function cus_clear_cart_before_adding($cart_item_data) { if(get_the_id() == 59) { global $woocommerce; $woocommerce->cart->empty_cart(); return true; } }
Please advise on how to improve this if statement in this function. Thank you!
- This topic was modified 4 years, 10 months ago by . Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
The page I need help with: [log in to see the link]
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Identifying product categories inside if else statements’ is closed to new replies.