[英]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 文件中。 測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.