【问题标题】:Wordpress search/create attribute assign then search/add valueWordpress 搜索/创建属性分配然后搜索/添加值
【发布时间】:2019-10-21 18:01:43
【问题描述】:

我正在为 API 创建一个 wordpress 插件。我正在获取产品,并且对于我获取的每个产品以及具有 namevalue 的属性数组。我尝试了一些东西,但它似乎没有做任何事情。

foreach($product->attributes as $attribute) {
    $slug = 'pa_' . strtolower($attribute->name);
    $attribute_value = $attribute->value;

    $thedata = [
        $slug => [
            'name'=> $slug, 
            'value'=> $attribute_value,
            'is_visible' => '1',
            'is_variation' => '1',
            'is_taxonomy' => '1'
        ]
    ];

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

我的猜测是我必须搜索(或添加如果不存在)和属性,然后对值执行相同的操作。我不确定。

事实: - 脚本中的其他所有内容都有效,是的 - $product 来了,并且 $product_id 保存了在脚本中创建的产品的 id。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    如果您只在 post_meta 中设置值,则无法在编辑产品屏幕上看到属性。尝试将 wp_set_object_terms 添加到您的 foreach 中

    foreach($product->attributes as $attribute) {
        $slug = 'pa_' . strtolower($attribute->name);
        $attribute_value = $attribute->value;
    
        wp_set_object_terms($post_id, attribute->value, $attribute->name, true);  
    
        $thedata = [
            $slug => [
                'name'=> $slug, 
                'value'=> $attribute_value,
                'is_visible' => '1',
                'is_variation' => '1',
                'is_taxonomy' => '1'
            ]
        ];
    
        update_post_meta( $product_id, '_product_attributes', $thedata);
    }
    

    在 SO here 上有一篇关于它的帖子。

    【讨论】:

    • 谢谢,这会将它们添加到产品页面上,但我仍然需要它们在属性页面上,以及添加的值和建立的连接。
    • 尝试使用 $thedata[] = 并将 update_post_meta 移到 foreach 之外,因为它看起来像是用更新覆盖了每个循环的“_product_attributes”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多