Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCommerce:将产品属性添加到产品中的现有属性_Php_Wordpress_Woocommerce_Attributes_Product - Fatal编程技术网

Php WooCommerce:将产品属性添加到产品中的现有属性

Php WooCommerce:将产品属性添加到产品中的现有属性,php,wordpress,woocommerce,attributes,product,Php,Wordpress,Woocommerce,Attributes,Product,我正在努力为产品添加属性 我有一系列的关键字要添加到产品中: $clean_keywords = array('cake','cup cakes'); $term_taxonomy_ids = wp_set_object_terms( get_the_ID(), $clean_keywords, 'pa_keywords', true ); $thedata = Array('pa_keywords' => Array( 'name' => 'pa_keywords',

我正在努力为产品添加属性

我有一系列的关键字要添加到产品中:

$clean_keywords = array('cake','cup cakes');
$term_taxonomy_ids = wp_set_object_terms( get_the_ID(), $clean_keywords, 'pa_keywords', true );
$thedata = Array('pa_keywords' => Array(
    'name' => 'pa_keywords',
    'value' => '',
    'is_visible' => '0',
    'is_taxonomy' => '1'
));

update_post_meta( get_the_ID(),'_product_attributes',$thedata);
这很好,但它会删除附加到产品的所有其他属性

我认为解决方案是获取当前属性并将其与
$thedata
变量合并。。。但不知道怎么做

有什么想法吗


谢谢

您需要先获取现有的产品属性,并在保存前将新的产品属性插入数组中。我还在数组中添加了2个缺少的参数

因此,您的代码应该是:

$product_id = get_the_ID();
$taxonomy = 'pa_keywords';
$clean_keywords = array('cake','cup cakes');
$term_taxonomy_ids = wp_set_object_terms( $product_id, $clean_keywords, $taxonomy, true );

// Get existing attributes
$product_attributes = get_post_meta( $product_id, '_product_attributes', true);

// get the count of existing attributes to set the "position" in the array
$count = count($product_attributes);

// Insert new attribute in existing array of attributes (if there is any)
$product_attributes[$taxonomy] = array(
    'name' => $taxonomy,
    'value' => '',
    'position' => $count, // added
    'is_visible' => '0',
    'is_variation' => '0', // added (set the right value)
    'is_taxonomy' => '1'
);

// Save the data
update_post_meta( $product_id, '_product_attributes', $product_attributes );

现在应该可以在不删除现有数据的情况下工作。

刚刚找到了如何做到这一点。。。再次感谢