Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 电子商务中的包装定价_Php_Wordpress_Woocommerce_Product Quantity - Fatal编程技术网

Php 电子商务中的包装定价

Php 电子商务中的包装定价,php,wordpress,woocommerce,product-quantity,Php,Wordpress,Woocommerce,Product Quantity,我基本上有3个产品价格。单品-全价,6包-5折,24包10折。但是,例如,如果客户购买25件,则会购买一包24件,其中包括10%折扣+1件单件全价商品,而对于14件,则会以5%折扣和2件单件全价商品的价格购买2-6包。我一直在想办法,谁能告诉我哪里出了问题,或者解释一下我应该使用的逻辑?我会非常感激的 更新了我现在已经改变了方法,但还没有完全改变。有什么建议吗 add_action( 'woocommerce_before_calculate_totals', 'quantity_based_p

我基本上有3个产品价格。单品-全价,6包-5折,24包10折。但是,例如,如果客户购买25件,则会购买一包24件,其中包括10%折扣+1件单件全价商品,而对于14件,则会以5%折扣和2件单件全价商品的价格购买2-6包。我一直在想办法,谁能告诉我哪里出了问题,或者解释一下我应该使用的逻辑?我会非常感激的

更新了我现在已经改变了方法,但还没有完全改变。有什么建议吗

add_action( 'woocommerce_before_calculate_totals', 'quantity_based_pricing', 9999 );

function quantity_based_pricing( $cart ) {

    // 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%
    $threshold3 = 1;
    $discount3 = 0;
    $new_price=0;

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
      $product_id = $cart_item['product_id'];
      $quantity = $cart_item['quantity'];
    $price = $cart_item['data']->get_price();

    if((int)($quantity/$threshold2)==0){
       for($i=0; $i<(int)($quantity/$threshold2);$i++){
                $new_price+=$threshold2*$price*(1-$discount2);
            }
                cart_item['data']->set_price( $new_price );
    }
    else{
          if((int)($quantity/$threshold2)!=0){

              for($i=0; $i<(int)($quantity/$threshold2);$i++){
                $new_price+=$threshold2*$price*(1-$discount2);
            }

             $remainder = $quantity % $threshold2;

              if((int)($remainder/$threshold1)==0){
                  for($i=0; $i<(int)($remainder/$threshold2);$i++){
                $new_price+=$threshold1*$price*(1-$discount1);
            }
                cart_item['data']->set_price( $new_price ); 
              }
              elseif((int)($remainder/$threshold1)!=0){
                   for($i=0; $i<(int)($remainder/$threshold2);$i++){
                $new_price+=$threshold1*$price*(1-$discount1);
            }
                $remainder2 = $remainder % $threshold1;
                $new_price+= $remainder2 *$price;
                cart_item['data']->set_price($new_price);
              }


        }

         }

    }
}
add_action('woocommerce_-before_-calculate_-total','quantity_-based_-pricing',9999);
基于功能数量的定价($cart){
//定义折扣规则和阈值
$threshold1=6;//如果项目=6,则更改价格
$折扣1=0.05;//单价降低5%
$threshold2=24;//如果项目=24,则更改价格
$折扣2=0.1;//单价降低10%
$threshold3=1;
$折扣3=0;
$new_价格=0;
foreach($cart->get\u cart()作为$cart\u item\u key=>$cart\u item){
$product_id=$cart_项目['product_id'];
$quantity=$cart_项目['quantity'];
$price=$cart_item['data']->get_price();
如果((int)($quantity/$threshold2)==0){
对于($i=0;$iset_价格($new_价格);
}
否则{
如果((int)($quantity/$threshold2)!=0){

对于($i=0;$i您需要从$cart\u项目['quantity']获取数量。

这是一个解决方案:

<?php
$price=100; //per item. Test values
$cart_item_quatity=25;  //Test values
$new_price=0;
$thresh1=5;
$thresh2=24;

$off1=0.05;
$off2=0.1;

if((int)($cart_item_quatity/$thresh2)!=0){
    // Add the full price for items left out or over the second threshold
    $new_price+=($cart_item_quatity%$thresh2)*$price;

    for($i=0;$i<(int)($cart_item_quatity/$thresh2);$i++){
        $new_price+=$thresh2*$price*(1-$off2);
    }
}elseif((int)($cart_item_quatity/$thresh1)!=0){
    // Add the full price for items left out or over the second threshold
    $new_price+=($cart_item_quatity%$thresh1)*$price;

    for($i=0;$i<(int)($cart_item_quatity/$thresh1);$i++){
        $new_price+=$thresh1*$price*(1-$off1);
    }
}else{
    $new_price+=$cart_item_quatity*$price;
}

你取价格,然后选择它所属的类别。你先除以24,然后再除以6。如果是24的倍数,你将“特殊”价格应用于24包的数量(使用商),其余部分应用完整价格。与6相同。