从结帐页面中删除用户名和帐户密码字段

时间:2019-07-10 作者:sanjay yadav

我正在尝试从签出页中删除帐户用户名和帐户用户密码。我正在使用这个代码,这是完美的工作。但我需要将其用于某些特定类别。

  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;
      }
如何将此代码用于类别。

1 个回复
SO网友:sanjay yadav

这是我的答案,它工作得很好。

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;
}