我正在构建一个插件,该插件将根据客户在结账表单中输入的电子邮件地址或电话号码隐藏特定的付款方式。
所以一开始我用这个JS代码来捕获这两个字段的
onChange
:
$(document).ready(function () {
if ($('form.woocommerce-checkout').length) {
$('#billing_phone_field, #billing_email_field').on('change', function () {
$('body').trigger('update_checkout');
});
}
});
然后,我使用了适合操作支付方式数组的钩子,即
woocommerce_available_payment_gateways
,如下所示:
public function update_payment_methods_based_on_user_input( $available_gateways ) {
if ( is_admin() ) return $available_gateways;
if ( ! is_checkout() ) return $available_gateways;
$wp_session = isset( WC()->session ) && WC()->session->has_session() ? WC()->session : null;
if ( is_a( $wp_session, 'WC_Session_Handler' ) ) {
$cust_session = $wp_session->get( 'customer' );
file_put_contents( ABSPATH . '/wp-content/cust_session.txt', print_r( $cust_session, true ) );
}
return $available_gateways;
}
我希望这样做是捕获
cust_session.txt
文件中的电子邮件地址和电话号码,首先检查它们是否存在于会话中......但它们不作为 email
和 phone
数组值是空的。
我做错了什么吗?如果不通过会话我该如何获取这两个字段?
请注意,此类自定义仅适用于旧版经典结帐,但不适用于新结帐块。
要显示/隐藏基于帐单电子邮件和/或电话的结账付款方式需要 Ajax。
以下示例将显示“bacs”付款选项(银行电汇),前提是结帐帐单电子邮件或帐单电话与预定义的相匹配:
// Embeded Javascript code (in checkout page only)
add_action('woocommerce_checkout_init', 'enqueue_checkout_custom_js_script');
function enqueue_checkout_custom_js_script() {
wc_enqueue_js( " var email = $('[name=billing_email]').val(),
phone = $('[name=billing_phone]').val();
$('form.checkout').on('change', '[name=billing_email],[name=billing_phone]', function() {
const field = $(this).attr('name');
if (field === 'billing_email') email = $(this).val();
if (field === 'billing_phone') phone = $(this).val();
if ( email.length > 0 && phone.length > 0 ) {
$.ajax({
type: 'POST',
url: '" . admin_url('/admin-ajax.php') . "',
data: {
'action' : 'checkout_billing_email_and_phone',
'email': field === 'billing_email' ? $(this).val() : email,
'phone': field === 'billing_phone' ? $(this).val() : phone,
},
success: function (response) {
console.log(response);
$(document.body).trigger('update_checkout');
}
});
}
});" );
}
// PHP AJAX Receiver: Process ajax request
add_action('wp_ajax_checkout_billing_email_and_phone', 'update_session_billing_email_and_phone');
add_action('wp_ajax_nopriv_checkout_billing_email_and_phone', 'update_session_billing_email_and_phone');
function update_session_billing_email_and_phone() {
if ( isset($_POST['email']) && isset($_POST['phone']) ) {
$email = sanitize_email($_POST['email']);
$phone = filter_var($_POST['phone'], FILTER_SANITIZE_NUMBER_INT);
WC()->customer->set_billing_email( $email );
WC()->customer->set_billing_phone( $phone );
WC()->customer->apply_changes();
wp_die('Ajax: email or phone set');
}
wp_die();
}
// Filter available payment options dynamically in checkout page
add_filter('woocommerce_available_payment_gateways', 'filter_available_payment_gateways');
function filter_available_payment_gateways( $available_gateways ) {
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
return $available_gateways;
// Below define in the corresponding arrays the allowed email and phones
$allowed_phones = array('0145878545',' 0145878548', '0145874586');
$allowed_emails = array('[email protected]',' [email protected]', '[email protected]');
if ( ! ( in_array( WC()->customer->get_billing_phone(), $allowed_phones )
|| in_array( WC()->customer->get_billing_email(), $allowed_emails ) ) ) {
unset($available_gateways['bacs']);
}
return $available_gateways;
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。