0

Once we click on button present in Product view page, we are doing 2 things :

  1. Creating 2 images in folder media/custom_product_preview/quote/

  2. Creating new Product Programtically & uploading those 2 images to that Product

Issue : Only 1 image is uploading to Product , but we need both images.

if ($doSave) {
    $images = array(
        'image1' => 'image.png',
        'image2' => 'image2.png'
    );

    $imag_data = Mage::getSingleton('core/session')->getProductImage();
    $newImagePath = $imag_data;
    $io = new Varien_Io_File();

    foreach ($images as $imageType => $imageFileName) {
        $path = '';
        if ($newImagePath != "") {
            $dir = Mage::getBaseDir('media') . DS . 'custom_product_preview/quote/';
            $path = $dir . $newImagePath;
        } else {
        }

        if ($io->fileExists($path)) {
            try {
               $product->addImageToMediaGallery($path, array('image', 'thumbnail'), false);
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        } else {
            echo "Can not find image by path: `{$path}`<br/>";
        }
    }
}
$product->save();
  • what is $imag_data ? It gives you new Image ? – Keyur Shah Jun 6 '17 at 9:50
  • @KeyurShah you are right..... – user52996 Jun 6 '17 at 9:50
  • Every time gives you new Image ? – Keyur Shah Jun 6 '17 at 9:52
  • @KeyurShah yes, everytime when we upload image and click on custom button, everytime we are saving that image in folder..... [ we are saving 2 images everytime..... ] – user52996 Jun 6 '17 at 9:53
  • Where is the 2 Image name ? – Keyur Shah Jun 6 '17 at 9:56
0
if ($doSave) 
       {                            
            $images = array(                 
                'image1' => 'image.png',
                'image2' => 'image2.png'                    
            );

            $imag_data = Mage::getSingleton('core/session')->getProductImage();
            $newImagePath = $imag_data;

            foreach ($images as $imageType => $imageFileName) 
            {
                $path = '';
                if ($imageFileName != "") // Issue here
                {
                    $dir = Mage::getBaseDir('media') . DS . 'custom_product_preview/quote/';
                    $path = $dir . $imageFileName; // Issue here
                } 
                else {}                                  

                if (file_exists($path)) 
                { 
                    try 
                    {                          
                       $product->addImageToMediaGallery($path, array('image','thumbnail'), false);
                    } 
                    catch (Exception $e) 
                    {
                        echo $e->getMessage();
                    }
                } 
                else
                {
                    echo "Can not find image by path: `{$path}`<br/>";
                }
            }
        }
        $product->save();  
| improve this answer | |
  • sorry, it didt worked for me..... – user52996 Jun 6 '17 at 11:04
0

This is how I programattically add images to products as part of a 3rd party import

$product = Mage::getModel("catalog/product")->load($id);
$product
    ->setMediaGallery (array('images'=>array (), 'values'=>array ()))
    ->addImageToMediaGallery($target, array('image','thumbnail','small_image'), false, false)
    ->save();
| improve this answer | |

Your Answer

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