是否可以在用户注册后自动创建产品?
我正在创建一种市场,所以当用户注册为供应商时,我希望 Woocommerce 自动创建具有这些规范的产品:
产品名称 = Booking - the_author_meta ('display_name')
产品 slug = calendar-the_author_meta ('user_login')
例如。 John Doe 使用“用户名:johndoe”注册为供应商。注册完成后,会自动创建一个产品。
产品名称是“Booking - John Doe”。产品标签是:“calendar-johndoe”。
因此可以在 mysite.com/product/calendar-johndoe 找到产品 Booking - John Doe
谢谢你的时间
最佳答案
You can achieve this by hooking it to
user_register
because WordPress core handles user registration and runs theuser_register
hook right after a user is registered. and to create a product you can usewp_insert_post
methord to insert post withpost_type = product
代码如下:
add_action( 'user_register', 'myCustomProduct', 10, 1 );
function myCustomProduct($user_id)
{
$user_info = get_userdata($user_id);
$display_name = $user_info->display_name;
$user_full_name = get_user_meta($user_id, 'first_name', TRUE) . ' ' . get_user_meta($user_id, 'last_name', TRUE);
$my_product_name = 'Booking - ' . trim($user_full_name);
$my_slug = 'calendar-' . str_replace(array(' '), '', strip_tags($display_name));
$post = array(
'post_author' => $user_id,
'post_content' => '',
'post_status' => "publish",
'post_title' => wp_strip_all_tags($my_product_name),
'post_name'=> $my_slug,
'post_parent' => '',
'post_type' => "product",
);
//Create Post
$post_id = wp_insert_post($post, $wp_error);
//set Product Category
//wp_set_object_terms( $post_id, 'Your Category Name ', 'product_cat' );
//set product type
wp_set_object_terms($post_id, 'simple', 'product_type');
update_post_meta($post_id, '_visibility', 'visible');
update_post_meta($post_id, '_stock_status', 'instock');
update_post_meta($post_id, 'total_sales', '0');
update_post_meta($post_id, '_sku', "");
update_post_meta($post_id, '_product_attributes', array());
update_post_meta($post_id, '_manage_stock', "no");
update_post_meta($post_id, '_backorders', "no");
update_post_meta($post_id, '_stock', "");
//update_post_meta($post_id, '_downloadable', 'yes');
//update_post_meta($post_id, '_virtual', 'yes');
//update_post_meta($post_id, '_regular_price', "1");
//update_post_meta($post_id, '_sale_price', "1");
//update_post_meta($post_id, '_purchase_note', "");
//update_post_meta($post_id, '_featured', "no");
//update_post_meta($post_id, '_weight', "");
//update_post_meta($post_id, '_length', "");
//update_post_meta($post_id, '_width', "");
//update_post_meta($post_id, '_height', "");
//update_post_meta($post_id, '_sale_price_dates_from', "");
//update_post_meta($post_id, '_sale_price_dates_to', "");
//update_post_meta($post_id, '_price', "1");
//update_post_meta($post_id, '_sold_individually', "");
}
代码位于您的事件子主题(或主题)的 function.php 文件中。或者也可以在任何插件 php 文件中。
代码已经过测试并且功能齐全。
请注意:我假设您的注册表单有 first_name
和 last_name
字段,否则如果您使用默认的 WooCommerce 注册表单,那么它只有 email
和 password
字段,所以上面的代码将生成一个产品名称为 Booking -
。
希望这对您有所帮助!
关于wordpress - 用户注册后创建产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41220947/