Woocommerce remove product attributes programmatically php

Viewed 1102

This is how I add attributes to a product in woocommerce programmatically:

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

The problem is that if in another update the attributes are changed then is not replacing the existing ones but the new ones are added too.

How I can remove the attributes that are not longer used, or remove all before inserting the new ones?

I tried calling this function in the beginning but didn't solve the problem: delete_post_meta($post_id, '_product_attributes');

1 Answers

you currently have set to append instead of overwrite as discribed in the wordpres developer docs

wp_set_post_terms( int $post_id, string|array $tags = '', string $taxonomy = 'post_tag', bool $append = false )

change your code as below by setting append to 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));
Related