我有一个产品的标准价格,另外还有6件装(5%折扣)和24件装(10%)折扣的价格。但是,这不是标准的批量折扣,数量6-23给予5%的折扣,然后24个以上的所有人都享有10%的折扣-7的价格为5%的折扣为6,全价的价格为1 ... 30的成本24折价10%,折价5%折价6,33折价10%6折价5%,折价3折价。24
这就是我所拥有的,而且我不确定自己在哪里出了问题,我认为我的逻辑是正确的(我尽了最大的努力来评论自己的想法)-检查24,如果有剩余数量,则对6进行检查,然后在剩余数量上应用单个价格。
我将此添加到了子主题的functions.php中,当我单击以将产品添加到购物车时,它不断旋转,并在控制台中给我一个添加到购物车的ajax错误。谁能指出我要去哪里了吗?
add_action( 'woocommerce_before_calculate_totals', 'quantity_based_pricing', 9999 );
function quantity_based_pricing( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Define discount rules and thresholds
$threshold1 = 6; // Change price if items = 6
$discount1 = 0.05; // Reduce unit price by 5%
$threshold2 = 24; // Change price if items = 24
$discount2 = 0.1; // Reduce unit price by 10%
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
//get product id
$product_id = $cart_item['product_id'];
//get quantity
$quantity = $cart_item['quantity'];
//get price of item
$price = $cart_item['data']->get_price();
if($quantity > $threshold2){
//if threshold 2 goes ecaxtly into the quantity then...
if((int)($quantity % $threshold2)==0){
//for the amount of times threshold 2 goes into quantity loop
for($i=0; $i<(int)($quantity/$threshold2);$i++){
//price += threshold * price(with discount) to give total price.
$new_price+=$threshold2*($price*(1-$discount2));
}
//set the price for the item
cart_item['data']->set_price( $new_price );
}
//else if threshold 2 doesn't go into the quantity exactly then..
else if((int)($quantity % $threshold2)!=0){
//for each time threshold 2 goes into quantity then...
for($i=0; $i<(int)($quantity/$threshold2);$i++){
//set the price to threshold 2 * price(with discount)
$new_price+=$threshold2*$price*(1-$discount2);
}
//work out the remainder of the quantity left over after applying the threshold 2 discount
$remainder = $quantity % $threshold2;
//check if threshold 1 goes into the remaining quantity..
//if it goes in exactly, then...
if((int)($remainder % $threshold1)==0){
//loop the amount of times threshold 1 goes into the quantity
for($i=0; $i<(int)($remainder/$threshold1);$i++){
//add to the price using threshold 1 * price with threshold 1 discount
$new_price+=$threshold1*$price*(1-$discount1);
}
//set the price for the item
cart_item['data']->set_price( $new_price );
}
//if threshold 1 doesn't go exactly into the remaining quantity then...
elseif((int)($remainder % $threshold1)!=0){
//loop the amount of times that threshold 1 goes into the quantity
for($i=0; $i<(int)($remainder/$threshold1);$i++){
//add to the price using threshold1 * price with discount
$new_price+=$threshold1*$price*(1-$discount1);
}
//find remainder from seeing the remainder of threshold1 going into the remaining quantity
$remainder2 = $remainder % $threshold1;
//multiply the second remainder by the actual price
$new_price+= $remainder2 *$price;
//set the price using the new price
cart_item['data']->set_price($new_price);
}
}
//if quantity is less than threshold 2 and quantity is bigger than threshold1
}else if($quantity < $threshold2 && $quantity > $threshold1){
if((int)($quantity % $threshold1)==0){
//loop the amount of times threshold 1 goes into the quantity
for($i=0; $i<(int)($quantity/$threshold1);$i++){
//add to the price using threshold 1 * price with threshold 1 discount
$new_price+=$threshold1*$price*(1-$discount1);
}
//set the price for the item
cart_item['data']->set_price( $new_price );
}
//if threshold 1 doesn't go exactly into the remaining quantity then...
elseif((int)($quantity % $threshold1)!=0){
//loop the amount of times that threshold 1 goes into the quantity
for($i=0; $i<(int)($quantity/$threshold1);$i++){
//add to the price using threshold1 * price with discount
$new_price+=$threshold1*$price*(1-$discount1);
}
//find remainder from seeing the remainder of threshold1 going into the remaining quantity
$remainder2 = $remainder % $threshold1;
//multiply the second remainder by the actual price
$new_price+= $remainder2 *$price;
//set the price using the new price
cart_item['data']->set_price($new_price);
}
//if quantity is smaller than threshold2 and threshold1
}else if($quantity < $threshold2 && $quantity < $threshold1){
$new_price+= $quantity *$price;
//set the price using the new price
cart_item['data']->set_price($new_price);
}
}
}