【发布时间】:2017-09-27 14:38:06
【问题描述】:
我正在尝试根据最少的购物车商品数量和类别添加自定义购物车折扣。
我已经从这个答案中获取了代码:
Cart discount based on cart item count and only for items that are not in sale
我对其进行了一些更改,这是我的代码:
add_action('woocommerce_cart_calculate_fees' , 'my_custom_discount', 10, 1);
function my_custom_discount( $cart_object ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only when there is 4 or more items in cart
if( $cart_object->get_cart_contents_count() >= 4):
// Defining variables
$categories = array('mycategory1','mycategory2');
$has_category = false;
// Iterating through each item in cart
foreach( $cart_object->get_cart() as $cart_item ){
// Getting an instance of the product object
$_product = new WC_Product( $cart_item['product_id'] );
// If a cart item has the category
if(has_category($category, $_product)){
$has_category = true;
break;
}
}
## Discount calculation ##
$discount = $cart_object->subtotal * -0.03;
## Applied discount (no products on sale) ##
if($has_category )
$cart_object->add_fee( '3% discount', $discount);
endif;
}
我不能让它工作。
我做错了什么以及如何使它起作用?
谢谢
【问题讨论】:
标签: php wordpress woocommerce cart categories