我已经在WooCommerce版本的3+中创建了一个变量产品(“父”产品)。从一个WordPress插件,我想以编程方式创建具有新属性值的产品变体(“子”产品)。
变体属性已在WooCommerce中设置。
因此,每次创建一个变体时,都应该以编程方式创建新属性的值,并在父变量product中进行设置。
如何做到这一点?有可能吗?
更新:我已经写了更多我希望的代码行,并尝试了许多事情来解决它,使用woocommerce对象,并使用WordPress数据库对象在数据库中添加了缺少的关于术语、术语元、术语与post的关系的数据-但没有任何东西足以使其工作。我不能准确地指出我哪里错了--这就是为什么我不能提供一个更小的问题--堆栈溢出更适合的东西。
发布于 2018-10-30 18:43:28
既然我不能让上面的任何一个例子工作,我就把它扔出去。不要问我为什么,因为其他人似乎都成功了。因此,我采取了极简主义的方法,试图找出产品属性+变体的基本要素(通过在wp中手动创建并查看db),并得出了以下结论。
$article_name = 'Test';
$post_id = wp_insert_post( array(
'post_author' => 1,
'post_title' => $article_name,
'post_content' => 'Lorem ipsum',
'post_status' => 'publish',
'post_type' => "product",
) );
wp_set_object_terms( $post_id, 'variable', 'product_type' );
$attr_label = 'Test attribute';
$attr_slug = sanitize_title($attr_label);
$attributes_array[$attr_slug] = array(
'name' => $attr_label,
'value' => 'alternative 1 | alternative 2',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '0' // for some reason, this is really important
);
update_post_meta( $post_id, '_product_attributes', $attributes_array );
$parent_id = $post_id;
$variation = array(
'post_title' => $article_name . ' (variation)',
'post_content' => '',
'post_status' => 'publish',
'post_parent' => $parent_id,
'post_type' => 'product_variation'
);
$variation_id = wp_insert_post( $variation );
update_post_meta( $variation_id, '_regular_price', 2 );
update_post_meta( $variation_id, '_price', 2 );
update_post_meta( $variation_id, '_stock_qty', 10 );
update_post_meta( $variation_id, 'attribute_' . $attr_slug, 'alternative 1' );
WC_Product_Variable::sync( $parent_id );
$variation_id = wp_insert_post( $variation );
update_post_meta( $variation_id, '_regular_price', 2 );
update_post_meta( $variation_id, '_price', 2 );
update_post_meta( $variation_id, '_stock_qty', 10 );
update_post_meta( $variation_id, 'attribute_' . $attr_slug, 'alternative 2' );
WC_Product_Variable::sync( $parent_id );
这不是使用全局产品属性,而是特定于文章的属性。我希望它能帮助一些人,因为在我开始工作之前,我已经准备好拔出我的头发了。
编辑:我要说的是,只有当你不能让官方方法工作时才使用它。它们会随着时间的推移而改变这些东西(比如‘_regular_price’之类的字段名),而且这种方式很有可能不是未来的最佳选择。
发布于 2020-07-20 07:16:53
不知道为什么,但是这些解决方案对我都不起作用,所以我决定创建我自己的:
<?php
/**
* Create a variable product on woocommerce
* @return int Product ID
*/
function pricode_create_product(){
$product = new WC_Product_Variable();
$product->set_description('T-shirt variable description');
$product->set_name('T-shirt variable');
$product->set_sku('test-shirt');
$product->set_price(1);
$product->set_regular_price(1);
$product->set_stock_status();
return $product->save();
}
/**
* Create Product Attributes
* @param string $name Attribute name
* @param array $options Options values
* @return Object WC_Product_Attribute
*/
function pricode_create_attributes( $name, $options ){
$attribute = new WC_Product_Attribute();
$attribute->set_id(0);
$attribute->set_name($name);
$attribute->set_options($options);
$attribute->set_visible(true);
$attribute->set_variation(true);
return $attribute;
}
/**
* [pricode_create_variations description]
* @param [type] $product_id [description]
* @param [type] $values [description]
* @return [type] [description]
*/
function pricode_create_variations( $product_id, $values ){
$variation = new WC_Product_Variation();
$variation->set_parent_id( $product_id );
$variation->set_attributes($values);
$variation->set_status('publish');
$variation->set_sku($data->sku);
$variation->set_price($data->price);
$variation->set_regular_price($data->price);
$variation->set_stock_status();
$variation->save();
$product = wc_get_product($product_id);
$product->save();
}
//Adding product
$product = pricode_create_product();
//Creating Attributes
$atts = [];
$atts[] = pricode_create_attributes('color',['red', 'green']);
$atts[] = pricode_create_attributes('size',['S', 'M']);
//Adding attributes to the created product
$product->set_attributes( $atts );
$product->save();
//Create variations
pricode_create_variations( $product->get_id(), ['color' => 'red', 'size' => 'M']);
希望能对其他人有所帮助。
发布于 2018-02-01 00:38:05
根据LoicTheAztec的答案展开,您可以通过对他的代码进行以下修改来检查属性组合是否存在。
function create_update_product_variation( $product_id, $variation_data ){
if(isset($variation_data['variation_id'])) {
$variation_id = $variation_data['variation_id'];
} else {
// if the variation doesn't exist then create it
// Get the Variable product object (parent)
$product = wc_get_product($product_id);
$variation_post = array(
'post_title' => $product->get_title(),
'post_name' => 'product-'.$product_id.'-variation',
'post_status' => 'publish',
'post_parent' => $product_id,
'post_type' => 'product_variation',
'guid' => $product->get_permalink()
);
// Creating the product variation
$variation_id = wp_insert_post( $variation_post );
}
// ...
}
示例用法
// The variation data
$variation_data = array(
'attributes' => array(
'size' => 'M',
'color' => 'Green',
),
'sku' => '',
'regular_price' => '22.00',
'sale_price' => '1',
'stock_qty' => 1,
);
// check if variation exists
$meta_query = array();
foreach ($variation_data['attributes'] as $key => $value) {
$meta_query[] = array(
'key' => 'attribute_pa_' . $key,
'value' => $value
);
}
$variation_post = get_posts(array(
'post_type' => 'product_variation',
'numberposts' => 1,
'post_parent' => $parent_id,
'meta_query' => $meta_query
));
if($variation_post) {
$variation_data['variation_id'] = $variation_post[0]->ID;
}
create_update_product_variation( $product_id, $variation_data );
https://stackoverflow.com/questions/47518280
复制相似问题