【发布时间】:2021-03-19 11:36:16
【问题描述】:
我正在尝试在我的网站的购物车页面上显示一条警报消息,管理 2 个不同类别的 2 个不同数量倍数。
分类如下:
- Birre 33 cL (slug: birre-33-cl ID:187) 用于多个 6 瓶
- Birre 75 cL (slug: birre-75-cl ID:188) 用于多个 12 瓶
我需要分别计数和管理(您可以购买 12/24/36...瓶 33 cL 和 6/12/18...瓶 75 cL,但不能购买 6 瓶 33 cL 和 6一瓶 75 cL)。
我已修改 this code 以满足我的需要:
//FILL THE BOX BIRRE 33 CL
function has_product_category( $product_id, $category_ids ) {
$term_ids = array();
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
if( $term->parent > 0 ){
$term_ids[] = $term->parent; // Set the parent product category
$term_ids[] = $term->term_id;
} else {
$term_ids[] = $term->term_id;
}
}
return array_intersect( $category_ids, array_unique($term_ids) );
}
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
$multiples = 12;
$total_products = 0;
$category_ids = array( 187 );
$found = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_product_category( $cart_item['product_id'], $category_ids ) ) {
$total_products += $cart_item['quantity'];
$found = true;
}
}
if ( ( $total_products % $multiples ) > 0 && $found )
wc_add_notice( sprintf( __('FILL YOUR 33 cL BOX: you have to buy multiples of %s.', 'woocommerce', 'woocommerce'), $multiples ), 'error' );
}
不幸的是,我无法管理 2 个不同的类别,每个类别显示不同的消息。
感谢任何帮助。
【问题讨论】:
标签: php wordpress woocommerce cart taxonomy-terms