0

Once we click on button in product view page, we are creating New Product Programatically & now I am trying to redirect to newly created Product's URL after click on same button.

result :

onclick button is redirecting to sitename/undefined

public function createSimpleProductAndRedirectAction()
{

$originalProductId = $this->getRequest()->getParam("id");
$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))
   {

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

             $result['message'] = "Your Image Saved";
             return $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
          }          
   }
}

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

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

                $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['redirect'] = $product->getProductUrl();             
                return $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                 exit;
            } else {

            }

        } catch (Mage_Core_Exception $e) {}

        } catch (Exception $e) { }              
    }

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

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

            $product = Mage::getModel('catalog/product');
            $rand = rand(11, 999999999);
            $product->setUrlKey($rand);


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

            return $product;
}

js

_ajaxCreateImg: function(dataUrl, startNumber) 
{ 
    new Ajax.Request(this.createImgUrl, { 
        onComplete: function(response) { 
            var responseData = response.responseText.evalJSON(); 
            if(lastRequest == 1){ 
                this._processAjaxResponse(response); 
                window.location.replace(responseData.redirect); 
            } 
         }.bind(this) 
     }); 
1

Try below,

    public function createSimpleProductAndRedirectAction()
    {

$originalProductId = $this->getRequest()->getParam("id");
$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))
   {

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

         $result['message'] = "Your Image Saved";
         $result['redirect'] = $product->getProductUrl();
         return $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
      }          
   }
}

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

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

            $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()) {
           return $res = $this->saveProduct($type, $doSave, $originalProduct, $newImagePath);
        } else {

        }

    } catch (Mage_Core_Exception $e) {}

    } catch (Exception $e) { }              
}

Your Answer

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