用户注册后创建产品 [英] Create product after User registration

查看:30
本文介绍了用户注册后创建产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在用户注册后自动创建产品?

Is it possible to create automatically a product after a user registers?

我正在创建一种市场,因此当用户注册为供应商时,我希望 Woocommerce 自动创建具有以下规格的产品:

I'm creating a sort of marketplace, so when a user signs up as vendor, I want Woocommerce to automatically create a product with these specs:

产品名称 = 预订 - the_author_meta ('display_name')
Product slug = calendar-the_author_meta ('user_login')

Product name = Booking - the_author_meta ('display_name')
Product slug = calendar-the_author_meta ('user_login')

例如.John Doe 使用用户名:johndoe"注册为供应商.注册完成后,会自动创建一个产品.

For example. John Doe registers as vendor with "username: johndoe". When the registration is complete, a product is automatically created.

产品名称是Booking - John Doe".产品标号为:calendar-johndoe".

The product name is " Booking - John Doe". The product slug is: "calendar-johndoe".

因此可以在 mysite.com/product/calendar-johndoe 上找到产品 Booking - John Doe

So the product Booking - John Doe can be found at mysite.com/product/calendar-johndoe

感谢您的时间

推荐答案

您可以通过将其挂接到 user_register 来实现这一点,因为WordPress 核心处理用户注册并运行user_register 在用户注册后立即挂钩.并创建一个可以使用wp_insert_post 方法插入的产品邮政post_type = product

You can achieve this by hooking it to user_register because WordPress core handles user registration and runs the user_register hook right after a user is registered. and to create a product you can use wp_insert_post methord to insert post with post_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_namelast_name 字段,否则如果您使用默认的 WooCommerce 注册表,那么它只有 emailpassword 字段,那么上面的代码将生成一个产品名称为 Booking -.

Please Note: I have assumed that your registration form has first_name and last_name field otherwise if you are using default WooCommerce registration form then it only has email and password field, So then above code will generate a product name as Booking -.

希望这会有所帮助!

这篇关于用户注册后创建产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆