1

I am trying below code to create product programtically , all is well, but if guest create product, than custom image not uploading

Can someone show me the right way ?

protected function _thisProduct($type, $doSave=true, $originalProduct, $newImagePath="") 
        {

            // code for Guest

             $session = Mage::getSingleton('customer/session');

            if ($session->isLoggedIn()) {
                return;
            }

            $result = array(
                'success' => false
            ); 

         if ($this->getRequest()->isPost()) {
                $login = $this->getRequest()->getPost('login');
                $productId=$this->getRequest()->getPost('product_id');
               // echo $login['product_id'];die;
                if (!empty($login['username']) && !empty($login['password'])) {
                    try {

                          $session->login($login['username'], $login['password']);
                        $result['redirect'] = $this->_getRefererUrl() ? $this->_getRefererUrl() : Mage::getUrl('customer/account', array('_secure' => true));
                        $result['success'] = true;
                        $customerId = Mage::getSingleton('customer/session')->getCustomerId();

                // code for guest end       

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

            //  custom product images

            $images = array(
                'thumbnail'   => 'image.png', // displaying under cart page
                //'small_image' => 'image.png',
                'image'       => 'image.png',  // displaying under my design
            ); 

            foreach ($images as $imageType => $imageFileName) {
                if($newImagePath != ""){
                    $dir = Mage::getBaseDir('media') . DS . 'one_two_three/quote/';
                    $path = $dir . $newImagePath;
                }else{
                    $dir = Mage::getBaseDir('media') . DS . 'example/super/';
                    $path = $dir . $imageFileName;
                }
                //echo $path."<br>";
                if (file_exists($path)) {
                    try {
                        $product->addImageToMediaGallery($path, $imageType, false);
                    } catch (Exception $e) {
                        echo $e->getMessage();
                    }
                } else {
                    echo "No image path found : `{$path}`<br/>";
                }
            }

            $customerId = Mage::getSingleton('customer/session')->getCustomerId();
           $product->setCreatedByCustomerId($customerId);

            if ($doSave)
                $product->save();

            return $product;

            // code for guest users

            } catch (Mage_Core_Exception $e) {
                        switch ($e->getCode()) {
                            case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
                                $message = Mage::helper('customer')->__('This account is not confirmed.username');
                                break;
                            case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
                                $message = $e->getMessage();
                                break;
                            default:
                                $message = $e->getMessage();
                        }
                        $result['error'] = $message;
                        $session->setUsername($login['username']);
                    } catch (Exception $e) {
                        Mage::helper("ajaxlogin")->log("There has been an error during the login.");
                        // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
                    }
                } else {
                    $result['error'] = Mage::helper('customer')->__('Login and password are required.');
                }
            }

            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
            // code for guest end
        }

BELOW CODE WORKING correctly for registered users :

   protected function _thisProduct($type, $doSave=true, $originalProduct, $newImagePath="") 
    { 

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

        $images = array(
            'thumbnail'   => 'image.png', // displaying under cart page
            //'small_image' => 'image.png',
            'image'       => 'image.png',  // displaying under my design
        ); 

        foreach ($images as $imageType => $imageFileName) {
            if($newImagePath != ""){
                $dir = Mage::getBaseDir('media') . DS . 'one_two_three/quote/';
                $path = $dir . $newImagePath;
            }else{
                $dir = Mage::getBaseDir('media') . DS . 'example/super/';
                $path = $dir . $imageFileName;
            }
            //echo $path."<br>";
            if (file_exists($path)) {
                try {
                    $product->addImageToMediaGallery($path, $imageType, false);
                } catch (Exception $e) {
                    echo $e->getMessage();
                }
            } else {
                echo "No image path found dear: `{$path}`<br/>";
            }
        }

        $customerId = Mage::getSingleton('customer/session')->getCustomerId();
       $product->setCreatedByCustomerId($customerId);

        if ($doSave)
            $product->save();           

        return $product;

    }

Updated question :

how to check $newImagePath variable sending the value or not ?

$newImagePath = $this->getRequest()->getParam("image");
7
  • may be u have missed the media gallery initialization, $product->setMediaGallery (array('images'=>array (), 'values'=>array ())) please add the same as shown in the screenshot prntscr.com/exeb7e Commented Apr 17, 2017 at 11:17
  • please check the same in this reference link prntscr.com/exebnx so that you will get an idea. update me thanks. Commented Apr 17, 2017 at 11:19
  • i am checking now sir.....
    – raj
    Commented Apr 17, 2017 at 11:19
  • sir, i tried like this : try { $product->setMediaGallery (array('images'=>array (), 'values'=>array ())); $product->addImageToMediaGallery($path, $imageType, false); } still the same result......
    – raj
    Commented Apr 17, 2017 at 11:24
  • kindly check commas and semi columns and try it again. Commented Apr 17, 2017 at 11:27

0

Your Answer

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

Browse other questions tagged or ask your own question.