0

I'm getting product data from different store via curl call.Now i want insert all the product in my magento store. But only 3 products are inserting than i'm getting this error Uncaught exception 'Exception' with message 'Warning: Invalid argument supplied for foreach(). Any help will be appreciated Thanks

<?php
   $mageFilename = '../app/Mage.php';
    require_once $mageFilename;

 Mage::setIsDeveloperMode(true);      
  ini_set('display_errors', 1);
   Mage::app();
   umask(0);

    $curl = curl_init();

    $API_ID          = "4";
    $API_URL         = "http://magento.com/component/controller.php";
    $App_key         = "as";
    $Functionname    ='get_product_list_by_category_id_and_or_vendor_id';
    $data            = 'data';


    $product_id='product_id';
    $name='name';
    $description='description';
    $short_d='short_description';
    $quantity='quantity';
    $weight = 'weight';
    $rate ='price';
    $sku='sku';
    $tax='tax_class_id';
    $visibility = 'Catlog, Search';
    $status='status';
    $meta_title='meta_title';
    $meta_description='meta_description';
    $meta_keyword='meta_keyword';
    $tag='tag';
    $model='model';
    $upc='upc';
    $ean='ean';
    $jan='jan';
    $isbn='isbn';
    $mpn='mpn';
    $location='location';
    $image='image';
    $stock='stock_status';





    curl_setopt_array($curl, array(
        CURLOPT_URL => $API_URL,
        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: 4",
            "Appkey: as",
            "Cache-control: no-cache",
            "Categoryid: 101,102,103,104",
            "Functionname: get_product_list_by_category_id_and_or_vendor_id",
            "Start: 0",
            "Limit: 2",
            "Postman-token: 7e0cbcb4-ad69-7f38-de10-f2387f8477ee",
            "Vendorid: 1474614118"
        ),
    ));
    $response = curl_exec($curl);
    $data_res = json_decode($response,true);
      //  echo $data_res;


       foreach($data_res[$data['get_product_list_by_category_id_and_or_vendor_id'] as $key){
           // echo $key[$product_id];

        $product = Mage::getModel("catalog/product");
        $product 
        // ->setStoreId(0)
         // ->setWebsiteIds(array(1))
          ->setAttributeSetId(4)
         ->setTypeId('simple')
          //->setCreatedAt(strtotime('now'))
          ->setSku($key[$sku])
         ->setName($key[$name])
          ->setWeight($key[$weight])
            ->setStatus($key[$status])
            ->setTaxClassId($key[$tax])           ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
          // ->setManufacturer(28)
           //->setColor(24)
           //->setNewsFromDate('06/26/2014')
         //->setNewsToDate('06/30/2016')
          //->setCountryOfManufacture('AF')
          ->setPrice($key[$rate])
           //->setCost(22.33)
           //->setSpecialPrice(00.44)
          //->setSpecialFromDate('06/1/2015')
            //->setSpecialToDate('06/30/2016')
          //->setMsrpEnabled(1)
             //->setMsrpDisplayActualPriceType(1)
         //->setMsrp(99.99)
            ->setMetaTitle($key[$meta_title]) 
         ->setMetaKeyword($key[$meta_keyword])
            ->setMetaDescription($key[$meta_description])
           ->setDescription($key[$meta_description])
           ->setShortDescription($key[$short_d])

  //    ->setMediaGallery (array('images'=>array (), 'values'=>array ()))
   //    ->addImageToMediaGallery('media/catalog/product/1/0/10243-1.png', array('image','thumbnail','small_image'), false, false)

   ->setStockData(array(
    'use_config_manage_stock' => 0,
    'manage_stock'=>1,
    'min_sale_qty'=>1,
    'max_sale_qty'=>2,
    'is_in_stock' => 1,
    'qty' => 999
    )
  )

       ->setCategoryIds(array(3, 7));
        $product->save();
           $err = curl_error($curl);
        curl_close($curl);



    }
1

your code is proper. You need to debug the following section,

$response = curl_exec($curl);
$data_res = json_decode($response,true);
foreach($data_res[$data['get_product_list_by_category_id_and_or_vendor_id'] as $key){
2
  • i debugged the section that you pointed out. but still i didnt get any solution. – rameez.hashmi Feb 28 '17 at 11:06
  • Are you sure you get value from $data_res[$data['get_product_list_by_category_id_and_or_vendor_id'] and $key[$sku]? – Tonmoy Mar 1 '17 at 9:54
0

There is a closing bracket missing after $data['...'] in this line:

foreach($data_res[$data['get_product_list_by_category_id_and_or_vendor_id'] as $key){

also make sure $data['get_product_list_by_category_id_and_or_vendor_id'] is set eg.

if(isset($data['get_product_list_by_category_id_and_or_vendor_id'])){
     //YOUR CODE
}

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.