当用户在 WooCommerce wordpress 主题中选择 paypal 付款方式时,我试图禁用结帐页面中的账单字段。
一旦用户完成项目选择并进入结帐页面,如果用户选择 Paypal 付款,则结帐表单不应验证帐单字段,如果选择任何其他付款方式,则它应该正常工作。
我搜索了很多,但找不到线索。请有人帮助我。
提前致谢。
最佳答案
/* if you want the title of the payment method */
add_action( 'woocommerce_review_order_before_payment', 'ts_refresh_payment_method' );
function ts_refresh_payment_method(){
$chosen_payment_method = WC()->session->get('chosen_payment_method'); //Get the selected payment method
// jQuery
?>
<script type="text/javascript">
(function($){
var cpm = '<?php echo $chosen_payment_method; ?>';
if(cpm == "paypal"){
$(".woocommerce-billing-fields").hide();
}else{
$(".woocommerce-billing-fields").show();
}
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
//alert(this.value);
if(this.value == "paypal"){
$(".woocommerce-billing-fields").hide();
}else{
$(".woocommerce-billing-fields").show();
}
$('body').trigger('update_checkout');
});
alert($('body').trigger('update_checkout'));
})(jQuery);
</script>
<?php
}
/* */
add_filter('woocommerce_billing_fields','wpb_custom_billing_fields');
function wpb_custom_billing_fields( $fields = array() ) {
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if($chosen_payment_method == "paypal"){
$fields['billing_first_name']['required']= false;
$fields['billing_last_name']['required']= false;
$fields['billing_company']['required'] = false;
$fields['billing_email']['required'] = false;
$fields['billing_address_1']['required']= false;
$fields['billing_address_2']['required']= false;
$fields['billing_state']['required'] = false;
$fields['billing_city']['required'] = false;
$fields['billing_phone']['required'] = false;
$fields['billing_postcode']['required'] = false;
$fields['billing_country']['required'] = false;
}
return $fields;
}
关于wordpress - 当用户选择 Paypal 付款方式时,在结帐表单中禁用 WooCommerce 中的账单地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22304098/