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

时间:2021-05-13 13:51:14

标签: php wordpress woocommerce

这是我在 woocommerce 中以编程方式向产品添加属性的方式:

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

1 个答案:

答案 0 :(得分:0)

您当前已按照 wordpres developer docs

中的说明设置为追加而不是覆盖
wp_set_post_terms( int $post_id, string|array $tags = '', string $taxonomy = 'post_tag', bool $append = false )

通过将 append 设置为 false 来更改您的代码如下

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