Magento – magento 2 Rest api to add simple and bundle products

bundled-productmagento-2.1restsimple-product

I am using rest API to add simple and bundle product

Please see the below code

$productData = '{
  "product": {
      "sku": "TESTPRD002",
      "name": "Women`s Running - Pure Boost X Shoes",
      "attribute_set_id": 15,
      "price": 84,
      "status": 1,
      "visibility": 4,
      "type_id": "simple",
      "created_at": "2016-12-16 15:20:55",
      "updated_at": "2016-12-16 15:20:23",
      "weight": 1,
      "extension_attributes": {
        "stock_item": {
          "item_id": 1,
          "stock_id": 1,
          "qty": 20,
          "is_in_stock": true,
          "is_qty_decimal": false
        }
      },
      "product_links": [],
      "options": [],
      "tier_prices": [],
      "custom_attributes": [
        {
          "attribute_code": "description",
          "value": "<p>Lightweight and sleek, these women`s running shoes are fueled by boost™ energy. The low-profile runners blend an energy-returning boost™ midsole with a STRETCHWEB outsole for a cushioned ride with terrific ground-feel. They feature a breathable mesh upper with a sock-like fit that offers all-around support. With a full boost™ midsole that keeps every stride charged with light, fast energy, the shoe has an upper that hovers over a free-floating arch.</p>"
        },
        {
          "attribute_code": "short_description",
          "value": "<p>PURE BOOST X SHOES</p><p>NATURAL RUNNING SHOES WITH ARCH SUPPORT.</p>"
        },
        {
          "attribute_code": "meta_title",
          "value": "PURE BOOST X SHOES"
        },
        {
          "attribute_code": "meta_keyword",
          "value": "boost X, running, shoes, adidas"
        },
        {
          "attribute_code": "meta_description",
          "value": "NATURAL RUNNING SHOES WITH ARCH SUPPORT."
        },
        {
          "attribute_code": "category_ids",
          "value": [
            "44"
          ]
        },
        {
          "attribute_code": "url_key",
          "value": "womens-running-pure-boost-x-shoes"
        },
        {
          "attribute_code": "tax_class_id",
          "value": "1"
        },
        {
          "attribute_code": "remarks",
          "value": "Lorem ipsum.."
        }
      ]
  },
  "saveOptions": true
}';
//echo $productData;
$product_url ='http://local.marketplace-release.com/marketplace/rest/V1/products/';

$setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $product_url);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

Result : "{"message":"Unable to save product"}"

Please help me..

Best Answer

Here's the solution for a bundle, quite complicated. It is based on Magento2 (I see different tags, but I think, according to the error message, that you want it for Magento2).

Let's assume that our bundle is identified by the SKU "bundle01" and ID 123.

First of all, let's take some information from the product:

GET /products/bundle01?searchCriteria

and take note of the "extension_attributes" --> "bundle_product_options" section, particularly to the option_idfields related to your products. And take not of all the product_links --> id values related to the product.

Let's assume that we have 3 products with option_id 643, 644, 645 and id 704,705,706 respectively.

With all these information, here's the body to add a bundle product to your cart:

{
"cart_item": {
    "quote_id": <quote_id>,
    "sku": "bundle-01",
    "qty": 1,
    "product_option": {
    "extension_attributes": {
        "bundle_options": [{
            "option_id": 643,
            "option_qty": 1,
            "option_selections": [704]
            }, {
            "option_id": 644,
            "option_qty": 1,
            "option_selections": [705]
            }, {
            "option_id": 645,
            "option_qty": 1,
            "option_selections": [706]
            }]
        }
    }
}
}
Related Topic