首页  >  问答  >  正文

向 Woocommerce 添加多个产品属性

我有一个表格可以将新书提交到我的 WooCommerce 网站。我以前只是将书的状况保存为产品属性。<​​/p>

// 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);

这很容易。现在我尝试添加图书作者的姓名和类型,但是当我复制代码时,它只设置最后一个产品属性。我知道我可能应该把它放在一个循环中,但我很愚蠢,否则我无法弄清楚我错过了什么。

$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);

P粉124890778P粉124890778181 天前381

全部回复(2)我来回复

  • P粉087074897

    P粉0870748972024-03-27 13:25:50

    很高兴您找到了解决方案。但是,原始问题的问题尚未得到解答,并且您的解决方案中存在一个简单的错误,应该予以改进。

    回到原来的问题。

    您的原始代码存在多个问题。

    1. 您的代码中的变量命名和使用包含错误。第二次调用update_post_meta;您分配$att_condition变量但使用att-genre。如果您想使用 3 个变量,请更正此项。但是,该代码仍然无法运行。

    2. 您对同一个元密钥 _product_attributes 调用 update_post_meta 3 次(假设对第一个问题进行了修正)3 个不同的变量($att_condition、 $att_genre,$att_author)。 update_post_meta 会覆盖 meta_key 的值(如果找到且与传递的值不同)。尽管您已将数组值分配给变量,但它们都是不同的,并且仅保存一个数组记录。因此,最后一次调用 update_post_meta 会覆盖之前的写入。 相反,您希望将所有属性数组数据分配给单个数组变量,然后对 update_post_meta 进行一次调用。然后,它将使用您添加的所有属性正确更新。

    这将导致您获得更强大且可支持的 foreach 样式解决方案。

    您的解决方案中遇到的简单问题是性能问题。您已包含

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

    在 foreach 循环内而不是在其外部。因此,目前仍会通过 update_post_meta 向数据库写入 3 次,并在迭代输入数组并构建 $thedata 时逐渐增大负载。

    解决方案: 将 update_post_meta 调用移至 foreach 之后,以便每次执行仅进行一次调用。

    最后,包装此调用,并且可能还包括您对 $my_product_attributes 的初始分配或在您认为适合您的需求的任何相关条件逻辑中使用 $value关于空值的初始化和处理。

    回复
    0
  • P粉327903045

    P粉3279030452024-03-27 10:23:39

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

    我将表单变量放入一个数组中,然后运行这个 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);
    }

    回复
    0
  • 取消回复