【问题标题】:Show cash on delivery (COD) based on applied coupons in WooCommerce根据在 WooCommerce 中应用的优惠券显示货到付款 (COD)
【发布时间】:2021-12-04 20:22:12
【问题描述】:

我正在尝试仅为使用特定类型优惠券的客户启用货到付款 (COD)。

我有一个现有代码,当输入某种类型的优惠券时,它会将产品销售价格转换为常规产品价格。

现在我想补充一点,货到付款 (COD) 仅适用于同一功能中的有效优惠券。

我尝试添加的部分是这样的:

if ($coupons = WC()->cart->get_applied_coupons()  == False )
    unset( $available_gateways['cod'] );

导致:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart_object) {

    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $coupon = False;

    if ($coupons = WC()->cart->get_applied_coupons()  == False )
        unset( $available_gateways['cod'] );

    if ($coupons = WC()->cart->get_applied_coupons()  == False ) 
      $coupon = False;
    else {
        foreach ( WC()->cart->get_applied_coupons() as $code ) {
          $coupons1 = new WC_Coupon( $code );
          if ($coupons1->type == 'percent_product' || $coupons1->type == 'percent')
            $coupon = True;
        }
    }

    if ($coupon == True)
        foreach ( $cart_object->get_cart() as $cart_item ) 
        {
            $price = $cart_item['data']->regular_price;
            $cart_item['data']->set_price( $price );
        }
}

这不会给出真正的错误消息,但也肯定不是预期的结果。有什么建议吗?

【问题讨论】:

    标签: php wordpress woocommerce payment-gateway coupon


    【解决方案1】:

    首先,我重写了您现有的代码,即在应用某种类型的优惠券时将销售价格转换为正常价格的部分。这是因为您当前的代码包含过时的方法:

    function action_woocommerce_before_calculate_totals( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
        
        // Initialize
        $flag = false;
        
        // Applied coupons only
        if (  sizeof( $cart->get_applied_coupons() ) >= 1 ) {
            // Loop trough
            foreach ( $cart->get_applied_coupons() as $coupon_code ) {
                // Get an instance of the WC_Coupon Object
                $coupon = new WC_Coupon( $coupon_code );
                
                // Only for certain types, several can be added, separated by a comma
                if ( in_array( $coupon->get_discount_type(), array( 'percent', 'fixed_product', 'percent_product' ) ) ) {
                    $flag = true;
                    break;
                }
            }
        }
        
        // True
        if ( $flag ) {
            // Loop through cart items
            foreach ( $cart->get_cart() as $cart_item ) {
                // Get regular price
                $regular_price = $cart_item['data']->get_regular_price();
                
                // Set new price
                $cart_item['data']->set_price( $regular_price );
            }
        }
    }
    add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
    

    然后只激活COD,如果应用了某种类型的优惠券,您可以使用woocommerce_available_payment_gateways钩子

    默认情况下 COD 不可用:

    // Payment gateways
    function filter_woocommerce_available_payment_gateways( $payment_gateways ) {
        // Not on admin
        if ( is_admin() ) return $payment_gateways;
        
        // Initialize
        $flag = false;
        
        // WC Cart
        if ( WC()->cart ) {     
            // Get cart
            $cart = WC()->cart;
        
            // Applied coupons only
            if (  sizeof( $cart->get_applied_coupons() ) >= 1 ) {
                // Loop trough
                foreach ( $cart->get_applied_coupons() as $coupon_code ) {
                    // Get an instance of the WC_Coupon Object
                    $coupon = new WC_Coupon( $coupon_code );
                    
                    // Only for certain types, several can be added, separated by a comma
                    if ( in_array( $coupon->get_discount_type(), array( 'percent', 'fixed_product', 'percent_product' ) ) ) {
                        $flag = true;
                        break;
                    }
                }
            }
        }
        
        // NOT true, so false
        if ( ! $flag ) {
            // Cod
            if ( isset( $payment_gateways['cod'] ) ) {
                unset( $payment_gateways['cod'] );
            }  
        }
        
        return $payment_gateways;
    }
    add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );
    

    两个代码都进入活动子主题(或活动主题)的functions.php 文件中。

    在 WordPress 5.8.1 和 WooCommerce 5.8.0 中测试

    【讨论】:

    • 这段代码完全符合我的要求 :-) 非常感谢您的帮助。
    猜你喜欢
    • 2020-01-09
    • 2014-11-07
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多