0

once guest user click on button "create",

1) we are creating product programatically and

2) displaying pop up form to enter email id , password and login button.

enter image description here

once click on login button, we are creating new product and assiging that to customer [ as guest will become customer after login ]

what i want to know is once guest user click on "create" button, as we create product programtically, can we keep that product in session or in somewhere and once guest click on login button, can we assign that product to customer ?

is this possible to achieve ?

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

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

            // 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 = "";
                    }                  

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

                    //images
                    $images = array(
                            '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;
                        }
                        //echo $path."<br>";
                        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/>";
                        }
                    }


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

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

                    if ($doSave)
                        $product->save();
                    // To save inside my_designs table

                    $data  = array(
                        'title' => fav1_id,
                        'product_id' => $product->getId(),
                        'customer_id' => $customerId,
                        'store_id' => 1,
                        'created_at' => $emaildate,
                        'value' => $newImagePath

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

                    // 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.', 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));

    }

1 Answer 1

1

Create an observer for customer_register_success event and create the product there.

10
  • can you please help me with code........
    – raj
    Commented Apr 23, 2017 at 5:56
  • check the code here. github.com/bgkavinga/sample-code/raw/master/… I haven't tested it. Commented Apr 23, 2017 at 6:28
  • i tried , but it didt worked for me....
    – raj
    Commented Apr 24, 2017 at 6:51
  • What you want is to create a new product for newly registered customers? Am I right? Commented Apr 26, 2017 at 4:57
  • once guest click on create button , We are creating new product... than we click on login button, it will display pop up , in that we will enter email id and pw and click on login button, than guest will become registered user. for that user we need to assign the product when we created after click on create button....
    – raj
    Commented Apr 26, 2017 at 5:15

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.