php - 禁用 WooCommerce 中特定运输方式的 “Place order” 按钮

标签 php wordpress woocommerce checkout shipping-method

我想在选择特定送货方式时禁用“下订单”按钮,有点像我之前问题的答案“Disable "Place order" button for specific shipping zone in WooCommerce”,但针对的是特定送货方式而不是运输区域。

相关运输方式的名称是“LATAM”。

感谢任何帮助。

最佳答案

首先,您需要检查送货方式单选按钮,找出与“LATAM”对应的的送货方式 ID...

enter image description here

要使其适用于特定的运输方式 ID,您将使用以下内容:

add_filter('woocommerce_order_button_html', 'disable_place_order_button_html' );
function disable_place_order_button_html( $button ) {
    // HERE define your targeted shipping method id
    $targeted_shipping_method = "flat_rate:14";

    // Get the chosen shipping method (if it exist)
    $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
    
    // If the targeted shipping method is selected, we disable the button
    if( in_array( $targeted_shipping_method, $chosen_shipping_methods ) ) {
        $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 文件中。已测试且有效。

关于php - 禁用 WooCommerce 中特定运输方式的 “Place order” 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57324907/

相关文章:

php - 在 mysql 中存储回历日期的最佳方式?

php - 如何在自定义登录表单中检索 wp 错误

php - 非上一个时显示下一个帖子

php - WordPress 如何仅向订阅者授予对页面的访问权限

php - 克服 PHP 中的针状大海捞针困惑

php - 下拉按钮的CSS

javascript - Stripe 动态支付

css - 响应式设计不会响应480以下

php - 将自定义总计行添加到 WooCommerce 管理订单总计

ionic-framework - Access-Control-Allow-Headers 不允许请求 header 字段 User-Agent