First things first: I have a very specific question that has surely NOT been asked yet.

I am developing a plugin where I can create and update Woocommerce products. The creation process of the product seems to go very well except for the part of creating and updating the variations. I have been searching all over the internet, stackoverflow and other blogs/sites but abssolutely no one has ever mentioned anything about this problem or even an example of creating a certain amount of variations.

The Situation

I am creating multiple variations to add to a product. My plugin UI is built using Javascript. To create multiple variations I'm using an array of variation data such as:

var variations = [{
    id: 21,
    attributes: [{key: 'Color', value: 'White'}, {key: 'Size', value: 'Large'}],
    sku: 'sample1234',
    weight: 10,
    stock_quantity: 20,
    regular_price: '25',
    sale_price: '22'

Now on the server I have this PHP:

$variations = $data['variations']; //<-- My variations JS array

foreach( $variations as $variation ) {
    $wc_variation = null;
    if($data['is_new_product'){ // <-- also in JS object but from the product
        $wc_variation = new \WC_Product_Variation();
    } else {
        $wc_variation = new \WC_Product_Variation(intval( $variation['id'] ));

    $variation_attributes = [];
    foreach( $variation['attributes'] as $attribute ) {
        $variation_attributes[ $attribute['key'] ] = $attribute['value'];
    /* So the outcome should be: $variation_attributes = ['Color' => 'White', 'Size' => 'Large']; */
    $wc_variation->set_attributes( $variation_attributes );

    $wc_variation->set_sku( $variation['sku'] );

The Problem

Now guess what? The variation saves other data perfectly except for the attributes. But when I select the attributes from Woocommerce itself they are actually saved. I do not get any errors in WP_Debug log only a warning like:

PHP Warning: rawurldecode() expects parameter 1 to be string, array given in ...\plugins\woocommerce\includes\wc-product-functions.php on line 413


The Automattic\WooCommerce\Admin\API\Options::get_options function is deprecated since version 3.1.

Maybe it has something to do with it but I would not even know why or how.

The Question

My question is how do I make Woocommerce save the variation attributes aswell correctly and without problems plus being able to update them without problems?

New contributor
Leander is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • You'll need to ask WooCommerce support about this; third-party plugin support is off topic here.
    – Pat J
    Jun 23 at 14:59


Browse other questions tagged or ask your own question.