隐藏 Woocommerce 中特定付款方式的下订单按钮
Hide Place Order button for specific payment methods in Woocommerce
如何仅对“支票”网关禁用“下订单”按钮。我不希望我的用户为此网关下订单,因为他们需要在付款前通过给定的信息进行联系。
我发现 这正是我想要做的,但我想用“支票”付款方式代替。
我尝试用“支票”ID 替换 ID 332,但它完全删除了所有网关的按钮。它在后端的 ID 是 cheque
,结帐页面上的 ID 和 class 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 文件。已测试并有效。
相关:
如何仅对“支票”网关禁用“下订单”按钮。我不希望我的用户为此网关下订单,因为他们需要在付款前通过给定的信息进行联系。
我发现
我尝试用“支票”ID 替换 ID 332,但它完全删除了所有网关的按钮。它在后端的 ID 是 cheque
,结帐页面上的 ID 和 class 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 文件。已测试并有效。
相关: