Wordpress Woocommerce获得产品的变体列表
我有一个插件,将Woocomece中的一个产品保存为示例产品,因此在保存该插件后,我启动了一个钩子,将该产品更改为variable,并初始化一些默认属性和变体,我将其设置为:Wordpress Woocommerce获得产品的变体列表,wordpress,woocommerce,Wordpress,Woocommerce,我有一个插件,将Woocomece中的一个产品保存为示例产品,因此在保存该插件后,我启动了一个钩子,将该产品更改为variable,并初始化一些默认属性和变体,我将其设置为: //change type $product_classname = WC_Product_Factory::get_product_classname($product_id, 'variable'); $new_product = new $product_classname($product_id); // li
//change type
$product_classname = WC_Product_Factory::get_product_classname($product_id, 'variable');
$new_product = new $product_classname($product_id);
// link default attribute
$attributes = create_default_attributes();
$new_product->set_attributes($attributes);
// link all variation
$data_store = $product->get_data_store();
$data_store->create_all_product_variations($product);
$new_product->save();
function create_default_attributes()
{
$attribute_taxonomies = wc_get_attribute_taxonomies();
$attributes = array();
if (!empty($attribute_taxonomies)) {
$tax_custom = false;
foreach ($attribute_taxonomies as $tax) {
$is_custom_name = "is_custom _" . $tax->attribute_id;
$is_custom = get_option($is_custom _name);
if ($is_custom == 1) {
$tax_custom = $tax;
break;
}
}
if ($tax_custom) {
$attribute_taxonomy_name = wc_attribute_taxonomy_name($tax_custom->attribute_name);
$attribute = new WC_Product_Attribute();
$attribute->set_id($tax_custom->attribute_id);
$attribute->set_name($attribute_taxonomy_name);
$all_terms = get_terms($attribute_taxonomy_name);
$option = array();
if (!empty($all_terms)) {
foreach ($all_terms as $term) {
$option[$term->term_id] = $term->name;
}
}
$attribute->set_options($option);
$attribute->set_position(0);
$attribute->set_visible(1);
$attribute->set_variation(1);
$attributes[] = $attribute;
}
}
return $attributes;
}
$variations=$new_product->get_children();
write_log($variations);
foreach ($variations as $value) {
$single_variation = new WC_Product_Variation($value);
}
所有这些都按预期工作,现在我需要将所有变化价格更改为样本产品的默认价格,如下所示:
//change type
$product_classname = WC_Product_Factory::get_product_classname($product_id, 'variable');
$new_product = new $product_classname($product_id);
// link default attribute
$attributes = create_default_attributes();
$new_product->set_attributes($attributes);
// link all variation
$data_store = $product->get_data_store();
$data_store->create_all_product_variations($product);
$new_product->save();
function create_default_attributes()
{
$attribute_taxonomies = wc_get_attribute_taxonomies();
$attributes = array();
if (!empty($attribute_taxonomies)) {
$tax_custom = false;
foreach ($attribute_taxonomies as $tax) {
$is_custom_name = "is_custom _" . $tax->attribute_id;
$is_custom = get_option($is_custom _name);
if ($is_custom == 1) {
$tax_custom = $tax;
break;
}
}
if ($tax_custom) {
$attribute_taxonomy_name = wc_attribute_taxonomy_name($tax_custom->attribute_name);
$attribute = new WC_Product_Attribute();
$attribute->set_id($tax_custom->attribute_id);
$attribute->set_name($attribute_taxonomy_name);
$all_terms = get_terms($attribute_taxonomy_name);
$option = array();
if (!empty($all_terms)) {
foreach ($all_terms as $term) {
$option[$term->term_id] = $term->name;
}
}
$attribute->set_options($option);
$attribute->set_position(0);
$attribute->set_visible(1);
$attribute->set_variation(1);
$attributes[] = $attribute;
}
}
return $attributes;
}
$variations=$new_product->get_children();
write_log($variations);
foreach ($variations as $value) {
$single_variation = new WC_Product_Variation($value);
}
$variations的列表为空?为什么是空的?钩子完成后,我在管理区确认所有变体的列表仍然在这里,有什么想法吗?我认为这是woocomerce中的一个bug,但我已经改变了创建变体的方法,该方法的灵感来自:
// link all variation
$data_store = $product->get_data_store();
$data_store->create_all_product_variations($product);
致:
现在,我可以对变量进行任何更改。自定义
create\u default\u attributes()
函数缺少一些代码……因此,请添加此函数的代码或将其记录下来,因为您提供的代码不可测试。例如,create\u default\u attributes()功能被添加,如果有任何为什么要减少代码,请让我知道