0

once we click on button in product view page, we are creating New Product Programatically & displaying pop up with message : "new product created" Instead of displaying pop up , i need to redirect to Newly created Product's url.

public function createSimpleProductAndRedirectAction()
    {       
        $originalProductId = $this->getRequest()->getParam("id");           
        $originalProduct = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($originalProductId);

        $_productUrl = Mage::getModel('catalog/product')->load($product->getId())->getUrlKey();         

        if ($product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE, true, $originalProduct, $newImagePath)) 
        {                   
            if(Mage::getSingleton('customer/session')->isLoggedIn()) 
            {
                return $this->getResponse()->setBody(Mage::helper('core')->jsonEncode("new Product created"));
            }       
        }               
    }

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

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

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

            }

            if(Mage::getSingleton('customer/session')->isLoggedIn()) {
                $res = $this->saveProduct($type, $doSave, $originalProduct, $newImagePath);
                $result['success'] = true;
                $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                return $this; exit;
            } else {

            }

        } catch (Mage_Core_Exception $e) {}

        } catch (Exception $e) { }              
    }

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

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

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

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

            return $product;
    }

I tried replacing this code :

return $this->getResponse()->setBody(Mage::helper('core')->jsonEncode("new Product created"));

by below code :

$_productUrl = Mage::getModel('catalog/product')->load($product->getId())->getUrlKey(); 
return $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($_productUrl));

Error :

Fatal error: Call to undefined method Amasty_Example_AmastyController::getId() in line 

$_productUrl = Mage::getModel('catalog/product')->load($product->getId())->getUrlKey();

Is i am using code in wrong place ? because echo $_productUrl gave empty value.

0

$product doesn't appear to have been defined when you called ->getId() on it. Perhaps try getting Id from $originalProduct or defer calling getUrl() until after the _createProduct method has been called?

6
  • yes , when i try with return $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($originalProduct)); it will display Product details in pop up , but i need to redirect to newly created product – user52996 May 18 '17 at 12:21
  • Ahh ok, you want to actually be redirected to that page. Sorry for misunderstanding. – BAF May 18 '17 at 12:27
  • thats fine..... you are right, instead of displaying , i want to redirect..... – user52996 May 18 '17 at 12:27
  • Perhaps you should explain a little more in the post - is what you're using custom code? If not then which class is it? – BAF May 18 '17 at 12:30
  • I won't be able to assist because I mainly work purely back end stuff like APIs - etc, I mean elaborate so that perhaps someone else will be able to help with it. However, the error you're getting is coming from a 3rd Party developer (Amasty) - not sure what that is about, but will be hard for people to assist without context. – BAF May 18 '17 at 12:35

Your Answer

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