禁用“下订单” WooCommerce中特定运输区域的按钮 [英] Disable "Place order" button for specific shipping zone in WooCommerce

查看:143
本文介绍了禁用“下订单” WooCommerce中特定运输区域的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果选择了特定的运输类别,我试图停用下订单按钮。装运类别的名称为 6区。

I am trying to deactivate the "Place order" button if a specific shipping class is selected. Name of the shipping class is "Zone 6".

基于 删除特定运输类别的Woocommerce下订单按钮 答案线程,我进行一些更改以处理运输区域:

Based on "Remove Woocommerce "place order" button for a specific shipping class" answer thread, I have make some changes to handle shipping zones instead:

add_filter('woocommerce_order_button_html', 'remove_order_button_html' );
function remove_order_button_html( $button ) {
    // HERE define your targeted shipping zone
    $targeted_shipping_zone = "Zone 6";
    $found = false;

    // Loop through cart items
    foreach( WC_Shipping_Zones::get_zones() as $shipping_zone ) {
        if( $shipping_zone['data']->get_zone_name() == $targeted_shipping_zone ) {
            $found = true; // The targeted shipping class is found
            break; // We stop the loop
        }
    }

    // If found we remove the button
    if( $found ) {
        $style = 'style="background:Silver !important; color:white !important; cursor: not-allowed !important;"';
        $button_text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
        $button = '<a class="button" '.$style.'>' . $button_text . '</a>';
    }
    return $button;
}

但是它不起作用。手动将 $ found 设置为true时,该按钮将根据需要停用。我认为 get_zone()行中有一个错误。

But it does not work. When setting $found to true manually the button is deactivated as wanted. I think there is a mistake in the get_zone() lines.

感谢您的帮助。

推荐答案

在您的代码中,您没有获得所选的运输区域,只是在每个现有的运输区域中循环,因此您总是在获得最后一个。这就是为什么它不起作用。

In your code, you are not getting the selected shipping zone, you are just looping through each existing shipping zone, so you are always getting the last one. That's why it doesn't work.

在下面,您将获得正确的所选运输区域名称:

In the following you will get the correct chosen shipping zone name:

add_filter('woocommerce_order_button_html', 'disable_place_order_button_html' );
function disable_place_order_button_html( $button ) {
    // HERE define your targeted shipping zone
    $targeted_zone_name = "Zone 6";

    // Get the chosen shipping method (if it exist)
    if( $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ) ){
        $chosen_shipping_method = reset($chosen_shipping_methods);
        $chosen_shipping_method = explode(':', $chosen_shipping_method );
        $chosen_shipping_zone   = WC_Shipping_Zones::get_zone_by( 'instance_id', end($chosen_shipping_method) );

        // If the targeted shipping zone is found, disable the button
        if( $targeted_zone_name == $chosen_shipping_zone->get_zone_name() ) {
            $style = 'style="background:Silver !important; color:white !important; cursor: not-allowed !important; text-align:center;"';
            $button_text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
            $button = '<a class="button" '.$style.'>' . $button_text . '</a>';
        }
    }
    return $button;
}

代码放入 functions.php 活动子主题(或活动主题)的文件。经过测试,可以正常工作。

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

这篇关于禁用“下订单” WooCommerce中特定运输区域的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆