4

I am trying to create a Wordpress plugin that creates variable products programmatically, but the problem is, I would like to use attributes I have previously defined manually from the admin dashboard. I need to assign the attribute to the the product I'm creating.

Here is the code that I am using (not mine, I got it from this answer: Create programmatically a variable product and two new attributes in WooCommerce):

function addProduct(){
    //Create main product
    $product = new WC_Product_Variable();

    //Create the attribute object
    $attribute = new WC_Product_Attribute();

    //pa_size tax id
    $attribute->set_id( 0 ); // -> SET to 0

    //pa_size slug
    $attribute->set_name( 'Couleur' ); // -> removed 'pa_' prefix

    //Set terms slugs
    $attribute->set_options( array(
        'Noir'
    ) );

    $attribute->set_position( 0 );

    //If enabled
    $attribute->set_visible( 1 );

    //If we are going to use attribute in order to generate variations
    $attribute->set_variation( 1 );

    $product->set_attributes(array($attribute));

    //Save main product to get its id
    $id = $product->save();

    $variation = new WC_Product_Variation();
    $variation->set_regular_price(10);
    $variation->set_parent_id($id);

    //Set attributes requires a key/value containing
    // tax and term slug
    $variation->set_attributes(array(
        'Couleur' => 'Noir' // -> removed 'pa_' prefix
    ));

    //Save variation, returns variation id
    echo get_permalink ( $variation->save() );


    // echo get_permalink( $id ); // -> returns a link to check the newly created product   
}

The product is correctly created, but the code creates a new attribute and calls it "Couleur" instead of using my already defined "Couleur" attribute.

Thanks in advance.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 1
    Hello, mind explaining a little bit more, please? I could not figure out what to do with the function mentioned. Thank you for your response, much appreciated! – Ahmed Zeghibi Apr 23 '21 at 09:41

2 Answers2

3

Thanks to Vincenzo Di Gaetano, the problem was solved.

Here's the working code:

function add_product(){
    //Create main product
    $product = new WC_Product_Variable();

    //Create the attribute object
    $attribute = new WC_Product_Attribute();

    // get a product attribute ID by name.
    $attribute_id = wc_attribute_taxonomy_id_by_name( 'Couleur' );
    $attribute->set_id( $attribute_id );

    //pa_size slug
    $attribute->set_name( 'pa_couleur' ); // -> removed 'pa_' prefix

    //Set terms slugs
    $attribute->set_options( array(
        'noir'
    ) );

    $attribute->set_position( 0 );

    //If enabled
    $attribute->set_visible( 1 );

    //If we are going to use attribute in order to generate variations
    $attribute->set_variation( 1 );

    $product->set_attributes(array($attribute));

    //Save main product to get its id
    $id = $product->save();

    $variation = new WC_Product_Variation();
    $variation->set_regular_price(10);
    $variation->set_parent_id($id);

    //Set attributes requires a key/value containing
    // tax and term slug
    $variation->set_attributes(array(
        'pa_couleur' => 'noir' // -> removed 'pa_' prefix
    ));

    //Save variation, returns variation id
    echo get_permalink ( $variation->save() );


    // echo get_permalink( $id ); // -> returns a link to check the newly created product   
}
2

WooCommerce allows you to use global attributes and product-level (local or custom) attributes.

Global attributes are available for all products. When you delete a term for that attribute, it will be removed for all products that use that attribute with that term. Conversely, when you add a new term it will be available for all other products.

Global attributes you will find them in Wordpress Dashboard > Products > Attributes.

While the product-level (local or custom) attribute is only defined for that product and will not be available for others.

The product-level attributes will only be found within the product edit page in the "Attributes" section.

You can find more detailed information here:

SOLUTION

Programmatically you will need to add the prefix pa_ to each attribute slug.

Also you are setting the id to zero: $attribute->set_id( 0 ); but by doing this you are setting a product-level (and not global) attribute. You will need to change this line to:

// get a product attribute ID by name.
$attribute_id = wc_attribute_taxonomy_id_by_name( 'Couleur' );
$attribute->set_id( $attribute_id );

Instead of using this answer you should use this.

USEFUL LINK

Vincenzo Di Gaetano
  • 3,892
  • 3
  • 13
  • 32