简体   繁体   English

WooCommerce 基于产品ID的批量折扣

[英]WooCommerce bulk discount based on product ids

I am trying to program a bulk discount in WooCommerce.我正在尝试在 WooCommerce 中设置批量折扣。

For now, I have this现在,我有这个

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 );

But how can I set this discount only on specific product IDs?但是,我如何才能仅针对特定产品 ID 设置此折扣?

If I have ID 1300 1x and 1403 2x this has a quantity of 3 together than the price is 1.62 per piece如果我有 ID 1300 1x 和 1403 2x 这有 3 个数量一起比价格是每件 1.62

Assuming you mean this?假设你是这个意思? comment with explanation added in the code注释并在代码中添加解释

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

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

    /* SETTINGS */

    // Set special prices
    $special_price = array(
        1 => 1.1,   
        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 );

    /* END SETTINGS */

    // total items
    $count = 0;

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

        // Quantity
        $product_quantity = $cart_item['quantity'];

        // Compare
        if ( in_array( $product_id, $specific_product_ids ) ) {
            $count += $product_quantity;
        }
    }

    // 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 ) ) {
            // If count is in range of the array
            if ( $count >= 2 & $count <= count( $special_price ) ) {
                // set new price
                $cart_item['data']->set_price( $special_price[$count] );                    
            } elseif ( $count > count( $special_price ) ) {
                // set new price
                $cart_item['data']->set_price( end($special_price) );       
            }             
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'se_bulkdiscount_on_ids', 10, 1 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce 中特定产品标签 ID 的购物车批量折扣 - Cart bulk quantity discount for specific product tag IDs in WooCommerce WooCommerce 中特定产品 ID 的批量折扣 - Bulk Discount for specific product ID in WooCommerce 基于 WooCommerce 产品自定义输入字段的折扣 - Discount based on WooCommerce product custom input fields WooCommerce 特定优惠券折扣基于产品类别 - WooCommerce specific coupon discount based on product category Woocommerce中基于产品类别的第二项的数量折扣 - Quantity Discount for 2nd Item Based on Product Category in Woocommerce 基于Woocommerce中特定产品变化的购物车项目折扣 - Cart item discount based on specific product variation in Woocommerce 基于 WooCommerce 中产品 ID 的 2 种不同固定产品折扣的优惠券 - Coupon with 2 differents fixed product discounts based on product IDs in WooCommerce 仅当特定产品在 WooCommerce 购物车中时,才根据特定产品类别应用/确定折扣 - Apply/determine discount based on a particular product category only when a specific product is in WooCommerce cart Woocommerce 中特定产品的购物车批量折扣 - Cart bulk quantity discount for specific products in Woocommerce WooCommerce 删除基于产品 ID 和购买数量的运输方式 - WooCommerce remove shipping method based on product IDs & quantity bought
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM