【问题标题】:Discounted price based on quantity for specific products in WooCommerce基于 WooCommerce 中特定产品数量的折扣价
【发布时间】:2021-05-18 07:49:22
【问题描述】:

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

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

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

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

  • 如果用户选择 20 瓶的数量,我指定 15 瓶的价格,即每瓶 105 欧元

  • 如果用户选择 45 瓶的数量,我指定 30 瓶的价格,即每瓶 99 欧元。

代码如下:

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

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

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

    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 文件中。它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2020-08-30
      • 2021-01-29
      • 2021-06-10
      • 2019-02-06
      • 2018-12-12
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多