我需要某些项目有一个混合和匹配的数量折扣的基础上类别。我已经看到了一些其他的代码,实现这一点使用整体折扣在底部的车,但是我需要每个项目的价格,以反映折扣,我也需要这个价格结转到销售订单,所以我们可以适当的发票.以下是我目前所掌握的:
add_action( 'woocommerce_cart_calculate_fees','custom_bulk_discount', 20, 1 );
function custom_bulk_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$category = 'ferns';
$calculated_qty = 0;
$calculated_total = 0;
foreach($cart->get_cart() as $cart_item):
if(has_term($category, 'product_cat', $cart_item['product_id'])):
$item_price = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->price : $cart_item['data']->get_price(); // The price for one (assuming that there is always 2.99)
$item_qty = $cart_item["quantity"];// Quantity
$item_line_total = $cart_item["line_total"]; // Item total price (price x quantity)
$calculated_qty += $item_qty; // ctotal number of items in cart
$calculated_total += $item_line_total; // calculated total items amount
endif;
endforeach;
if ( $calculated_qty >= 100 && $calculated_qty < 200 ){
foreach($cart->get_cart() as $cart_item):
$price = $cart_item['data']->price - 1;
if(has_term($category, 'product_cat', $cart_item['product_id'])):
$cart_item['data']->set_price( $price );
endif;
endforeach;
}else if( $calculated_qty >= 200){
foreach($cart->get_cart() as $cart_item):
$price = $cart_item['data']->price - 1.5;
if(has_term($category, 'product_cat', $cart_item['product_id'])):
$cart_item['data']->set_price( $price );
endif;
endforeach;
}
}
问题是小计没有更新正确的价格。我假设这是因为我只改变了显示价格,而不是实际计算的价格。有办法做到我要求的吗?
1条答案
按热度按时间8gsdolmq1#
首先,你没有使用正确的钩子,你的代码可以被简化,并且有遗漏的东西。
要根据特定产品类别的数量折扣购物车项目尝试:
代码放在子主题的functions.php文件中(或插件中)。应该可以的