WooCommerce产品供应商-注册

时间:2020-05-11 16:41:08

标签: php wordpress woocommerce

基本上,我使用的是“最终会员”注册表,该表将授予用户“待处理的供应商”角色。

问题在于,尽管为用户赋予了该角色,但该用户实际上并未注册为“供应商”。由于现在用户无法执行供应商应做的所有事情,因此造成了严重破坏。

我发现了一段代码,可以帮助将用户角色更改为正式的“ WooCommerce产品供应商”用户角色。代码如下:

    **
* Create a vendor on account registration.
*
* @param int $customer_id
* @param array $new_customer_data
* @return boid
*/

function wc_create_vendor_on_registration( $customer_id, $new_customer_data ) {
$username = $new_customer_data['user_login'];
$email = $new_customer_data['user_email'];

// Ensure vendor name is unique
if ( term_exists( $username, 'shop_vendor' ) ) {
$append = 1;
$o_username = $username;
while ( term_exists( $username, 'shop_vendor' ) ) {
$username = $o_username . $append;
$append ++;
}
}

// Create the new vendor
$return = wp_insert_term(
$username,
'shop_vendor',
array(
'description' => sprintf( __( 'The vendor %s', 'localization-domain' ), $username ),
'slug' => sanitize_title( $username )
)
);

if ( is_wp_error( $return ) ) {
wc_add_notice( __( '<strong>ERROR</strong>: Unable to create the vendor account for this user. Please contact the administrator to register your account.', 'localization-domain' ), 'error' );
} else {

// Update vendor data
$vendor_data['paypal_email'] = $email; // The email used for the account will be used for the payments
$vendor_data['commission'] = '50'; // The commission is 50% for each order
$vendor_data['admins'][] = $customer_id; // The registered account is also the admin of the vendor
update_option( 'shop_vendor_' . $return['term_id'], $vendor_data );
}
}

add_action( 'woocommerce_created_customer', 'wc_create_vendor_on_registration', 10, 2 ); 

将其放入我的function.php文件时,我没有收到任何错误,但问题仍然是,当用户注册时,他们没有被授予适当的角色“待处理的供应商”

我遇到的另一段代码提到它将把用户角色从Ultimate Member同步到正确的WP“ Pending Vendor”角色。这段代码如下:

add_action(‘um_after_user_is_approved’, ‘sync_um_and_wp_role’, 99 );
function sync_um_and_wp_role( $user_id ) {

// Get UM role
$role = get_user_meta( $user_id, ‘role’, true );

// Set WordPress role for same user
$wp_user_object = new WP_User( $user_id );
$wp_user_object->set_role( ‘seller’ );

}

如果有人可以帮助我,我将非常感激。

0 个答案:

没有答案