0

I used this code to remove some fields if they have a coupn, and it works, but if the coupons are more than one I can't do it, for example, coupon summer and coupon winter tnx.

add_filter( 'woocommerce_checkout_fields' ,'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    global $woocommerce;
    $coupon_id = 'summer';

    if (in_array($coupon_id, $woocommerce->cart->get_applied_coupons())) {

        //unset($fields['billing']['billing_first_name']);
        //unset($fields['billing']['billing_last_name']);
        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['billing']['billing_company']);
        //unset($fields['billing']['billing_email']);
        unset($fields['order']['order_comments']);
    }

    return $fields;
}
  • You can use `array_intersect()` instead of `in_array()` BUT you indicate that your current code works, which it does, but only if the coupon code is entered on the cart page and not on the checkout page since the hook is not ajax driven. Is this correct? or does your checkout page have no option to apply/remove coupons? – 7uc1f3r Jan 31 '22 at 20:56
  • I use a snippset which works in wp, but modifying it doesn't work – eros casula Feb 01 '22 at 10:26
  • If I put the code that is there, it works (summer) but I would like to put more codes and it does not work, it does not remove the summer and winter fieldsif i put array_intersect() instead of in_array() it gives me php error in line 6 – eros casula Feb 01 '22 at 10:48
  • I'm sorry you're angry and I don't understand why, if you don't understand each other I'm sorry, yes my current code works! the coupn field is only in the cart and if I put it it removes the fields I don't want to see – eros casula Feb 01 '22 at 11:33
  • **A coupon code can be entered on the cart page, then your code will work BUT a coupon code can also be entered on the checkout page, and then your code will NOT work.** That is why I am asking for clarification on this. If that problem is/would have been solved, another problem will arise, namely that you cannot complete the order because required address fields will be missing due your code. If you can clarify this, please edit your question – 7uc1f3r Feb 01 '22 at 11:39
  • The script works, and if I put the coupon (summer) in "view cart" and continue it removes the fields that I DO NOT want to be seen and inserted. What I can not do and manage more coupons in the script, example "summer" and "winter", I would like these 2 coupons (which I created) to remove the information I do not want in checkout, if you do not have the coupons instead that information goes compiled. Example: summer coupn > remove the country field, if you do not have the coupon the country field must remain. https://filebin.net/f77pg1cqcc3wl82x – eros casula Feb 01 '22 at 11:50

0 Answers0