Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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 WooCommerce:根据单个项目数量添加折扣_Php_Wordpress_Woocommerce_Cart_Discount - Fatal编程技术网

Php WooCommerce:根据单个项目数量添加折扣

Php WooCommerce:根据单个项目数量添加折扣,php,wordpress,woocommerce,cart,discount,Php,Wordpress,Woocommerce,Cart,Discount,在我的WooCommerce网站上,我有几款价格相同的产品,80$ 我想按产品数量增加折扣 逻辑是这样的: if (Products Quantity is 2){ // the original product price change from 80$ to 75$ each. } if(Products Quantity is 3 or more){ //the original product price change from 80$ to 70$ each.

在我的WooCommerce网站上,我有几款价格相同的产品,80$
我想按产品数量增加折扣

逻辑是这样的:

if (Products Quantity is 2){
   // the original product price change from 80$ to 75$ each.
}

if(Products Quantity is 3 or more){
   //the original product price change from 80$ to 70$ each.      
}
比如说,

如果客户选择2种产品,原价将为
(80美元x2)
=>
160美元

但打折后的价格是:
(75美元x2)
=>
150美元

而且

如果访客选择3种产品,原价为
(80美元x 3)
=>
240美元

但在费用之后,它将是:
(70美元x 3)
=>
210美元

需要帮忙吗


谢谢

这个定制的钩子函数应该可以满足您的期望。您可以在其中设置基于单个项目数量的累进折扣限制

这是密码

## Tested and works on WooCommerce 2.6.x and 3.0+
add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_by_item_quantity', 10, 1 );
function progressive_discount_by_item_quantity( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
        
    # Progressive quantity until quantity 3 is reached (here)
    # After this quantity limit, the discount by item is fixed
    # No discount is applied when item quantity is equal to 1
        
    // Set HERE the progressive limit quantity discount
    $progressive_limit_qty = 3; //  <==  <==  <==  <==  <==  <==  <==  <==   <==  <==  <==

    $discount = 0;

    foreach( $cart->get_cart() as $cart_item_key => $cart_item ){

        $qty = $cart_item['quantity'];
        
        if( $qty <= $progressive_limit_qty )
            $param = $qty; // Progressive
        else
            $param = $progressive_limit_qty; // Fixed

        ## Calculation ##
        $discount -=  5 * $qty * ($param - 1); 
    }

    if( $discount < 0 )
        $cart->add_fee( __( 'Quantity discount' ), $discount); // Discount

}
已在WooCommerce 2.6.x和3.0上测试并运行+ 添加操作('woocommerce\u cart\u calculate\u fees'、'progressive\u discount\u by\u item\u quantity',10,1); 功能按商品数量累进折扣($cart){ if(定义了('DOING'uajax')) 返回; #达到数量3之前的累进数量(此处) #在此数量限制之后,按项目的折扣是固定的 #当物料数量等于1时,不应用折扣 //在此设置累进限制数量折扣 $progressive\u limit\u数量=3//