Wordpress 用户注册后创建产品

Wordpress 用户注册后创建产品,wordpress,woocommerce,Wordpress,Woocommerce,用户注册后是否可以自动创建产品 我正在创建一种市场,因此当用户注册为供应商时,我希望Woocommerce自动创建具有以下规格的产品: 产品名称=预订-作者元('display\u name') Product slug=calendar-作者元('user\u login') 比如说。John Doe以“用户名:johndoe”注册为供应商。注册完成后,将自动创建产品 产品名称为“Booking-John Doe”。产品名称是:“日历约翰多” 因此,产品预订-John Doe可以在mysite

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

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

产品名称=预订-
作者元('display\u name')

Product slug=calendar-
作者元('user\u login')

比如说。John Doe以“用户名:johndoe”注册为供应商。注册完成后,将自动创建产品

产品名称为“Booking-John Doe”。产品名称是:“日历约翰多”

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

谢谢你抽出时间

您可以通过将其挂接到
user\u register
来实现这一点,因为 WordPress core处理用户注册并运行 在用户注册后立即挂起。以及 创建一个可以使用Method插入帖子的产品 带有
post\u 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文件中使用。
代码经过测试,功能齐全。

请注意:我假设您的登记表中有
名字
姓氏
字段,否则如果您使用默认的WooCommerce登记表,则它只有
电子邮件
密码
字段,因此上述代码将生成一个产品名称,即
预订-


希望这有帮助

太棒了!它可以完美触发,甚至可以指定正确的供应商!我只想知道为什么它调用每个产品预订-管理员和slug calendar管理员而不是用户凭据。非常感谢。我很高兴它成功了,因为我还没有测试过上面的代码。这些都是我对WordPress的了解。”我回家后会测试一下,然后告诉你。是的,我想这需要管理员证书。我试图更改
$user\u info->display\u name
$user\u info->user\u firstname,但仍然没有更改任何内容。还是谢谢你@PaoloMontalto:我已经更新了我的代码,实际上我硬编码了
user\u id
作为
get\u userdata(1)
,所以它提供了管理员详细信息,现在我修复了打字错误,并测试了代码,它工作正常;如果您想设置产品类别,请取消注释
wp\u set\u object\u terms
,并替换为您的产品类别名称。实际上,我没有注意到它是这样创建产品标题的:Booking-username。因此,基本上对于我们的带有用户名*jdoe的johndoe示例,它显示了product:product name:Booking-jdoe product slug:calendar jdoe