0

once guest user click on button "add to cart1" , we are doing below things :

1] we are creating simple product programatically

2] we are assigning custom image to that product.

3] Display one more pop-up to Login to site by click on "login" button

once user click on "login button , we are doing below things :

1] we are creating simple product programmatically

2] we are not assigning a custom image to that product.

but here we need to assign custom image to that product.

public function thisSimpleProductAndRedirectAction()
    {
        $originalProductId = $this->getRequest()->getParam("id");
        // $productNameArray = explode(" - ",$this->getRequest()->getParam("id"));
        // $originalProductId = $productNameArray[0];
        $newImagePath      = $this->getRequest()->getParam("image");
        $originalProduct   = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($originalProductId);

        if ($product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE, true, $originalProduct, $newImagePath)) {
            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode("custom image saving"));
        }
        else{
            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode("custom image not saving"));
        }

    }

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

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



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

        $login ="";
        $productId ="";
        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 {
                    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();
                    }
                    else{
                        $customerId = "";
                    }

                    // code for guest end        

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

                    // add images

                    $images = array(
                        'thumbnail' => 'image.png', // displaying under cart page

                        'image' => 'image.png' // displaying under my design
                    );

                    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;
                        }
                        //echo $path."<br>";
                        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/>";
                        }
                    }                  

                    $emailimage = Mage::helper('catalog/image')->init($product, 'thumbnail');

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

                    return $product;

                    // code for guest

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

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

updated the question = >

add to cart1 code is as below :

<button onclick="return setproductlogin(\'<?php echo 

Mage::registry("current_product")->getId()?>\', event);" 

 id="submit-editorApply-{{rand}}" >Add to cart1</button>

update 2 :

once we click on add to cart1 , it redirect to link5 with image parameter

and when we click on login , it redirect to link1 without image param , so only image not creating for login.....

6
  • Once again, you've posted code and what you "expect" to happen. What is actually happening? Are you getting any errors? Is there anything in your logs? Have you tried logging the $path variable to see what the full path is? Do you have the ability to step-through your code with a debugger like PHPStorm with Remote Debug?
    – Brett
    Apr 20 '17 at 13:02
  • @Brett i dont see any errors in logs. sorry i dont have any debuggers , please guide me how to ` loggin the $path variable to see what the full path is` ?
    – raj
    Apr 20 '17 at 13:22
  • Mage::log("File Path: " . $path, Zend_Log::DEBUG, 'debug.log'); Add that line where you have //echo $path."<br>"; Then run your code and look in var/log/debug.log to see what the path is set as.
    – Brett
    Apr 20 '17 at 13:23
  • @Brett when we click on addtocart1 button, its showing image path as in debug.log file as media/custom_product_preview/quote/514075431.png , but when i click on login button, it showing image path as media/example/amasty/image.png
    – raj
    Apr 20 '17 at 13:29
  • So it seems that your $newImagePath isn't persisting after logging in or you're not saving the new image path in your session and retrieving it properly after login. I don't see a Mage::getSingleton('checkout/session')->setNewImagePath($newImagePath);, so how are you saving the $newImagePath to be read back AFTER they log in?
    – Brett
    Apr 20 '17 at 13:36
0

It appears that you're not persisting the $newImagePath value from before login to after login (or before redirect to after redirect). This value needs to be kept somewhere (like the session) so ensure you can use it later.

So I would probably do the following:

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 {
            if ($imageType == 'image' && $newImagePath) {
                Mage::getSingleton('checkout/session')->setNewImagePath($path);
            }
            $product->addImageToMediaGallery($path, $imageType, false);
        }
        catch (Exception $e) {
            echo $e->getMessage();
        }
    } else {
        echo "Can not find image by path: `{$path}`<br/>";
    }
}

Then after they've logged in and you're doing whatever you need to do, you can retrieve the full new image path using:

$newImagePath = Mage::getSingleton('checkout/session')->getNewImagePath();

It's unclear how you use the $newImagePath after they've logged in, so I can't help much more than for you to retrieve the value from the session after they've logged in (the above line of code).

4
  • still the same, please let me if you want to know any other details......
    – raj
    Apr 20 '17 at 19:19
  • onclick login button, we are not saving custom images in server, is that causing the issue as in link : magento.stackexchange.com/questions/171175/…
    – raj
    Apr 21 '17 at 13:19
  • If you're not saving the data posted before you redirect them, you'll lose the file upon redirect. PHP cleans up temporary uploaded files unless you move them with move_uploaded_file (or some other mechanism). So if you're not saving the file before redirecting them, that's an issue.
    – Brett
    Apr 21 '17 at 19:16
  • yes, once we click on button add to cart1/create`, we are saving custom image in server and displaying pop up , in pop up once user enter email, pw, click on login, we are creating only product, we are not creating any custom image in server, so to solve this issue, i posted a question , when you get free time please check link : magento.stackexchange.com/questions/171175/…
    – raj
    Apr 21 '17 at 19:28
0

Please do refer this -

https://www.hummingbirduk.com/add-images-to-products-programmatically-in-magento/

Hope it is helpful for you.

-1

You need to replace this code :

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;
}

with this :

 $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;
    }
4
  • its not working , can you re any other idea ?
    – raj
    Apr 20 '17 at 12:57
  • have you debug code, you get proper value $path ? Apr 20 '17 at 12:58
  • This code makes no difference. $path would be initialized no matter if $newImagePath is empty or not, so adding $path = ''; won't have any effect.
    – Brett
    Apr 20 '17 at 13:00
  • but in above code, very next to this code it check this if (file_exists($path)) So it is usefull to check that properly pathis assign or not. Apr 20 '17 at 13:02

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.