Home  >  Q&A  >  body text

Add attribute on save if product is not set: WooCommerce

When saving a product, I want to check if the product has a specific attribute. In my case, pa_region. If not, I want to add attribute sets and attribute terms to the product. If the property pa_region is already set, I don't want to update/change it.

I see there is a function (documentation) called wp_set_object_terms. I tried a few ways but I think update_post_meta is the right way.

From this answer I know how to check if a product has an attribute. I'll add that check later.

Currently I try to add the attribute first. Not working properly yet.

I found a similar question here and I tried to use that code to achieve my purpose. But this doesn't work. I guess the reason is that the feature requires properties that are already in the product? ! Edit: I checked. Even though the property pa_region is set in the product, the code does not update its value.

This is my current code:

add_action('woocommerce_update_product', 'save_product_region');
function save_product_region( $post )
{
    if( in_array( $post->post_type, array( 'product' ) ) ){

        $test = 'test';
        $product_id = $post->ID;

        $product_attributes = get_post_meta( $product_id ,'_product_attributes', true);
        var_dump($product_attributes);

        // Loop through product attributes
        foreach( $product_attributes as $attribute => $attribute_data ) {
            // Target specif attribute  by its name
            if( 'pa_region' === $attribute_data['name'] ) {
                // Set the new value in the array
                $product_attributes[$attribute]['value'] = $test;
                break; // stop the loop
            }
        }

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

    }
}

P粉555682718P粉555682718285 days ago451

reply all(1)I'll reply

  • P粉520204081

    P粉5202040812023-12-14 10:05:21

    The first $post is not an object. will return the ID, which is good.

    add_action('woocommerce_update_product', 'save_product_region');
    function save_product_region( $product_id ) {
    
        //Get product object from the ID
        $_product = wc_get_product($product_id);
        $attributes = $_product->get_attributes();
    
        $add_option = wp_set_object_terms( $product_id, 'canada', 'pa_region', true );
        $curr_options = $attributes['pa_region']['options'];
        
        //Check if we have this attribute set already 
        if(!in_array($add_option,$curr_options)):
            $updated_options = array_push($curr_options,$add_option);
            $data = array(
                'pa_region' => array(
                    'name'=>'pa_region',
                    'options'=> $updated_options,
                    'is_visible' => '1',
                    'is_variation' => '0',
                    'is_taxonomy' => '1'
                )
            );
            //First getting the Post Meta
            $_product_attributes = get_post_meta($product_id, '_product_attributes', TRUE);
            //Updating the Post Meta
            update_post_meta($product_id, '_product_attributes', array_merge($_product_attributes, $data));
        endif;
    }

    reply
    0
  • Cancelreply