【问题标题】:Hide WooCommerce payment methods for specific shipping zones and min subtotal隐藏特定运输区域和最小小计的 WooCommerce 付款方式
【发布时间】:2019-02-15 18:01:39
【问题描述】:

在 WooCommerce 中,当特定运输区域名称(1 区、4 区和 7 区)的购物车小计高达 250 美元时,我正在尝试删除“货到付款”付款方式。

所有其他区域不得有此限制。

这是我基于this thread的不完整代码:

add_filter( 'woocommerce_available_payment_gateways', 'change_payment_gateway', 20, 1);
function change_payment_gateway( $gateways ){
    
    $zone = $shipping_zone->get_zone_name();

    if( WC()->cart->subtotal > 250 ) && if($zone=='Zone 1','Zone 4','Zone 7'){
    
        unset( $gateways['cod'] );
    }
    return $gateways;
}

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce shipping payment-method


    【解决方案1】:

    当购物车小计达到250时,以下内容将删除“货到付款”支付网关:

    add_filter( 'woocommerce_available_payment_gateways', 'conditionally_remove_payment_methods', 20, 1);
    function conditionally_remove_payment_methods( $gateways ){
        // Not in backend (admin)
        if( is_admin() ) 
            return $gateways;
    
        // HERE below your targeted zone names
        $targeted_zones_names = array('Zone 1','Zone 4','Zone 7');
    
        $chosen_methods    = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
        $chosen_method     = explode(':', reset($chosen_methods) );
        $shipping_zone     = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[1] );
        $current_zone_name = $shipping_zone->get_zone_name();
    
        if( WC()->cart->subtotal > 250 && in_array( $current_zone_name, $targeted_zones_names ) ){
            unset( $gateways['cod'] );
        }
        return $gateways;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2018-11-09
      • 2014-08-06
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多