WooCommerce在钩子'woocommerce_checkout_process'内获得支付网关



我试图在用户完成结账之前获得当前选择的支付网关。我这样做是因为我在用户选择' backs '支付方式时添加了一些自定义字段。但是目前,我的函数返回结账过程开始时选择的支付方式,而不是当前选择的支付方式。这是我正在使用的代码。

/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
$selected_payment_method_id = WC()->session->get( 'chosen_payment_method' ); // this code returns cod even when bacs is selected because cod was selected when the checkout process started
if($selected_payment_method_id == 'bacs'){
// Check if set, if its not set add an error.
if ( ! $_POST['extra_info_company'] || ! $_POST['extra_info_ordernumber'] || ! $_POST['extra_info_first_name'] || ! $_POST['extra_info_last_name'] || ! $_POST['extra_info_phonenumber'] || ! $_POST['extra_info_email'] )
wc_add_notice( __( 'Vul alle extra velden in.' ), 'error' );
}
}
有人能帮我一下吗?

这是给其他发现这个的人的。遗憾的是,这个函数的方式不允许您获得当前的付款方式,我修复它的方式是通过添加一个隐藏字段,使用jQuery更新所选的付款方式。

您完全可以从woocommerce_checkout_process钩子中获得付款方式。你只需要像这样从$_POST中截取[payment_method]:

add_action('woocommerce_checkout_process', 'blacklist_woocommerce_checkout_process');
function blacklist_woocommerce_checkout_process() {
echo '<pre>'; print_r( $_POST ); echo '</pre>'; // view in the chrome DevTools console
$payment_method = $_POST['payment_method'];
if ( $payment_method == 'authorize_net_cim_credit_card' ) { // hard code for now
// I do some stuff like throw an error if I want
}
}

这是woocommerce_checkout_process钩子中的$_POST输出(当然是编辑过的):

(
[billing_first_name] => First
[billing_last_name] => Last
[billing_company] => Company
[billing_country] => US
[billing_address_1] => Street
[billing_address_2] => 
[billing_city] => City
[billing_state] => ST
[billing_postcode] => 00000
[billing_phone] => (000) 000-0000
[billing_email] => email@gmail.com
[shipping_first_name] => First
[shipping_last_name] => Last
[shipping_company] => Company
[shipping_country] => US
[shipping_address_1] => Street
[shipping_address_2] => 
[shipping_city] => City
[shipping_state] => ST
[shipping_postcode] => 00000
[order_comments] => 
[shipping_method] => Array
(
[0] => flat_rate:1
)
[payment_method] => cod
[wc-authorize-net-cim-credit-card-expiry] => 01 / 24
[wc-authorize-net-cim-credit-card-payment-nonce] => 
[wc-authorize-net-cim-credit-card-payment-descriptor] => 
[wc-authorize-net-cim-credit-card-last-four] => 
[wc-authorize-net-cim-credit-card-card-type] => 
[woocommerce-process-checkout-nonce] => 2f8875d74e
[_wp_http_referer] => /?wc-ajax=update_order_review
)

最新更新