基于 WooCommerce 中特定产品数量的折扣价

Discounted price based on quantity for specific products in WooCommerce

我想根据用户在购物车中选择的数量修改我的价格。

我有价目表(每瓶数的价格):

1 bottle = 135 €, 
3 bottles = 125 €(per unit), 
5 bottles = 120 € (per unit), 
10 bottles = 110 € (per unit), 
15 bottles = 105 € (per unit), 
30 bottles = 99 € (per unit) , 
60 bottles = 95 € (per unit).

如果他选择的数量不在价目表中,我就给他下面数量的价格,例如:

这是代码:

add_action( 'woocommerce_before_calculate_totals', 'quantite', 9999 );
function quantite($cart){
    global $product;
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;
    foreach ( $cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        if ($product->get_id()==59){
            if ( $cart_item['quantity'] == 1 ) {
                $value['data']->set_price(135.00 );
                $new_price = $value['data']->get_price();
            } 
        } elseif ( $cart_item['quantity'] == 2 ) {
            $value['data']->set_price( 135.00 );
            $new_price = $value['data']->get_price();
        }elseif ( $cart_item['quantity'] < 5 &&  $cart_item['quantity'] >= 3) {
            $value['data']->set_price( 125.00 );
            $new_price = $value['data']->get_price();
        } elseif ( $cart_item['quantity'] < 10 && $cart_item['quantity'] >= 5) {
            $value['data']->set_price( 120.00 );
            $new_price = $value['data']->get_price();
        } elseif ( $cart_item['quantity'] < 15 && $cart_item['quantity'] >= 10) {
            $value['data']->set_price( 110.00 );
            $new_price = $value['data']->get_price();
        } elseif ( $cart_item['quantity'] < 30 && $cart_item['quantity'] >= 15) {
            $value['data']->set_price( 105.00 );
            $new_price = $value['data']->get_price();

        } elseif ( $cart_item['quantity'] < 60 && $cart_item['quantity'] >= 30) {
            $value['data']->set_price( 99.00 );
            $new_price = $value['data']->get_price();

        } elseif ( $cart_item['quantity'] < 100 && $cart_item['quantity'] >= 60) {
            $value['data']->set_price( 95.00 );
            $new_price = $value['data']->get_price();
        }
    }
}

但我收到此错误:致命错误:未捕获错误:在 null

上调用成员函数 set_price()

您的代码中存在多个错误,请尝试以下操作:

add_action( 'woocommerce_before_calculate_totals', 'conditional_price_based_on_quantity', 10000 );
function conditional_price_based_on_quantity( $cart ){
    global $product;
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    $targeted_id = array(59); // Here set your targeted product ids
    $price = 0; // Initializing
        
    foreach ( $cart->get_cart() as $cart_item ) {
        $quantity = $cart_item['quantity'];
        if ( array_intersect($targeted_id, array($cart_item['product_id'], $cart_item['variation_id']) ) ){
            if ( $quantity <= 2 ) {
                $price = 135.00;
            } elseif ( $quantity >= 3 && $quantity < 5 ) {
                $price = 125.00;
            } elseif ( $quantity >= 5 && $quantity < 10 ) {
                $price = 120.00;
            } elseif ( $quantity >= 10 && $quantity < 15) {
                $price = 110.00;
            } elseif ( $quantity >= 15 && $quantity < 30 ) {
                $price = 105.00;
            } elseif ( $quantity >= 30 && $quantity < 60 ) {
                $price = 99.00;
            } elseif ( $quantity >= 60 ) {
                $price = 95.00;
            }
        }
    }
    if( $price > 0 ) {
        $cart_item['data']->set_price( $price );
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。