简体   繁体   English

为Woocommerce添加多个产品属性

[英]Adding multiple product attributes to Woocommerce

I have a form to submit new books to my WooCommerce website.我有一个表格可以向我的 WooCommerce 网站提交新书。 I used to have it just save the book's condition as a Product Attribute.我曾经让它只是将书的条件保存为产品属性。

// Set the book's condition

$condition = $_POST['condition'];
wp_set_object_terms( $product_id, $condition, 'pa_condition', true );

$att_condition = Array('pa_condition' =>Array(
       'name'=>'pa_condition',
       'value'=>$condition,
       'is_visible' => '1',
       'is_taxonomy' => '1'
     ));

update_post_meta( $product_id, '_product_attributes', $att_condition);

That was easy.那很简单。 Now I am trying to add the Book Author's name and the Genre, but when I duplicated the code it only sets the last Product Attribute.现在我正在尝试添加图书作者的姓名和类型,但是当我复制代码时,它只设置了最后一个产品属性。 I know I should probably put it in a loop, but I'm being stupid and otherwise I can't figure out what I am missing.我知道我可能应该把它放在一个循环中,但我很愚蠢,否则我无法弄清楚我错过了什么。

$condition = $_POST['condition'];
$genre = $_POST['genre'];
$author = $_POST['author'];
    
wp_set_object_terms( $product_id, $condition, 'pa_condition', true );

$att_condition = Array('pa_condition' =>Array(
       'name'=>'pa_condition',
       'value'=>$condition,
       'is_visible' => '1',
       'is_taxonomy' => '1'
     ));

update_post_meta( $product_id, '_product_attributes', $att_condition);

wp_set_object_terms( $product_id, $genre, 'pa_genre', true );

$att_condition = Array('pa_genre' =>Array(
       'name'=>'pa_genre',
       'value'=>$genre,
       'is_visible' => '1',
       'is_taxonomy' => '1'
     ));

update_post_meta( $product_id, '_product_attributes', $att_genre);

wp_set_object_terms( $product_id, $author, 'pa_author', true );

$att_author = Array('pa_author' =>Array(
       'name'=>'pa_author',
       'value'=>$author,
       'is_visible' => '1',
       'is_taxonomy' => '1'
     ));

update_post_meta( $product_id, '_product_attributes', $att_author);

在这种情况下,您可以尝试使用add_post_meta函数而不是update_post_meta

I found the solution on https://stackoverflow.com/a/45475863/12092133 .我在https://stackoverflow.com/a/45475863/12092133上找到了解决方案。

I took my form variables and threw them into an array, and then ran this foreach and it did the trick.我把我的表单变量放入一个数组中,然后运行这个 foreach 就成功了。

$my_product_attributes['condition'] = $_POST['condition'];
$my_product_attributes['genre'] = $_POST['genre'];
$my_product_attributes['authors'] = $_POST['author'];

foreach ($my_product_attributes as $key => $value) {
    $key = 'pa_' . $key;
    $attribute_values = explode(",", $value);

    wp_set_object_terms($product_id, $attribute_values, $key, false);
    $thedata[sanitize_title($key)] = Array(
        'name' => wc_clean($key),
        'value' => $attribute_values,
        'postion' => '0',
        'is_visible' => '1',
        'is_variation' => '0',
        'is_taxonomy' => '1'
    );
    update_post_meta($product_id, '_product_attributes', $thedata);
}

It's good that you've found a solution.很高兴您找到了解决方案。 However, the question of the original problem has gone unanswered, and there is a simple fault in your solution which should be improved upon.但是,原始问题的问题没有得到解答,您的解决方案中有一个简单的错误,应该改进。

To the original question.到原来的问题。

That was easy.那很简单。 Now I am trying to add the Book Author's name and the Genre, but when I duplicated the code it only sets the last Product Attribute.现在我正在尝试添加图书作者的姓名和类型,但是当我复制代码时,它只设置了最后一个产品属性。 I know I should probably put it in a loop, but I'm being stupid and otherwise I can't figure out what I am missing.我知道我可能应该把它放在一个循环中,但我很愚蠢,否则我无法弄清楚我错过了什么。

There are multiple issues in your original code.您的原始代码中有多个问题。

  1. Your variable naming and usage in your code contains an error.您的代码中的变量命名和使用包含错误。 The second call to update_post_meta ;第二次调用update_post_meta you assign $att_condition variable but use att-genre .您分配$att_condition变量但使用att-genre If you want to use 3 variables, correct this.如果要使用 3 个变量,请更正此问题。 However, the code still won't work.但是,代码仍然无法正常工作。

  2. You call update_post_meta for the same meta_key _product_attributes 3 times with (assuming the correction to the first issue) 3 different variables ( $att_condition, $att_genre, $att_author ).您为相同的 meta_key _product_attributes调用update_post_meta 3 次(假设对第一个问题进行了更正)3 个不同的变量( $att_condition、$att_genre、$att_author )。 update_post_meta overwrites the value of the meta_key, if found and different to the value being passed. update_post_meta覆盖 meta_key 的值,如果找到并且与传递的值不同。 Though you've assigned array values to the variables, they are each distinct and hold only one array record.尽管您已经为变量分配了数组值,但它们每个都是不同的并且只保存一个数组记录。 And so the last call to update_post_meta overwrites the previous writes.所以最后一次调用update_post_meta会覆盖之前的写入。 Rather, you want to be assigning all your attribute array data to a single array variable and then making a single call to update_post_meta .相反,您希望将所有属性数组数据分配给单个数组变量,然后对update_post_meta进行一次调用。 Which will then update correctly with all the attributes you have added.然后它将使用您添加的所有属性正确更新。

Which then leads towards the more robust and supportable foreach style solution you have come to.然后,这会导致您获得更健壮和可支持的 foreach 样式解决方案。

The simple issue you have in your solution is one of performance.您在解决方案中遇到的简单问题是性能问题之一。 You have included你已经包括

update_post_meta($product_id, '_product_attributes', $thedata);

within the foreach loop instead of outside it.foreach 循环内而不是在它之外。 So, this will currently still write to the database via update_post_meta 3 times, with incrementally larger payloads as you iterate your input array and build up $thedata .因此,这目前仍将通过update_post_meta 3 次写入数据库,随着您迭代输入数组并构建$thedata ,有效负载会逐渐增大。

Solution: Move the update_post_meta call to after your foreach so that only one call is made per execution.解决方案:将update_post_meta调用移到 foreach 之后,以便每次执行只进行一次调用。

Finally, wrap this call and likely also either your initial assignment to your $my_product_attributes or the usage of $value in any relevant conditional logic you determine appropriate for your needs regarding initialisation and handling of empty values.最后,包装此调用,并且可能还包装您对$my_product_attributes的初始分配或$value在您确定适合您的初始化和处理空值的需求的任何相关条件逻辑中的使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM