woocommerce offtopic - Remove username and account password field from checkout page

admin2025-01-07  3

I ma trying to remove account user name and Account user password from checkout page. I am using this code and this is working perfect.But I need use this with some particular categories .

  add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    function custom_override_checkout_fields( $fields) {
         unset($fields['account']['account_password']);
         unset($fields['account']['account_password-2']);
         unset($fields['account']['account_username']);
         return $fields;
      }

How can use this code with categories .

I ma trying to remove account user name and Account user password from checkout page. I am using this code and this is working perfect.But I need use this with some particular categories .

  add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    function custom_override_checkout_fields( $fields) {
         unset($fields['account']['account_password']);
         unset($fields['account']['account_password-2']);
         unset($fields['account']['account_username']);
         return $fields;
      }

How can use this code with categories .

Share Improve this question asked Jul 10, 2019 at 10:46 sanjay yadavsanjay yadav 11 bronze badge 1
  • if( is_page( $page_id ) ) { // do your stuff here } – Max Yudin Commented Jul 10, 2019 at 10:53
Add a comment  | 

1 Answer 1

Reset to default 0

Here is my answer and it's working perfectly.

function conditionally_remove_checkout_fields( $fields ) {

    // HERE the defined product Categories
    $categories = array('age-defying-skincare','az-brand-accessories','az-healthy-supplements','az-other-natural-products','Not available   mwb_wgm_giftcard','little-geniuses-children');

    $found = false;

    // CHECK CART ITEMS: search for items from our defined product category
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $found = true;
            break;
        }
    }

    // If a special category is in the cart, remove some shipping fields
    if ( $found ) {

        // hide the billing fields
     unset($fields['account']['account_password']);
     unset($fields['account']['account_password-2']);
     unset($fields['account']['account_username']);
     return $fields;

        // hide the additional information section
        add_filter('woocommerce_enable_order_notes_field', '__return_false');
        add_filter( 'woocommerce_ship_to_different_address_checked', '__return_false' );
    }
    return $fields;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257574a449.html

最新回复(0)