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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.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 将基于ACF字段的折扣应用于WooCommerce中的购物车总额_Php_Wordpress_Woocommerce_Cart_Discount - Fatal编程技术网

Php 将基于ACF字段的折扣应用于WooCommerce中的购物车总额

Php 将基于ACF字段的折扣应用于WooCommerce中的购物车总额,php,wordpress,woocommerce,cart,discount,Php,Wordpress,Woocommerce,Cart,Discount,我正在尝试以编程方式为我的店铺上的订单创建折扣。我的用户有可变的折扣率,例如10%。我想在结账前对订单进行折扣,但是,商店中的某些商品不适用折扣 这些项目有一个切换设置,因此我可以检查哪些产品允许或不允许应用折扣 目前,我正在循环查看订单,以检查哪些项目适用,并使用“固定购物车”优惠券添加折扣 然而,这是可行的,在管理中优惠券适用于所有行项目,甚至是应该跳过的项目。当需要退款时,无法计算要向客户退款多少 检查购物车项目 $tradeDiscountTotal=0; foreach(WC()->c

我正在尝试以编程方式为我的店铺上的订单创建折扣。我的用户有可变的折扣率,例如10%。我想在结账前对订单进行折扣,但是,商店中的某些商品不适用折扣

这些项目有一个切换设置,因此我可以检查哪些产品允许或不允许应用折扣

目前,我正在循环查看订单,以检查哪些项目适用,并使用
“固定购物车”
优惠券添加折扣

然而,这是可行的,在管理中优惠券适用于所有行项目,甚至是应该跳过的项目。当需要退款时,无法计算要向客户退款多少

检查购物车项目

$tradeDiscountTotal=0;
foreach(WC()->cart->get_cart()作为$cart_项目){
如果(!get_字段('trade_折扣_免税',$cart_商品['product_id'])){
$tradeDiscountTotal+=$cart\u商品['line\u subtotal'];
}
}

应用此订单的总折扣

$优惠券->设置折扣类型(“固定购物车”);
$优惠券->设置金额($tradeDiscountTotal);
返回$优惠券;

如何为每个订单创建定制折扣,并确保折扣产品在管理区域中正确显示?

更新

解决方案是手动创建带有百分比折扣的优惠券,例如10%。 然后,我们根据
couponcode

  • 第一部分,确保对符合条件的产品应用0折扣
  • 第二部分是确保优惠券自动添加到购物车
  • 因此,我们得到:

    • 交易折扣免税折扣金额=0
    功能过滤器\u woo商业\u优惠券\u获取\u折扣金额($折扣,$price\u to\u折扣,$cart\u商品,$single,$优惠券){
    //购物车中的产品ID
    $product_id=$cart_项目['product_id'];
    //如果“贸易折扣免税”
    if(获取字段(‘交易折扣免税’,$product\U id)){
    $折扣=0;
    }
    返回$折扣;
    }
    添加过滤器(“woocommerce\u优惠券\u获得折扣\u金额”、“过滤器\u woocommerce\u优惠券\u获得折扣\u金额”,10,5);
    
    • 如果购物车中未
      交易折扣免税
      ,则以编程方式应用优惠券
    计算总计之前的功能操作($cart){ if(定义了('DOING'uajax')) 返回; 如果(did_action('woocommerce_before_calculate_totals')>=2) 返回; //独车 如果(!is_cart()) 返回; //优惠券代码 $toucon_code='testcoucon'; //格式 $优惠券代码=wc\U格式\U优惠券代码($优惠券代码); //迭代每个购物车项目 foreach($cart->get\u cart()作为$cart\u item\u key=>$cart\u item){ //购物车中的产品ID $product_id=$cart_项目['product_id']; //不是“贸易折扣免税” 如果(!get_字段('trade_折扣_免税',$product_id)){ //应用优惠券 $applied_优惠券=$cart->get_applied_优惠券(); //应用 $is_applied=在_数组中($优惠券代码,$applied_优惠券); //不适用 如果(!$已应用){ //申请 $cart->apply_优惠券(优惠券代码); 打破 } } } } 添加行动(“计算总计前的行动”,“计算总计前的行动”,10,1);

    可选:以下是两种其他可能的解决方案:

    1。使用
    woocommerce\u计算得出的\u总数
    过滤器挂钩,在购物车上全局应用折扣

    函数过滤器\u woocommerce\u计算的\u总计($total,$cart){
    //折扣(百分比)
    $percentage=10;//10%
    //设置变量,在循环中使用
    $new_总计=0;
    //迭代每个购物车项目
    foreach($cart->get\u cart()作为$cart\u item\u key=>$cart\u item){
    //购物车中的产品ID
    $product_id=$cart_项目['product_id'];
    //不是“贸易折扣免税”
    如果(!get_字段('trade_折扣_免税',$product_id)){
    //产品价格
    $price=$cart_item['data']->get_price();
    //哄骗新价格
    $new_price=$price*(1-($percentage/100));
    //将新价格添加到新总价中
    $new\u总计+=$new\u价格;
    }
    }
    //新总数大于
    如果($new_total>0){
    $total=$new_总计;
    }
    返回$total;
    }
    添加过滤器('woocommerce\u calculated\u total','filter\u woocommerce\u calculated\u total',10,2);
    

    2.在计算总计之前使用
    woocommerce\u操作挂钩,授予产品价格折扣

    function action_woocommerce_before_calculate_totals( $cart ) {  
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
        
        // Discount (percentage)
        $percentage = 10; // 10 %
        
        // Iterating though each cart items
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
            // Product ID in cart
            $product_id = $cart_item['product_id'];
            
            // NOT 'trade_discount_exempt'
            if( ! get_field( 'trade_discount_exempt', $product_id ) ) {
                // Product price
                $price = $cart_item['data']->get_price();
                
                // Caclulate new price
                $new_price = $price * ( 1 - ( $percentage / 100 ) ); 
    
                // Set price
                $cart_item['data']->set_price( $new_price );
            }
        }
    }
    add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
    

    “…我想在结帐前将他们的折扣应用于订单…”是否需要创建优惠券?反对在结账时直接给予折扣?当涉及到以编程方式创建优惠券时,请查看:@7uc1f3r无优惠券是不必要的。我看过WooCommerce
    add_fee
    方法,但我在他们的GitHub问题请求中看到的一切都表明,这不应该用于负数。。。折扣:(WooCommerce确实不推荐负费用,但过去经常使用,不过还有其他方法,请参见我的回答我已使用第三种解决方案(您最初寻找的解决方案)更新了我的回答(10月17日)。您将自己创建优惠券,并根据一些条件对相关产品应用此百分比折扣。优惠券将自动添加到购物车中。请阅读“谢谢,
    $cart\u item['data']->是否设置价格($new\u price);