我需要某些项目有混合和匹配的数量折扣的基础上.我已经看到在购物车底部使用总体折扣来实现这一点的一些其他代码,但是我需要每个项目的价格来反映折扣,我还需要将该价格结转到销售订单中,以便我们可以适当地开具发票.以下是我目前掌握的情况:
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;
}
}
问题是小计没有以正确的价格更新.我假设这是因为我只更改了显示价格,而不是实际计算的价格.有什么办法可以照我说的做吗?