繁体   English   中英

隐藏特定运输区域和最小小计的 WooCommerce 付款方式

[英]Hide WooCommerce payment methods for specific shipping zones and min subtotal

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

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

这是我基于此线程的不完整代码:

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;
}

任何帮助表示赞赏。

以下将删除特定运输区域的“货到付款”支付网关,并且当购物车小计达到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 文件中。 测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM