5

Question : Once Registered user upload custom image [custom image is Image which is uploaded by User ] in product view page & click on button "Save design" , we are creating Product Programatically . We want to upload that custom image to newly created product programatically.

Now we are using below code to upload existing image [ already saved image in server ] programatically.

protected function _createProduct($type, $doSave=true, $originalProduct) 
    {

        $product = Mage::getModel('catalog/product');

        // finally set custom data
        $product->setName($originalProduct->getName()); 

        // add product images

        $images = array(
            'thumbnail'   => 'image.png',
            'small_image' => 'image.png',
            'image'       => 'image.png',
        ); 

        $dir = Mage::getBaseDir('media') . DS . 'example/amasty/';

        foreach ($images as $imageType => $imageFileName) {
            $path = $dir . $imageFileName;
            if (file_exists($path)) {
                try {
                    $product->addImageToMediaGallery($path, $imageType, false);
                } catch (Exception $e) {
                    echo $e->getMessage();
                }
            } else {
                echo "Can not find image by path: `{$path}`<br/>";
            }
        }

        if ($doSave)
            $product->save();
        // To save inside my_designs

        $data = array('title'=>fav1_id,'product_id'=>$product->getId(),'customer_id'=> $customerId, 'value'=>image);
        $model = Mage::getModel('example/mydesign')->setData($data);
      try 
         {
        $fav1_id = $model->save()->getId();
        echo "Custom Design saved successfully";
          } catch (Exception $e)
          {
            echo $e->getMessage();   
          }
        // To save inside my_designs end

        return $product;
    }

Sidenote : Once Registered user upload custom image in product view page & click on Add to cart, we are saving Custom image in Server in path : media/custom_options/quote and displaying image path in Table : sales_flat_quote_item_options

  • Sorry I'm working on Magento 2 – Ankit Shah Mar 16 '17 at 7:58
  • The code you provided does not include code to upload for, I think it's just to add button. – Neha Raval Mar 17 '17 at 4:22
  • @NehaRaval code in question will create product programtically & upload image which is already present in server. but i need to upload custom image to the programtically created product. – Baby in Magento Mar 17 '17 at 5:11
  • @NehaRaval please check edit part in question...... – Baby in Magento Mar 17 '17 at 6:53
2

I think error in below line

instead of

  $dir = Mage::getBaseDir('media') . DS . 'example/amasty/';

Try below on

  $dir = Mage::getBaseDir('media') . DS . 'custom_options/quote/';

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.