删除特定运输方式的“下订单”按钮,订单支付端点除外

Remove “Place order” button for specific shipping method, except on the Order-Pay endpoint

我想在选择特定运输方式时删除“下订单”按钮,使用优秀的代码 LoicTheAztec” 但不包括订单支付端点。

我尝试了以下解决方案,但它不起作用,“下订单”按钮在订单支付端点上也消失了。

add_filter('woocommerce_order_button_html', 'disable_place_order_button_html' );
function disable_place_order_button_html( $button ) {
    if ( is_checkout() && ! is_wc_endpoint_url( 'order-pay' ) ) {  
        // HERE define your targeted shipping method id
        $targeted_shipping_method = "table_rate:3";
    
        // 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;
}

感谢任何帮助。

我测试了你的代码,它对我有用。问题与这段代码无关。

支付订单 按钮的钩子是 woocommerce_pay_order_button_html 而不是 woocommerce_order_button_html。您可以在模板中找到它:/woocommerce/checkout/form-pay.php

You could remove the ! is_wc_endpoint_url( 'order-pay' ) check because it will never run on that page.

您可以检查几件事:

  • 访问该页面的用户是否登录?
  • 检查您的活动主题的 functions.php 文件是否有任何其他函数使用 woocommerce_pay_order_button_html 挂钩。
  • 您是否安装了任何管理/编辑此页面的插件(WooCommerce 除外)?
  • 最后,检查没有CSS规则隐藏元素