1

Once Registered user upload custom image on Product view image & click on button, We are creating Product programtically with custom image.

but when guest click on same button after uploading custom image, Product is creating with static image [ Image which is present in media/example/amasty folder]

We want to create product with custom image for guest also....

protected function _createProduct($type, $doSave=true, $originalProduct, $newImagePath="") 
{       
        $product = Mage::getModel('catalog/product');

        $product->setName($originalProduct->getName()); 

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

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

        foreach ($images as $imageType => $imageFileName) {
            if($newImagePath != ""){
                $dir = Mage::getBaseDir('media') . DS . 'custom_product_preview/quote/';
                $path = $dir . $newImagePath;
            }else{
                $dir = Mage::getBaseDir('media') . DS . 'example/amasty/';
                $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();

        return $product;
    }

full controllers : https://pastebin.com/Gn6iMwug

7
  • $customerId = Mage::getSingleton('customer/session')->getCustomerId(); $product->setCreatedByCustomerId($customerId); have you tried by commenting these 2 line ? Commented Apr 11, 2017 at 6:34
  • @MineshPatel yes, still the same..... Commented Apr 11, 2017 at 6:37
  • Are you sure you are sending the $newImagePath variable. May be you are not sending this value. Check that. Commented Apr 11, 2017 at 6:39
  • @KingshukDeb but for Registered users its working fine, let me cross check once..... Commented Apr 11, 2017 at 6:53
  • @BabyinMagento you mean create custom product with image programmatically ??
    – Dhrumin
    Commented Apr 19, 2017 at 4:59

1 Answer 1

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

        Mage::register('isSecureArea', true);
        $session = Mage::getSingleton('customer/session');

        $result = array(
            'success' => false
        );
        $res = '';
        $login = "";
        $productId = "";
        if ($this->getRequest()->isPost()) {
            $login = $this->getRequest()->getPost('login');
            $productId = $this->getRequest()->getPost('product_id');
        }

        try {
            if (isset($login) && (is_array($login) && !empty($login)) || ($login != "")) {

                $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();

            }

            if(Mage::getSingleton('customer/session')->isLoggedIn()) {


            // Registered user click on save design
            return $res = $this->saveProduct($type, $doSave, $originalProduct, $newImagePath);

            } else {
                $p_data = array(
                'type' => $type, 
                'doSave' => $doSave, 
                'newImagePath' => $newImagePath,
                'name' => $originalProduct->getName(),
                );
                Mage::getSingleton('core/session')->unsProductdata();
                Mage::getSingleton('core/session')->setProductdata($p_data);

                $result['success'] = false;

                //guest enter wrong credentilas & click on login

                $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                return $this; exit;     

            }

        } 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. <a href="%s">Click here</a> to resend confirmation email.', Mage::helper('customer')->getEmailConfirmationUrl($login['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) {
            $result = "ERROR :" . $e->getMessage();


        }       

        Mage::unregister('isSecureArea');
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));       
    }

public function saveProduct($type, $doSave = true, $originalProduct, $newImagePath) {

            Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

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

            $rand = rand(11, 999999999);
            $product->setTypeId($type)
            ->setAttributeSetId(17)
            ->setSku('CDSB1_sku' . $rand)
            ->setWebsiteIDs(array(
                1
            ));                      

            $product
                ->setCategoryIds(array(2, 3))
                ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) 
            ;

            $product->setStockData(array(
                'use_config_manage_stock' => 1, 
                'manage_stock' => 0, 
                'is_in_stock' => 1,
                'qty' => 50
            ));

            $product->setIsMassupdate(true)->setExcludeUrlRewrite(true);

            $product->setName($originalProduct->getName());

            $product->setDescription($originalProduct->getDescription())->setShortDescription($originalProduct->getShortDescription())

                ->setPrice(599)
                ->setTaxClassId(0)
                ->setWeight(1);
            $product->setDeliveryInfo($originalProduct->getDeliveryInfo());
            $product->setInteriorExterior($originalProduct->getInteriorExterior());
            $product->setSizeSpace($originalProduct->getSizeSpace());


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

            $emaildate = Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s');


            if ($doSave) {          

                $images = array(

                    'image' => 'image.png' 

                );


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

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

                    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();                       

            return $product;    
    }

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.