I’m building a plugin that will hide a specific payment method based on the email address or phone number typed in the checkout form by the customer.
So at first I used this JS code to catch the onChange
of those two fields:
$(document).ready(function () {
if ($('form.woocommerce-checkout').length) {
$('#billing_phone_field, #billing_email_field').on('change', function () {
$('body').trigger('update_checkout');
});
}
});
Then, I used the hook that’s appropriate for manipulating the payment methods array, that is woocommerce_available_payment_gateways
in the following way:
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;
}
What I was hoping with this was to catch the email address and phone number in the cust_session.txt
file, to first check that they exist in the session… But they don’t as both email
and phone
array values are empty.
Am I doing something wrong? How am I supposed to get those two fields if not through the session?