首页 > 解决方案 > 删除结帐页面上特定类别的帐户用户名和密码

问题描述

希望每一个人都做得很好。我正在尝试从结帐页面中删除帐户密码和帐户用户字段。这段代码运行完美。

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

但问题是,我只为某些类别删除了这个。所以我试图按类别删除。所以我的代码有些人这样认为,但它不起作用。

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
     global $product;
    $terms = get_the_terms( $product->ID, 'product_cat' );
     foreach ($terms as $term) {
        $product_cat = $term->slug;
    } 
    if ($product_cat='age-defying-skincare') {
    unset($fields['account']['account_password']);
     unset($fields['account']['account_password-2']);
     unset($fields['account']['account_username']);
     return $fields;

    }

}

标签: wordpresshook-woocommerce

解决方案


您正在使用动作挂钩。它不能更新或删除该字段。

do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated ); 

如果要在用户字段中进行更改。

woocommerce_new_customer_data这是一个过滤钩子。

然后试试这个钩子:

      $new_customer_data = apply_filters( 'woocommerce_new_customer_data', array( 
          'user_login' => $username,  
          'user_pass' => $password,  
          'user_email' => $email,  
          'role' => 'customer',  
 ) ); 

您将在此处获得有关此内容的更多详细信息。 http://hookr.io/plugins/woocommerce/3.0.6/files/includes-wc-user-functions/

更新: 对于 WooCommerc 结帐字段

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

    $cart_items = WC()->cart->get_cart();

    // Categories Array
    $categories = array('age-defying-skincare');

    foreach( $cart_items as $cart_item ){
        if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            unset($fields['account']['account_password']);
            unset($fields['account']['account_password-2']);
            unset($fields['account']['account_username']); 
        }
    }

    return $fields;
}

试试上面的代码。


推荐阅读