Php Woocommerce以编程方式创建可变产品多属性
我正在以编程方式创建可变产品。已成功创建单个属性Size,但现在我正在尝试创建另一个属性颜色。我的表单中有这个属性数组Php Woocommerce以编程方式创建可变产品多属性,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在以编程方式创建可变产品。已成功创建单个属性Size,但现在我正在尝试创建另一个属性颜色。我的表单中有这个属性数组 Array ( [0] => size [1] => color ) 这是我试过的代码: $args_t = array( 'orderby' => 'name', 'order' => 'ASC', 'hide_empty'
Array
(
[0] => size
[1] => color
)
这是我试过的代码:
$args_t = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'fields' => 'names'
);
if ($product_attributes) {
foreach ($product_attributes as $attr) {
$avail_attributes = array();
$avail_attributes = get_terms(wc_attribute_taxonomy_name($attr), $args_t);
$attr = 'pa_'.$attr;
wp_set_object_terms($new_product_id, $avail_attributes, $attr);
$thedata = array();
$thedata = Array($attr => Array(
'name' => $attr,
'value' => '',
'postion' => '0',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta($new_product_id, '_product_attributes', $thedata);
}
}
以下是当前情况的屏幕截图:
您的代码中存在问题。。很明显,您正在更新foreach循环中的post元。使其覆盖当前值直到最后一个值。请尝试下面的代码
if ($product_attributes) {
$thedata = array();
foreach ($product_attributes as $attr) {
$avail_attributes = array();
$avail_attributes = get_terms(wc_attribute_taxonomy_name($attr), $args_t);
$attr = 'pa_'.$attr;
wp_set_object_terms($new_product_id, $avail_attributes, $attr);
$thedata[sanitize_title($attr)] = Array(
'name' => wc_clean($attr),
'value' => '',
'postion' => '0',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
);
}
update_post_meta($new_product_id, '_product_attributes', $thedata);
}
您的代码中存在问题。。很明显,您正在更新foreach循环中的post元。使其覆盖当前值直到最后一个值。请尝试下面的代码
if ($product_attributes) {
$thedata = array();
foreach ($product_attributes as $attr) {
$avail_attributes = array();
$avail_attributes = get_terms(wc_attribute_taxonomy_name($attr), $args_t);
$attr = 'pa_'.$attr;
wp_set_object_terms($new_product_id, $avail_attributes, $attr);
$thedata[sanitize_title($attr)] = Array(
'name' => wc_clean($attr),
'value' => '',
'postion' => '0',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
);
}
update_post_meta($new_product_id, '_product_attributes', $thedata);
}