【发布时间】: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