Home > Enterprise >  WooCommerce: Add attribute to product on save if it's not set
WooCommerce: Add attribute to product on save if it's not set

Time:07-02

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

I saw that there is a function called wp_set_object_terms (Docs). I tried a few things with it but I think, that update_post_meta is the correct way to do it.

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

At the moment I try to add the attribute in the first place. For now it's not working.

I found a similar question here and I tried to use the code for my purpose. But it doesn't work. I guess the reason is that the function needs the attribute already in the product?! Edit: I checked. Even if the attribute pa_region is set in the product, the code doesn't update the value of it.

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

    }
}

CodePudding user response:

First $post is not object. Will return ID which is fine.

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;
}
  • Related