2

Before anything, I'd like to say that I have thoroughly searched all over internet to find as much help as I could into solving this particular issue.

I have tried many different ways of creating a variation, in particular this one which also didn't seem to bind the label to the field next to the variation ID on my product page. The outcome was the same as the method I've written with the helper class. I still have it commented somewhere so if you happen to know a solution for this specific way of creating a variation, hit me up!


Some context: I have a csv file with a lot of information on my products. I wrote a script to import them into WooCommerce programmatically using the WC_Product_Simple helper class and a stripped down version of the original csv (with only two items), and everything worked perfectly for the two products. Item #2 is flour, so I figured I'd try creating variations for it instead of creating multiple simple products (for instance Flour (100Gr), Flour (500Gr) and Flour (1Kg)).

I was able to create an attribute for my WC_Product_Variable, set its variation field to true, create three variations (100Gr, 500Gr and 1Kg) and save them. When I look at my Products page in WooCommerce, I can see the three variations under my variable product.

The issue I'm having is that I can't bind a particuliar field to one variation, no matter how hard I try. I have no idea how this field is called, tho I've noticed that setting it reads/writes the post_excerpt column in my {$wpdb->prefix}posts table.

Here is what is displayed:

My variations are correctly created, tho the default value "Weight..." is selected

And here is what I want to achieve programmatically:

This field is what I want to edit

Here's the code:

/**
 * At this point, the product is correctly created and available through $wc_product.
 * Its id is stored in $product_id.
 * I also have an array with information from my product called $product. 
 * This is where the information is stored to create said product.
 * You don't have to worry about the creation of the product itself.
 */

// I have stored as a boolean whether my product had variations or not.
if ($variable) {

    // Weight => Label
    $weights = array(
        '100' => '100Gr',
        '500' => '500Gr',
        '1000' => '1Kg'
    );

    $attribute = new WC_Product_Attribute();
    $attribute->set_id(0);
    $attribute->set_name('Weight');
    $attribute->set_options(
        $weights
    );
    $attribute->set_position(0);
    $attribute->set_visible(true);
    $attribute->set_variation(true);
    $wc_product->set_attributes(array($attribute));
    $wc_product->save();

    // At this point my attribute is created. There is no save method for WC_Product_Attribute.
    // I'm 100% sure that everything up to this point works as expected, I've left it here
    // for clarity and for better understanding what the code does.

    foreach ($weights as $weight=>$label) {
        // Each variation will be named after the product sku followed by its label.
        // For instance: sku5436-100Gr where sku5436 is the variable product.
        $variation_sku = "{$product["sku"]}-{$label}";

        // Will uncomment once this works!
        // These two lines make sure that each variation is unique through their sku.
        // $var_id = wc_get_product_id_by_sku($variation_sku);
        // $variation = $var_id?new WC_Product_Variation($var_id):new WC_Product_Variation();

        /**
         * I create my variation,
         * I set its parent id,
         * I set its sku,
         * I calculate its price with a simple weight/price formula,
         * I set its price
         * 
         * And here's the part I'm not too sure about
         * I thought "set_attributes" would select between 100Gr, 500Gr and 1Kg
         * but it looks like it doesn't do anything. I could just remove that line
         * and nothing would change.
         */
        $variation = new WC_Product_Variation();
        $variation->set_parent_id($product_id);
        $variation->set_sku($variation_sku);
        $variation_price = ($weight / 1000) * $product["kg_price"];
        $variation->set_regular_price($variation_price);

        // This is the line I thought would bind the label to the field, but it doesn't
        $variation->set_attributes(array("Weight" => $label));


        $id_var = $variation->save();

        // I tried manually setting the post excerpt. The update works, but since
        // it wasn't set through the helper class, I'm assuming some cache
        // thing isn't correctly binding the label to the field which I still don't know the name
        // global $wpdb;
        // $wpdb->update($wpdb->prefix . "posts", array("post_excerpt" => $label), array("ID" => $id_var));
    }
}

In short: I'd like to bind the fields next to the post ID (see screenshots) to the correct variation. My code does almost everything except that.

I would also really appreciate if someone could tell me the name of this field. The closest thing I've found is that it is bound to the post_excerpt.

0 Answers0