繁体   English   中英

在 Woocommerce 中隐藏特定付款方式的下订单按钮

[英]Hide Place Order button for specific payment methods in Woocommerce

如何仅针对“支票”网关禁用“下订单”按钮。 我不希望我的用户为此网关下订单,因为他们需要在进行任何付款之前通过给定的信息进行联系。

我找到了针对特定运输类别的 Remove Woocommerce“下订单”按钮,这正是我想要做的,但用于“支票”付款方式。

我试图用“支票”ID 替换 ID 332,但它完全删除了所有网关的按钮。 它在后端的 ID 是cheque ,结帐页面上的 ID 和类是payment_method_cheque

add_filter('woocommerce_order_button_html', 'remove_order_button_html' );
function remove_order_button_html( $button ) {
    // HERE define your targeted shipping class
    $targeted_payment_method = 'payment_method_cheque';
    $found = false;

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if( $cart_item['data']->get_shipping_class_id() == $targeted_shipping_class ) {
            $found = true; // The targeted shipping class is found
            break; // We stop the loop
        }
    }

    // If found we remove the button
    if( $found )
        $button = '';

    return $button;
}

但它不起作用。 有什么建议吗?

更新:它比你想象的要简单一些,但需要一些 jQuery 来刷新结帐……请尝试以下操作:

add_filter('woocommerce_order_button_html', 'remove_place_order_button_for_specific_payments' );
function remove_place_order_button_for_specific_payments( $button ) {
    // HERE define your targeted payment(s) method(s) in the array
    $targeted_payments_methods = array('cheque');
    $chosen_payment_method     = WC()->session->get('chosen_payment_method'); // The chosen payment

    // For matched payment(s) method(s), we remove place order button (on checkout page)
    if( in_array( $chosen_payment_method, $targeted_payments_methods ) && ! is_wc_endpoint_url() ) {
        $button = ''; 
    }
    return $button;
}

// jQuery - Update checkout on payment method change
add_action( 'wp_footer', 'custom_checkout_jquery_script' );
function custom_checkout_jquery_script() {
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('form.checkout').on('change', 'input[name="payment_method"]', function(){
            $(document.body).trigger('update_checkout');
        });
    });
    </script>
    <?php
    endif;
}

代码位于活动子主题(或活动主题)的 functions.php 文件中。 测试和工作。

相关: 根据 Woocommerce 选择的付款方式更改结帐时的付款按钮

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM