创建帖子后创建Woocommerce产品 [英] Create a Woocommerce product when post is created

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

问题描述

我正在WordPress网站上使用Woocommerce.并且我在我的网站上出售各种商品.

I am using Woocommerce on my WordPress site. and I am Selling Various items on my site.

我想要的是每次我创建一个关于特定商品的小文章时,它还会创建一个Woocommerce产品页面,其中有一个商品可以出售.

What I want is that every time I create a small post about a particular item.it also creates a Woocommerce product page with the one item available to be sold.

例如:我创建了一个有关定制珠宝的帖子,并写了一个小帖子,客户可以查看该帖子并从Woocommerce产品部分购买.
产品缺货后,该帖子会隐藏",直到我有库存为止.

For example: I create a post about custom-made jewelry and I write a small post about it, and the customer can look at the post and buy it from the Woocommerce product section.
Once the product is out of stock the post disappears"Hidden" until I have them in stock.

如何做到这一点?有什么想法吗?

How this can be done? Any ideas?

推荐答案

将其添加到函数文件中,并在必要时用变量替换空字符串.这应该可以解决您的问题.

Add this to your function file and replace empty strings with variable where necessary. This should solve your problem.

add_action( 'save_post', 'auto_create_product_from_post', 100, 2 ); 
function auto_create_product_from_post($id, $post){
$post_id = wp_insert_post( array(
    //'post_title' => 'Adams Product',
    'post_title' => $post.post_title,
    'post_content' => $post.post_title,
    'post_status' => 'publish',
    'post_type' => "product",
) );
    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, '_downloadable', 'no' );
    update_post_meta( $post_id, '_virtual', 'yes' );
    update_post_meta( $post_id, '_regular_price', '' );
    update_post_meta( $post_id, '_sale_price', '' );
    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, '_sku', '' );
    update_post_meta( $post_id, '_product_attributes', array() );
    update_post_meta( $post_id, '_sale_price_dates_from', '' );
    update_post_meta( $post_id, '_sale_price_dates_to', '' );
    update_post_meta( $post_id, '_price', '' );
    update_post_meta( $post_id, '_sold_individually', '' );
    update_post_meta( $post_id, '_manage_stock', 'no' );
    update_post_meta( $post_id, '_backorders', 'no' );
    update_post_meta( $post_id, '_stock', '' );
}

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

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