Woocommerce IDS批量折扣

问题描述 投票:0回答:1

我正在尝试在Woocommerce中设置批量折扣。现在我有这个

add_action( 'woocommerce_before_calculate_totals', 'se_bulkdiscount_on_ids', 9999 );

function se_bulkdiscount_on_ids($cart ) {
$specialprice = array(
    2 => '1.75',
    3 => '1.62',
    4 => '1.61',
    8 => '1.59',
); 

foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {          
    foreach($specialprice as $code => $price){
        if($cart_item['quantity'] >= $code){
            $cart_item['data']->set_price( $price );
        }
    }
} 
}

?>

但是我如何仅针对特定IDS设置此折扣?

如果我有ID 1300 1x和1403 2x,那么它的数量为3,而每片价格为1.62

php woocommerce shopping-cart
1个回答
0
投票

假设您是这个意思?注释,并在代码中添加了说明]

function se_bulkdiscount_on_ids( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) 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( 30, 813 );

    // total items in the cart
    $count = $cart->cart_contents_count;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {    
        // Get product id
        $product_id = $cart_item['product_id'];

        // Compare
        if ( in_array( $product_id, $specific_product_ids ) ) {
            // set new price
            $cart_item['data']->set_price( $special_price[$count] );            
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'se_bulkdiscount_on_ids', 10, 1 );
© www.soinside.com 2019 - 2024. All rights reserved.