We are displaying "Create product" link in product view page , once we click on link, we are creating product in backend.
addtocart.phtml
<button>
<a href="<?php echo Mage::getUrl("example/amasty/createSimpleProductAndRedirect"); ?>">Create product</a>
</button>
createSimpleProductAndRedirectAction()
public function createSimpleProductAndRedirectAction()
{
if($product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE))
{
$this->_redirect("catalog/product/view/id/".$product->getId());
}
}
when i used ->setName($rand)
, product names are generating as random numbers. but instead of random numbers, we want to create new product with same product name that is present on product view page.
example : If link is present in product page with product name Custom Apple IPhone 4 , once we click on "create product link", it should create product with name "Custom Apple IPhone 4"
I am trying this : ->setName($product->getName())
to get same product names as in below code : app/code/local/Amasty/Example/controllers/AmastyController.php , but its not working.
class Amasty_Example_AmastyController extends Mage_Core_Controller_Front_Action
{
protected function _createProduct($type, $doSave=true)
{
// required for some versions
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
// set madatory system attributes
$rand = rand(1, 9999);
// finally set custom data
$product
->setName($product->getName()) // add string attribute
->setShortDescription('description') // add text attribute
;
if ($doSave)
$product->save();
return $product;
}
}
full code : http://pastebin.com/i6bQFeLa
->setName("test product21")
, but my requirement is to display the same product name which is in product view page, let me know if you have any clarifications & please help me.....