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

4
  • Sorry I'm working on Magento 2
    – Jackson
    Mar 16, 2017 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, 2017 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. Mar 17, 2017 at 5:11
  • @NehaRaval please check edit part in question...... Mar 17, 2017 at 6:53

1 Answer 1

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/';
5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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