0

I'm getting product data through an api call and now i want to store this data in my magento store? can anyone help me out to store without duplication

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://magento.com/component/controller.php",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "appid: 9",
    "appkey: 0325615546",
    "cache-control: no-cache",
    "data: {   \"1474614118_1\": [ \"147461411870027\",\"147461411870027\"]    }",
"functionname: get_products_by_ids",
"postman-token: fd83165f-51e2-4e26-c15f-df5aba3250d7"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Respose:

"data": {
    "get_products_by_ids": [
      {
        "product_id": 0,
        "name": "Fresco 2 pc",
        "description": "Light Weight  Cotton Blend Fabric 2 Pc. White color kameez with round neck, full sleeves, embroidery on the front paired with shalwar.",
        "meta_title": "Fresco 2 pc",
        "meta_description": "",
        "meta_keyword": "",
        "tag": "1474614118",
        "model": "E02516-WHT",
        "sku": "1474614118E02516-WHT",
        "upc": "1474614118",
        "ean": "1474614118",
        "jan": "1474614118",
        "isbn": "",
        "mpn": "1474614118",
        "location": "",
        "quantity": "0",
        "stock_status": "Out Of Stock",
        "image": "http://www.wearego.com/image/catalog/eid2/E02516-WHT.JPG",
        "manufacturer_id": null,
        "manufacturer": null,
        "price": "3950.0000",
        "special": "1580.0000",
        "reward": null,
        "points": "0",
        "tax_class_id": "14746141180",
        "date_available": "2016-09-08",
        "weight": "0.50000000",
        "weight_class_id": "14746141181",
        "length": "0.00000000",
        "width": "0.00000000",
        "height": "0.00000000",
        "length_class_id": "14746141181",
        "subtract": "1",
        "rating": 0,
        "reviews": 0,
        "minimum": "1",
        "sort_order": "1",
        "status": "1",
        "date_added": "2016-09-08 23:03:57",
        "date_modified": "2016-09-08 23:03:57",
        "viewed": "3899",
        "vendor_id": "1474614118",
        "addtionalimages": [
            {
                "product_image_id": "14746141184247",
                "product_id": "147461411870027",
                "image": "http://www.wearego.com/image/catalog/eid2/E02516-WHT-01.JPG"
            },
            {
                "product_image_id": "14746141184248",
                "product_id": "147461411870027",
                "image": "http://www.wearego.com/image/catalog/eid2/E02516-WHT-02.JPG"
            }
        ],
        "options": [],
        "categories": []
      }
]

}

  • Depends on the output of $response, but basically you can just create (or load) a product and save the data using the set methods. Could you update the question and add the content of $response? – Arjen Miedema Feb 24 '17 at 6:49
  • @ArjenMiedema i have now added content of response – rameez.hashmi Feb 24 '17 at 6:59
1

Saving this data into a product is pretty easy, although setting the values needs your own interpretation. Magento uses the setData method to set the product data.

So it should look like something like this:

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://magento.com/component/controller.php",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "appid: 9",
    "appkey: 0325615546",
    "cache-control: no-cache",
    "data: {
        "1474614118_1": [ 
            "147461411870027",
            "147461411870027"
        ]
    }",
    "functionname: get_products_by_ids",
    "postman-token: fd83165f-51e2-4e26-c15f-df5aba3250d7"
  )
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  $data = json_decode($response);

  foreach ($data->get_products_by_ids as $item) {
      $product = Mage::getModel('catalog/product');

      if ($item->product_id) {
          $product->load($item->product_id);
      }

      $product->setSku($item->sku)
          ->setEan($item->ean)
          // Continue setting all other attribute values
          ->save();

      $stockItem = $product->getStockItem();
      $stockItem->setQty($item->quantity)
          ->setIsInStock($item->quantity > 0)
          ->save();
  }
}

It might be clear that this is not your total solution, but it should get you into the right direction. Also, I haven't tested this code myself, so it might need some tweaks here and there to get it to work properly for you.

  • Is this how should i do ? – rameez.hashmi Feb 24 '17 at 10:15
  • $product->setSku($item->sku) //->setEan($item->ean) ->setProductID($item->product_id) ->setName($item->name) ->setDescription($item->description) ->setKeyword($item->keyword) ->setPrice($item->Price) ->setQtr($item->Quantity) ->setS_descriotion($item->S_description) ->setStatus($item->Status) ->setVisibility($item->Visibility) ->SetWeight($item->Weight) // Continue setting all other attribute values ->save() – rameez.hashmi Feb 24 '17 at 10:15
  • @rameez.hashmi did you test this code? – Arjen Miedema Feb 24 '17 at 10:20
  • yes i'm getting error. Invalid argument supplied for foreach() – rameez.hashmi Feb 24 '17 at 10:27
  • @rameez.hashmi have you done some debugging yet? This error means that the data I use for the foreach loop is incorrect. You need to find out what you should use. – Arjen Miedema Mar 2 '17 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.