【问题标题】:WooCommerce bulk discount based on product ids基于产品 ID 的 WooCommerce 批量折扣
【发布时间】: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


【解决方案1】:

假设你是这个意思?注释并在代码中添加解释

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

【讨论】:

  • 嗨 7uc1f3r 感谢您的回复 :) 我测试了您发送的代码,但有两个问题。如果数量为 1 或更多 dan 8 我收到错误:警告:wordpress/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php 中遇到的非数字值 如果数量是一个,使用正常价格。如果数量大于数组 special_price 的最后一个键,则使用 special_price 中最后一个键的价格
  • 很抱歉我探讨了另一个问题。如果我添加了一个不在 ID 数组中的产品,你会计算购物车中的所有产品,他会计算错误的价格 hihi
  • 我更新了我的答案。现在只对具有特定 ID 的产品进行计数
猜你喜欢
  • 2021-01-29
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 2019-02-06
  • 2019-01-31
相关资源
最近更新 更多