1 回答
TA贡献1943条经验 获得超7个赞
您没有使用正确的钩子,并且缺少一些东西。尝试以下操作:
add_action( 'woocommerce_before_calculate_totals', 'discounted_cart_item_price', 20, 1 );
function discounted_cart_item_price( $cart ){
// Not for wholesaler user role
if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || current_user_can('wholesaler') )
return;
// Required since Woocommerce version 3.2 for cart items properties changes
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE set your product categories in the array (can be IDs, slugs or names)
$categories = array('surfacing-adhesives');
$categories = array('t-shirts');
// Initializing
$found = false;
$count = 0;
// 1st Loop: get category items count
foreach ( WC()->cart->get_cart() as $cart_item ) {
// If product categories is found
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$count += $cart_item['quantity'];
}
}
// Applying discount
if ( $count >= 10 ) {
// Discount calculation (Drop the per item qty price)
$price = $count >= 20 ? 5 : 10;
// 2nd Loop: Set discounted price
foreach ( WC()->cart->get_cart() as $cart_item ) {
// If product categories is found
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$cart_item['data']->set_price( $price );
}
}
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
- 1 回答
- 0 关注
- 146 浏览
添加回答
举报