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
    Commented 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
    Commented 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. Commented Mar 17, 2017 at 5:11
  • @NehaRaval please check edit part in question...... Commented 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
  • Hi plz come here once bro chat.stackexchange.com/rooms/55514/teja-bhagavan Commented Mar 17, 2017 at 4:24
  • @TejabhagavanKollepara I dont have rights to message in that chatroom Commented Mar 17, 2017 at 4:29
  • check once now. Commented Mar 17, 2017 at 4:33
  • @MurtuzaZabuawala Thanks, after click on save design i need to upload custom image to the programtically created product, for that i need to save custom image in server , but once i click on save design custom image is not saving in server. Commented Mar 17, 2017 at 5:14
  • please check edit part in question...... Commented Mar 17, 2017 at 6:53

Your Answer

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

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