【发布时间】:2020-08-30 01:17:32
【问题描述】:
我正在尝试在 WooCommerce 中设置批量折扣。
现在,我有这个
function se_bulkdiscount_on_ids( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// Set special prices
$special_price = array(
2 => '1.2',
3 => '1.3',
4 => '1.4',
5 => '1.5',
6 => '1.6',
7 => '1.7',
8 => '1.8',
);
// Set product ids
$specific_product_ids = array( 1465, 1785 );
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get product id
$product_id = $cart_item['product_id'];
// Compare
if ( in_array( $product_id, $specific_product_ids ) ) {
foreach($special_price as $quantity => $price){
if($cart_item['quantity'] >= $quantity){
$cart_item['data']->set_price( $price );
}
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'se_bulkdiscount_on_ids', 10, 1 );
但我如何才能仅针对特定产品 ID 设置此折扣?
如果我有 ID 1300 1x 和 1403 2x,数量为 3,而价格为每件 1.62
【问题讨论】:
-
结合特价数组和产品ids数组
标签: php wordpress woocommerce cart hook-woocommerce