【发布时间】:2018-05-08 13:52:59
【问题描述】:
我正在努力实现以下目标:
- 一位客户将产品 1(来自图书类别)添加到购物车,成本为 10 美元。
- 然后他将产品 2 添加到购物车。
如果产品 2 在购物车中,如何使产品 1 = 0 美元,并且所有从图书类别中添加的产品为 0 美元?
我的想法是,如果客户购买某种产品,则该产品类别的所有产品都免费。
到目前为止,我得到了这个,但它无法正常工作并且正在将购物车中的所有产品发送到$0:
add_action( 'woocommerce_before_calculate_totals', 'change_price_if_6245_in_cart' );
function change_price_if_6245_in_cart( $cart_object ) {
if (woo_in_cartbooks (6245) ) {
// set our flag to be false until we find a product in that category
$cat_check = false;
// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
// replace 'books' with your category's slug
if ( has_term( 'books', 'product_cat', $product->id ) ) {
$cat_check = true;
// break because we only need one "true" to matter here
break;
}
}
// if a product in the cart is in our category, do something
if ( $cat_check ) {
// we have the category, do what we want
foreach ( $cart_object->get_cart() as $hash => $value ) {
$value['data']->set_price( 0 );
}
}
}
【问题讨论】:
标签: php wordpress woocommerce cart price