[英]How do I create a product with variations?
I'm trying to create a product in php with variations.我正在尝试在 php 中创建一个带有变体的产品。 I've gotten the product creation but I can't seem to figure out how to create variations for it.
我已经创建了产品,但我似乎无法弄清楚如何为其创建变体。 Googling only returns adding variations to existing products, but I'm trying to create a product completely from scratch, give it an attribute, and using that attribute to make variations from an array of strings.
谷歌搜索只会返回为现有产品添加变体,但我正在尝试完全从头开始创建产品,为其赋予属性,并使用该属性从字符串数组中进行变体。
$team = get_post();
$post_id = wp_insert_post(
array( 'post_title' => $team->post_title,
'post_type' => 'product',
'post_status' => 'publish'
)
);
wp_set_object_terms( $post_id, 'simple', 'variable' );
update_post_meta( $post_id, '_sku', 'FEES-'.$team->ID );
This is what I have so far.这就是我到目前为止所拥有的。
I think you need to use "WC_Product_Variation"
class for registering variations and wp_insert_post
for creating the product variable.我认为您需要使用
"WC_Product_Variation"
class 来注册变体,并wp_insert_post
来创建产品变量。
have a look at this example, it might help you or give an idea.看看这个例子,它可能会对你有所帮助或给出一个想法。
Create WooCommerce Variable Product with Attributes 创建具有属性的 WooCommerce 可变产品
See more info in woocommerce docs: WC_Product_Variation在 woocommerce 文档中查看更多信息: WC_Product_Variation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.