首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于ACF字段的折扣在WooCommerce中的应用

基于ACF字段的折扣在WooCommerce中的应用
EN

Stack Overflow用户
提问于 2020-10-16 09:54:29
回答 1查看 788关注 0票数 1

我正试图通过编程为我的商店的订单创造一个折扣。我的用户有一个可变的贴现率,例如10%。我想在结账前把他们的折扣应用到订单上,但是,商店里的一些商品不适用于折扣。

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

目前,我正在循环的订单,以检查哪些项目是适用的,并使用'fixed_cart'优惠券,以增加折扣。

这是可行的,然而,在管理优惠券适用于所有的行项目,甚至应该跳过的项目。当需要退款时,不可能把多少钱退还给客户。

检查购物车项目

代码语言:javascript
复制
$tradeDiscountTotal = 0;
foreach( WC()->cart->get_cart() as $cart_item ) {
    if(!get_field('trade_discount_exempt', $cart_item['product_id'])) {
        $tradeDiscountTotal += $cart_item['line_subtotal'];
    }
}

应用此订单的总折扣

代码语言:javascript
复制
$coupon->set_discount_type('fixed_cart');
$coupon->set_amount($tradeDiscountTotal);
return $coupon;

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

EN

回答 1

Stack Overflow用户

发布于 2020-10-16 11:47:29

更新

解决方案是手动创建优惠券,其中包括10%的百分比折扣。然后在couponcode的基础上添加以下代码

确保满足condition

  • Second部件的产品的折扣为0,以确保优惠券自动添加到购物车

所以我们得到:

  • 贴现率trade_discount_exempt = 0

代码语言:javascript
复制
function filter_woocommerce_coupon_get_discount_amount( $discount, $price_to_discount , $cart_item, $single, $coupon ) {
    // Product ID in cart
    $product_id = $cart_item['product_id'];
    
    // If 'trade_discount_exempt'
    if ( get_field( 'trade_discount_exempt', $product_id ) ) {
        $discount = 0;
    }

    return $discount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );

  • 以编程方式在cart

中应用优惠券(如果不是trade_discount_exempt )

代码语言:javascript
复制
function action_woocommerce_before_calculate_totals( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

   if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Only cart
    if( ! is_cart() )
        return;

    // Coupon code
    $coupon_code = 'testcoupon';
    
    // Format
    $coupon_code = wc_format_coupon_code( $coupon_code );
    
    // 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 ) ) {
            // Applied coupons
            $applied_coupons = $cart->get_applied_coupons();

            // Is applied
            $is_applied = in_array( $coupon_code, $applied_coupons );

            // NOT applied
            if ( ! $is_applied ) {
                // Apply
                $cart->apply_coupon( $coupon_code );
                break;
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );

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

1.使用woocommerce_calculated_total过滤器钩子,在cart上应用全局折扣。

代码语言:javascript
复制
function filter_woocommerce_calculated_total( $total, $cart ) {
    // Discount (percentage)
    $percentage = 10; // 10 %
    
    // Set variable, used in loop
    $new_total = 0;
    
    // 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 ) ); 

            // Add new price to new total
            $new_total += $new_price;
        }
    }
    
    // New total greater than
    if ( $new_total > 0 ) {
        $total = $new_total;
    }

    return $total;
}
add_filter( 'woocommerce_calculated_total', 'filter_woocommerce_calculated_total', 10, 2 );

2.使用woocommerce_before_calculate_totals动作钩子,对产品价格给予折扣。

代码语言:javascript
复制
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 );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64386896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档