admin管理员组文章数量:1406926
I'm trying to set notifications when I have products from these two different categories inside the card in WooCommerce
.
This is the code which I using:
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 = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( 'cat1', 'product_cat', $cart_item['product_id'] ) &&
has_term( 'cat2', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
break;
}
}
if ( $cat_in_cart ) {
$notice = 'Notification';
wc_print_notice($notice, 'notice');
}
}
This code works perfectly if I only set one category, but when I set two categories, for some reason I don't have results nor errors.
I'm trying to set notifications when I have products from these two different categories inside the card in WooCommerce
.
This is the code which I using:
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 = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( 'cat1', 'product_cat', $cart_item['product_id'] ) &&
has_term( 'cat2', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
break;
}
}
if ( $cat_in_cart ) {
$notice = 'Notification';
wc_print_notice($notice, 'notice');
}
}
This code works perfectly if I only set one category, but when I set two categories, for some reason I don't have results nor errors.
Share Improve this question asked Nov 22, 2019 at 20:35 batmanbatman 51 bronze badge2 Answers
Reset to default 0Quickly, I thought about that :
(but it's not my best! Maybe I'll come rewrite a more thoughtful code sometimes but for now, this code'll work)
add_action( 'woocommerce_before_cart', 'webroom_check_if_product_category_is_in_cart' );
function webroom_check_if_product_category_is_in_cart() {
$cat1_in_cart = false;
$cat2_in_cart = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( 'cat1', 'product_cat', $cart_item['product_id'] ) ) {
$cat1_in_cart = true;
} elseif(has_term( 'cat2', 'product_cat', $cart_item['product_id'] )){
$cat2_in_cart = true;
}
}
if ($cat1_in_cart === true && $cat2_in_cart === true) {
$notice = 'Notification';
wc_print_notice($notice, 'notice');
}
}
Here is the solution:
add_action( 'woocommerce_before_cart', 'webroom_check_if_product_category_is_in_cart' );
function webroom_check_if_product_category_is_in_cart() {
$cat1_in_cart = false;
$cat2_in_cart = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( 'cat1', 'product_cat', $cart_item['product_id'] ) ) {
$cat1_in_cart = true;
} elseif(has_term( 'cat2', 'product_cat', $cart_item['product_id'] )){
$cat2_in_cart = true;
}
}
if ($cat1_in_cart === true && $cat2_in_cart === true) {
$notice = 'Notification';
wc_print_notice($notice, 'notice');
}
}
本文标签: phpSet notification if is two product category in cart
版权声明:本文标题:php - Set notification if is two product category in cart 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744976067a2635522.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论