![](/img/trans.png)
[英]Disable “Place order” button for specific shipping method in WooCommerce
[英]Disable "Place order" button for specific shipping zone in WooCommerce
如果选择了特定的运输类别,我将尝试停用“下订单”按钮。 运输类别的名称是“Zone 6”。
基于“ 针对特定运输类别的“ 删除 Woocommerce”下订单按钮“回答线程,我进行了一些更改以处理运输区域:
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
设置$found
true 时,按钮会根据需要停用。 我认为get_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;
}
代码位于活动子主题(或活动主题)的functions.php
文件中。 测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.