Php 基于商业中特定产品数量的折扣价格

Php 基于商业中特定产品数量的折扣价格,php,wordpress,woocommerce,product,price,Php,Wordpress,Woocommerce,Product,Price,我想根据用户在购物车中选择的数量修改我的价格 我有一份价目表(每瓶的价格): 如果他选择了一个不在价目表中的数量,我会给他以下数量的价格,例如: 如果用户选择20瓶的数量,我会指定15瓶的价格,即每瓶105欧元 如果用户选择45瓶的数量,我会指定30瓶的价格,即每瓶99欧元 代码如下: add_action( 'woocommerce_before_calculate_totals', 'quantite', 9999 ); function quantite($cart){ g

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

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

如果他选择了一个不在价目表中的数量,我会给他以下数量的价格,例如:

  • 如果用户选择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();
        }
    }
}
add_action('woocommerce_-before_-calculate_-totals','quantite',9999);
函数量化($cart){
全球$产品;
if(定义了('DOING'uajax'))
返回;
如果(did_action('woocommerce_before_calculate_totals')>=2)
返回;
foreach($cart->get\u cart()作为$cart\u项目){
$product=$cart_项目['data'];
如果($product->get_id()==59){
如果($cart_item['quantity']==1){
$value['data']->定价(135.00);
$new_price=$value['data']->get_price();
} 
}其他($cart_项目['quantity']==2){
$value['data']->定价(135.00);
$new_price=$value['data']->get_price();
}其他($cart_项目['quantity']<5和$cart_项目['quantity']>=3){
$value['data']->定价(125.00);
$new_price=$value['data']->get_price();
}其他($cart_项目['quantity']<10&$cart_项目['quantity']>=5){
$value['data']->定价(120.00);
$new_price=$value['data']->get_price();
}其他($cart_项目['quantity']<15&$cart_项目['quantity']>=10){
$value['data']->定价(110.00);
$new_price=$value['data']->get_price();
}其他($cart_项目['quantity']<30&$cart_项目['quantity']>=15){
$value['data']->定价(105.00);
$new_price=$value['data']->get_price();
}其他($cart_项目['quantity']<60&$cart_项目['quantity']>=30){
$value['data']->定价(99.00);
$new_price=$value['data']->get_price();
}elseif($cart_item['quantity']<100&$cart_item['quantity']>=60){
$value['data']->定价(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 );
    }
}
add_action('woocommerce_-before_-calculate_-total','conditional_-price_-based_-on_-quantity',10000);
函数条件价格基于数量($cart){
全球$产品;
if(定义了('DOING'uajax'))
返回;
如果(did_action('woocommerce_before_calculate_totals')>=2)
返回;
$targeted_id=array(59);//这里设置您的目标产品id
$price=0;//正在初始化
foreach($cart->get\u cart()作为$cart\u项目){
$quantity=$cart_项目['quantity'];
if(数组相交($targeted\u id,数组($cart\u item['product\u id'],$cart\u item['variation\u id'])){
如果($quantity=3&&$quantity<5){
$price=125.00;
}其他($quantity>=5&$quantity<10){
$price=120.00;
}其他($quantity>=10&$quantity<15){
$price=110.00;
}其他($quantity>=15&$quantity<30){
$price=105.00;
}其他($quantity>=30&$quantity<60){
$price=99.00;
}其他(数量>=60){
$price=95.00;
}
}
}
如果($price>0){
$cart_item['data']->set_price($price);
}
}

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

非常感谢,它工作得非常好!
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 );
    }
}