php - How to conditionally hide payment method based on shipping country in WooCommerce block-based checkout? - Stack Overflow

I’m using the latest version of WooCommerce with the block-based checkout (i.e., WooCommerce Blocks), a

I’m using the latest version of WooCommerce with the block-based checkout (i.e., WooCommerce Blocks), and I need to hide the default “Cash on Delivery” (COD) payment method unless the shipping country is German (DE).

In the classic checkout flow, this could easily be achieved using the woocommerce_available_payment_gateways filter:

add_filter('woocommerce_available_payment_gateways', 'custom_restrict_cod_for_slovenia');
function custom_restrict_cod_for_slovenia($gateways) {
    if (is_admin()) return $gateways;

    if (isset($gateways['cod'])) {
        $shipping_country = WC()->customer->get_shipping_country();
        if ($shipping_country !== 'DE') {
            unset($gateways['cod']);
        }
    }
    return $gateways;
}

This works perfectly in the classic checkout, but it does NOT work in the block-based checkout – the COD method still appears even if the shipping country is not Slovenia, and it doesn’t update dynamically when the user changes the country.

I’ve also tried hooking into woocommerce_store_api_payment_methods like this:

add_filter('woocommerce_store_api_payment_methods', 'custom_cod_for_si_only', 10, 2);
function custom_cod_for_si_only($gateways, $request) {
    if (isset($gateways['cod'])) {
        if (
            isset($request['shipping_address']['country']) &&
            $request['shipping_address']['country'] !== 'DE'
        ) {
            unset($gateways['cod']);
        }
    }
    return $gateways;
}

But even this doesn’t work as expected. The COD payment method still shows up regardless of the selected country, and doesn’t hide dynamically when switching the country at checkout.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744270151a4566060.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信