Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 禁用";下单“;WooCommerce中特定装运区的按钮_Php_Wordpress_Woocommerce_Checkout_Shipping Method - Fatal编程技术网

Php 禁用";下单“;WooCommerce中特定装运区的按钮

Php 禁用";下单“;WooCommerce中特定装运区的按钮,php,wordpress,woocommerce,checkout,shipping-method,Php,Wordpress,Woocommerce,Checkout,Shipping Method,如果选择了特定的装运类别,我将尝试停用“下订单”按钮。装运类别的名称为“6区” 基于“”回答线程,我对处理装运区做了一些更改: add_filter('woocommerce_order_button_html', 'remove_order_button_html' ); function remove_order_button_html( $button ) { // HERE define your targeted shipping zone $targeted_ship

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

基于“”回答线程,我对处理装运区做了一些更改:

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;
}
add_filter('woocommerce_order_button_html','remove_order_button_html');
函数remove\u order\u button\u html($button){
//这里定义您的目标配送区域
$targeted_shipping_zone=“zone 6”;
$found=false;
//循环浏览购物车项目
foreach(WC_Shipping_zone::get_Zones()作为$Shipping_zone){
如果($shipping_zone['data']->get_zone_name()==$targeted_shipping_zone){
$found=true;//找到了目标装运类
break;//我们停止循环
}
}
//如果找到了,我们就把按钮取下来
如有($已找到){
$style='style=“背景:银色!重要;颜色:白色!重要;光标:不允许!重要;”;
$button_text=apply_过滤器('woocommerce_order_button_text'、'placeorder'、'woocommerce');
$button='.$button_text'.';
}
返回$按钮;
}
但它不起作用。手动将
$found
设置为true时,按钮会根据需要停用。我认为
get\u zone()
行中有一个错误


非常感谢您的帮助。

在您的代码中,您没有得到所选的配送区域,您只是在每个现有配送区域中循环,因此您总是得到最后一个配送区域。这就是它不起作用的原因

在以下内容中,您将获得正确选择的装运区名称:

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)
    $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;"';
        $text   = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
        $button = '<a class="button" '.$style.'>' . $text . '</a>';
    }
    return $button;
}
add_filter('woocommerce_order_button_html','disable_place_order_button_html');
功能禁用\u下订单\u按钮\u html($button){
//这里定义您的目标配送区域
$targeted_zone_name=“zone 6”;
//获取所选的装运方式(如果存在)
$selected_shipping_methods=WC()->session->get('selected_shipping_methods');
$selected_shipping_method=重置($selected_shipping_methods);
$selected_shipping_method=分解(“:”,$selected_shipping_method);
$selected_shipping_zone=WC_shipping_Zones::get_zone_by('instance_id',end($selected_shipping_method));
//如果找到目标装运区,请禁用该按钮
如果($targeted_zone_name==$selected_shipping_zone->get_zone_name()){
$style='style=“背景:银色!重要;颜色:白色!重要;光标:不允许!重要;文本对齐:居中;”;
$text=apply_过滤器('woocommerce_order_button_text',uu('placeorder','woocommerce');
$button='.$text';
}
返回$按钮;
}

代码进入活动子主题(或活动主题)的functions.php文件。已测试并运行。

我刚刚测试了您的代码。问题在于foreach循环。我会添加我的答案,如果我能解决它。航运类或航运区,你能说清楚吗?如果我想绑定按钮的停用到一个特定的航运方法?如果你能再帮我一次就太好了:)谢谢你的建议。他这样做了,并提出了一个新问题。