0

How to insert image in magento product via url. Products are inserting but without images $urlToImage = 'image'; has the url of image. and How do i insert products option?

            <?php

            include_once("vendor.php");
            include_once("category.php");
            include_once("latest.php");


            $mageFilename = '../app/Mage.php';
            require_once $mageFilename;
            Mage::setIsDeveloperMode(true);
            ini_set('display_errors', 1);
            Mage::app();
            umask(0);

            $offerJsonURL    =  'http://affilnow.com/component/controller.php';
            $Products        =  'get_product_list_by_category_id_and_or_vendor_id';
            $Categories      =  'get_parent_categories';
            $Latest          =  'get_latest_products';

            $fk_= new vendor_data($affiliateID, $token);
            $fk_Categories= new category($affiliateID, $token);
            $fk_Latest = new latest($affiliateID,$token);


            $stores= vendor_data::getVendor($offerJsonURL, 'json');
            $categories= category::getCat($offerJsonURL, 'json');
            $latest = latest::getLatest($offerJsonURL, 'json');



            $product_id='product_id';
            $name='name';
            $description='description';
            $short_d='short_description';
            $quantity='quantity';
            $weight = 'weight';
            $rate ='price';
            $sku='sku';
            $tax='None';
            $visibility= 4;
            $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';
            $category ='categories';
            $options = 'options' ;
            $urlToImage = 'image';





               $i=1;
                foreach ($stores['data'][$Products] as $review) {
            $i++;
                 Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
                 $product = Mage::getModel("catalog/product");
                  try {
                     $product
                    ->setStoreId(0)
                    ->setWebsiteIds(array(1))
                    ->setAttributeSetId(4)
                    ->setTypeId('simple')
                    ->setSku($review[$sku])
                    ->setName($review[$name])
                    ->setWeight($review[$weight])
                    ->setTaxClassId('0')
                    ->setPrice($review[$rate])
                    ->setMetaTitle($review[$meta_title])
                    ->setMetaKeyword($review[$meta_keyword])
                    ->setMetaDescription($review[$meta_description])
                    ->setDescription($review[$description])
                    ->setShortDescription($review[$description])
                    ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                    ->setStockData(array(
                            'qty' => $review[$quantity],
                            'use_config_manage_stock' => 1, // use global config ?
                            'manage_stock'            => 0, // shoudl we manage stock or not?
                            'is_in_stock'             => 1,

                        )
                    )



                    ->setMediaGallery (array('images'=>array (), 'values'=>array ())) //media gallery initialization
                    ->addImageToMediaGallery($review[$urlToImage], array('image', 'small_image', 'thumbnail'), false, false)
                    ->setCategoryIds(array($review[$category]));

                   $product->save();
                   $err = curl_error($curl);
                  curl_close($curl);
                 }catch (Exception $e) {
                   Mage::log($e->getMessage());
               }

             }
             echo $i."Products upload";
1

I guess that the problem is because your images are coming as URL links ($review[$urlToImage]), and that's why you cannot import them directly.

Instead, I've added part of code which downloads your image into media/import directory, and then executes import process.

<?php
    include_once("vendor.php");
    include_once("category.php");
    include_once("latest.php");

    $mageFilename = '../app/Mage.php';
    require_once $mageFilename;
    Mage::setIsDeveloperMode(true);
    ini_set('display_errors', 1);
    Mage::app();
    umask(0);

    $offerJsonURL    =  'http://affilnow.com/component/controller.php';
    $Products        =  'get_product_list_by_category_id_and_or_vendor_id';
    $Categories      =  'get_parent_categories';
    $Latest          =  'get_latest_products';

    $fk_= new vendor_data($affiliateID, $token);
    $fk_Categories= new category($affiliateID, $token);
    $fk_Latest = new latest($affiliateID,$token);

    $stores= vendor_data::getVendor($offerJsonURL, 'json');
    $categories= category::getCat($offerJsonURL, 'json');
    $latest = latest::getLatest($offerJsonURL, 'json');

    $product_id='product_id';
    $name='name';
    $description='description';
    $short_d='short_description';
    $quantity='quantity';
    $weight = 'weight';
    $rate ='price';
    $sku='sku';
    $tax='None';
    $visibility= 4;
    $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';
    $category ='categories';
    $options = 'options' ;
    $urlToImage = 'image';

    $i=1;
    foreach ($stores['data'][$Products] as $review) {
        $i++;

        $image_url  = $review[$urlToImage];
        $image_type = substr(strrchr($image_url,"."),1);
        $filename   = md5($image_url . $review[$sku]).'.'.$image_type;
        $filepath   = Mage::getBaseDir('media') . DS . 'import'. DS . $filename;
        file_put_contents($filepath, file_get_contents(trim($image_url)));

        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
        $product = Mage::getModel("catalog/product");
        try {
            $product
            ->setStoreId(0)
            ->setWebsiteIds(array(1))
            ->setAttributeSetId(4)
            ->setTypeId('simple')
            ->setSku($review[$sku])
            ->setName($review[$name])
            ->setWeight($review[$weight])
            ->setTaxClassId('0')
            ->setPrice($review[$rate])
            ->setMetaTitle($review[$meta_title])
            ->setMetaKeyword($review[$meta_keyword])
            ->setMetaDescription($review[$meta_description])
            ->setDescription($review[$description])
            ->setShortDescription($review[$description])
            ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
            ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
            ->setStockData(array(
                    'qty' => $review[$quantity],
                    'use_config_manage_stock' => 1, // use global config ?
                    'manage_stock'            => 0, // shoudl we manage stock or not?
                    'is_in_stock'             => 1,

                )
            )
            ->setMediaGallery (array('images'=>array (), 'values'=>array ())) //media gallery initialization
            ->addImageToMediaGallery($filepath, array('image', 'small_image', 'thumbnail'), false, false)
            ->setCategoryIds(array($review[$category]));

           $product->save();
           $err = curl_error($curl);
           curl_close($curl);
        } catch (Exception $e) {
            Mage::log($e->getMessage());
        }

    }
    echo $i."Products upload";
1

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.