Where my mistake with woocommerce api custom option?

where is my mistake in a source with custom options? im working with woocommerce rest api. In foreach I need add different option like S – blue, M – red, S – red, M – blue, but I got empty inputs in wordpress:

enter image description here

Read More

there is my code and DOC:
http://woothemes.github.io/woocommerce-rest-api-docs/#create-a-product

public function addProduct($data)
    {
    $wc_api = $this->_getClient();

    // size
    $sizeArray = array();
    foreach($data['size'] as $size){
        $sizeArray[] = $size;
    }

    // color
    $colorArray = array();
    foreach($data['color'] as $color){
        $colorArray[] = $color;
    }

    foreach ($data['size'] as $size) {
        foreach ($data['color'] as $color) {
            $options[] = 
                [
                    'regular_price' => $data['price'],
                    'attributes'    => 
                        [ 
                            array('name' => 'Size', 'slug' => 'size', 'option' => $size),
                            array('name' => 'Color', 'slug' => 'color', 'option' => $color)
                        ]
                ];
        }
    }
    // http://woothemes.github.io/woocommerce-rest-api-docs/#create-a-product
    $newProductData = array(
        'product' => array(
            'title' => $data['title'],
            'type' => 'variable',
            'regular_price' => $data['price'],
            'description' => $data['description'],
            'sku' => $data['sku'],
            'tags' => [ $data['tags'] ],
            'attributes' =>
                [
                    array('name' => 'Size', 'slug' => 'size', 'position' => '0', 'visible' => true, 'variation' => true, 'options' => [ implode(' | ', $sizeArray) ]),
                    array('name' => 'Color', 'slug' => 'color', 'position' => '1', 'visible' => true, 'variation' => true, 'options' => [ implode(' | ', $colorArray) ])
                ],
            'variations' => $options,
            'images' => [ array('src' => $data['image'], 'position' => '0') ],
            'virtual' => true
        )
    );

    return $wc_api->create_product($newProductData);
}

Related posts

Leave a Reply

1 comment

  1. I had the same issue when i was trying to create a product specially a variable product. Finally got a solution for it.

    If your code is correct and its just not creating the variation then the solution i found is that you must use only one attribute ‘name’ or ‘slug’ with ‘option’ attribute kept as it is. Try using only ‘name’ and ‘option’ in the attributes or ‘slug’ and ‘option’.

    I am giving my code for as an example here.

    $child_prod_json = json_encode(
        array( 'product' =>
        array( 'title'           => $child_prod->product->title,
            'type'           => 'variable',
            'variations'         => array(array(
            'regular_price'      => $child_prod->product->regular_price,
            'sale_price'         => $child_prod->product->sale_price,
            'images'         => array( array( 'src'      => $child_prod->product->images[0]->src,
                'position'   => $child_prod->product->images[0]->position ) ),
            'attributes'    => array(array('name'=>$child_prod->product->attributes[0]->name,'option'=>$child_prod->product->attributes[0]->option)),
            'managing_stock'     => $child_prod->product->managing_stock,
            'in_stock'       => $child_prod->product->in_stock,
            'stock_quantity'     => $child_prod->product->stock_quantity,
            'shipping_class' => $shipping_class,
            'shipping_class_id'     =>$shipping_class_id,
        'purchase_note'     =>$child_prod->product->purchase_note,          
            ),)
        )  )    );
    

    For this example i am using only the first attribute i.e [0] position as an demo.

    My point is that- ‘name’ and ‘slug’ won’t work together. Use anyone of them. This solution worked for me. Do try lemme know whether it worked/useful for you.