0

We have 2 buttons in Product view page, after we click on 1st button, we are creating Product Programtaically.

once click on 2nd button [ after entering email, password] we are assigning the product to that customer.

Here issue is we are creating product immediately after click on 1st button, but we need to save the product in session and nwe want to create that product after user click on 2nd button.

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

        try {
            if (isset($login) && (is_array($login) && !empty($login)) || ($login != "")) {
                $designId = Mage::getSingleton('core/session')->getData('mydesign_id');
                $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();
                $model = Mage::getModel('example/mydesign')->load($designId);
                if ($model->getId()) {
                    $model->setData('customer_id',$customerId);
                    $model->save();
                }

                Mage::getSingleton('core/session')->unsetData('mydesign_id');
                $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                return $this;
            } else {
                $customerId = "";
            }        

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

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

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

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

            $fav1_id = '';
            $data = array(
                'title' => $fav1_id,
                'product_id' => $product->getId(),
                'customer_id' => $customerId

            );
            //echo '<pre/>';
            $model = Mage::getModel('example/mydesign')->setData($data);
            try {
                $fav1_id = $model->save()->getId();
                if (!$customerId) {
                    Mage::getSingleton('core/session')->setData('mydesign_id', $fav1_id);
                }

            } catch (Exception $e) {
                echo $e->getMessage();
            }
            // To save

            return $product;           

        } catch (Mage_Core_Exception $e) {
            switch ($e->getCode()) {
                case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
                    $message = Mage::helper('customer')->__('This account');
                    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));


    }
0

You can save your entire created product in session like

Mage::getModel('core/session')->setMyProduct("your product variable");

and get it back when you set your customer by

Mage::getModel('core/session')->getMyProduct();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy