首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Woocommerce以编程方式删除产品属性php

Woocommerce以编程方式删除产品属性php
EN

Stack Overflow用户
提问于 2021-05-13 21:51:15
回答 1查看 336关注 0票数 1

以下是我如何以编程方式向woocommerce中的产品添加属性:

代码语言:javascript
运行
复制
if($item->POIOTHTA != null && !empty($item->POIOTHTA)){
                wp_set_object_terms( $post_id, $item->POIOTHTA, 'pa_poiotita', true );

                $att_quality = Array('pa_poiotita' =>Array(
                       'name'=>'pa_poiotita',
                       'value'=>$item->POIOTHTA,
                       'is_visible' => '1',
                       'is_taxonomy' => '1'
                     ));
            }
            
            
            if($item->MHKOS != null && !empty($item->MHKOS)){
                wp_set_object_terms( $post_id, $item->MHKOS, 'pa_mikos', true );

                $att_length = Array('pa_mikos' =>Array(
                       'name'=>'pa_mikos',
                       'value'=>$item->MHKOS,
                       'is_visible' => '1',
                       'is_taxonomy' => '1'
                     ));
            }
            
            if($item->PAXOS != null && !empty($item->PAXOS)){
                wp_set_object_terms( $post_id, $item->PAXOS, 'pa_paxos', true );

                $att_thickness = Array('pa_paxos' =>Array(
                       'name'=>'pa_paxos',
                       'value'=>$item->PAXOS,
                       'is_visible' => '1',
                       'is_taxonomy' => '1'
                     ));
            }
            
            
            if($item->DIAMETROS != null && !empty($item->DIAMETROS)){
                wp_set_object_terms( $post_id, $item->DIAMETROS, 'pa_diametros', true );

                $att_diameter = Array('pa_diametros' =>Array(
                       'name'=>'pa_diametros',
                       'value'=>$item->DIAMETROS,
                       'is_visible' => '1',
                       'is_taxonomy' => '1'
                     ));
            }
            
            update_post_meta( $post_id, '_product_attributes', array_merge((array)$att_quality, (array)$att_length, (array)$att_thickness, (array)$att_diameter));

问题是,如果在另一个更新中更改了属性,则不会替换现有的属性,而是添加新的属性。

如何删除不再使用的属性,或在插入新属性之前将其全部删除?

一开始我试着调用这个函数,但是没有解决问题:delete_post_meta($post_id, '_product_attributes');

EN

回答 1

Stack Overflow用户

发布于 2021-05-13 23:12:01

您当前已设置为追加而不是覆盖,如wordpres developer docs中所述

代码语言:javascript
运行
复制
wp_set_post_terms( int $post_id, string|array $tags = '', string $taxonomy = 'post_tag', bool $append = false )

如下所示,通过将append设置为false来更改代码

代码语言:javascript
运行
复制
if($item->POIOTHTA != null && !empty($item->POIOTHTA)){
                wp_set_object_terms( $post_id, $item->POIOTHTA, 'pa_poiotita', false);

                $att_quality = Array('pa_poiotita' =>Array(
                       'name'=>'pa_poiotita',
                       'value'=>$item->POIOTHTA,
                       'is_visible' => '1',
                       'is_taxonomy' => '1'
                     ));
            }
            
            
            if($item->MHKOS != null && !empty($item->MHKOS)){
                wp_set_object_terms( $post_id, $item->MHKOS, 'pa_mikos', false);

                $att_length = Array('pa_mikos' =>Array(
                       'name'=>'pa_mikos',
                       'value'=>$item->MHKOS,
                       'is_visible' => '1',
                       'is_taxonomy' => '1'
                     ));
            }
            
            if($item->PAXOS != null && !empty($item->PAXOS)){
                wp_set_object_terms( $post_id, $item->PAXOS, 'pa_paxos', false);

                $att_thickness = Array('pa_paxos' =>Array(
                       'name'=>'pa_paxos',
                       'value'=>$item->PAXOS,
                       'is_visible' => '1',
                       'is_taxonomy' => '1'
                     ));
            }
            
            
            if($item->DIAMETROS != null && !empty($item->DIAMETROS)){
                wp_set_object_terms( $post_id, $item->DIAMETROS, 'pa_diametros', false);

                $att_diameter = Array('pa_diametros' =>Array(
                       'name'=>'pa_diametros',
                       'value'=>$item->DIAMETROS,
                       'is_visible' => '1',
                       'is_taxonomy' => '1'
                     ));
            }
            
            update_post_meta( $post_id, '_product_attributes', array_merge((array)$att_quality, (array)$att_length, (array)$att_thickness, (array)$att_diameter));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67520421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档