当购物车小计达到Woocommerce中的特定金额时,禁用特定送货区域的特定付款方式

问题描述 投票:1回答:1

在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个回答
0
投票

以下将删除特定运输区域的“货到付款”支付网关,并且当购物车小计达到250时:

add_filter( 'woocommerce_available_payment_gateways', 'conditionally_remove_payment_methods', 20, 1);
function conditionally_remove_payment_methods( $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;
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

© www.soinside.com 2019 - 2024. All rights reserved.