基于Woocommerce支付网关的结账字段

时间:2015-07-08 12:10:28

标签: php wordpress woocommerce

我希望根据所选的付款网关删除结帐字段。

我在function.php中添加了以下代码,但它无效。我面临的问题是,它正处于If状态,但如果条件未设置则不在内部。当我试图在条件内回应时,它会显示在浏览器中。

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {

    if($_POST['payment_method'] === "wcwcPDpg"){

       echo "this";
       unset($fields['billing']['billing_first_name']);
       unset($fields['billing']['billing_last_name']);
       unset($fields['billing']['billing_company']);
       unset($fields['billing']['billing_address_1']);
       unset($fields['billing']['billing_address_2']);
       unset($fields['billing']['billing_city']);
       unset($fields['billing']['billing_postcode']);
       unset($fields['billing']['billing_country']);
       unset($fields['billing']['billing_state']);
       unset($fields['billing']['billing_phone']);
       unset($fields['order']['order_comments']);
       unset($fields['billing']['billing_email']);
       unset($fields['account']['account_username']);
       unset($fields['account']['account_password']);
       unset($fields['account']['account_password-2']);
    }


   return $fields;
}

当我删除if条件时,字段未设置,但是,我只想为特定的支付网关取消设置。

任何帮助表示赞赏:)

1 个答案:

答案 0 :(得分:0)

尝试

if (WC()->session->chosen_payment_method == "wcwcPDpg") {

而不是

if($_POST['payment_method'] === "wcwcPDpg"){

然后将其添加到您的子主题的functions.php中,以便每次切换支付网关时都应用您的过滤器:

function filter_woocommerce_update_order_review_fragments( $array ) { 
   ob_start();
   wc_get_template( 'checkout/form-billing.php', array( 'checkout' => WC()->checkout() ) );

   $array[".woocommerce-billing-fields"] = ob_get_clean();
   return $array; 
}; 

add_filter( 'woocommerce_update_order_review_fragments', 'filter_woocommerce_update_order_review_fragments', 10, 1 )

我怀疑有更好的方法可以做到这一点,但这是我的第一次尝试。如果您有更好的解决方案,请在此处发布更新。

相关问题