Lubo Masura
Lubo Masura

Reputation: 1053

Woocommerce - Set default variation with PHP and save this value into database

I have this code which adds the product default variation automatically.

/*xml extension*/
/*
 * Add default attributes to a product
 */
function artbuild_default_product_attributes()
{
    global $product;
    if (! $product) {
        $product = $GLOBALS['product_object'];
    }
    if (! $product) {
        return;
    }
    $attributes = $product->get_attributes();
    
    $defaultAttributes = array(

    'pa_velkost',
    'pa_farba',
    'pa_material',
    );
    
    $changed=false;
    foreach ($defaultAttributes as $key){
        if (! isset($attributes[$key])){
            $newattr = artbuild_make_product_attribute($key);
            if ($newattr){
                $attributes[$key] = $newattr;
            }
            $changed = true;
        }
    }
    if ($changed){
        $product->set_attributes($attributes);
    }
}
/*
 * added to last hook before rendering of Product Edit screen
 */
add_action('woocommerce_product_write_panel_tabs', 'artbuild_default_product_attributes');

What do I need is to save the default variation into the database. Because when you turn of the function it comes back into not set value, so its not probably being saved.

I need this to work properly because of the XML feed, where I get the only default variation products. With this code it behaive like there is no default variation set in the whole store, even when it is acting like there is, so my feed is empty. Once I save products default variations manually, it works like I need. Do you have any ideas how to make it work?

Thank you in advance.

Upvotes: 0

Views: 647

Answers (1)

Naveen Chand K
Naveen Chand K

Reputation: 426

Your code is getting executed when the Product Edit Screen opens, because you are using this hook: woocommerce_product_write_panel_tabs.

You should use the hook that executes your code after the product is saved. There is a hook for that: woocommerce_admin_process_product_object since WooCommerce 3.0

Here is the function and hook to save default attributes to database:

function myfunction_save_product_meta( $product ) {
    
    $attributes = $product->get_attributes();
    
    $defaultAttributes = array(
    'pa_velkost',
    'pa_farba',
    'pa_material',
    );
    
    $changed=false;
    foreach ($defaultAttributes as $key){
        if (! isset($attributes[$key])){
            $newattr = artbuild_make_product_attribute($key);
            if ($newattr){
                $attributes[$key] = $newattr;
            }
            $changed = true;
        }
    }
    
    $product->set_default_attributes( $attributes ); // This should set default attributes

}
add_action( 'woocommerce_admin_process_product_object', 'myfunction_save_product_meta' );

I am not sure what is the code in your function artbuild_make_product_attribute but whatever it is, it should return true for attributes array to be generated.

Setting default attributes is taken care of by:

$product->set_default_attributes( $attributes ); 

Upvotes: 1

Related Questions