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
2 Answers 2
Here is what you can do.
Update your button code to pass current product name to your controller action like below.
<button>
<a href="<?php echo Mage::getUrl("example/amasty/createSimpleProductAndRedirect", array('name' => $_product->getName())); ?>">Create product</a>
</button>
Update your function createSimpleProductAndRedirectAction
public function createSimpleProductAndRedirectAction()
{
$productNameArray = explode(" - ",$this->getRequest()->getParam("name"));
$productName = $productNameArray[0];
if($product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE, true, $productName))
{
$this->_redirect("catalog/product/view/id/".$product->getId());
}
}
Now update your _createProduct
protected function _createProduct($type, $doSave=true, $productName = NULL)
{
// 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
if($productName){
$product
->setName($productName) // add string attribute
->setShortDescription('description') // add text attribute
;
}else{
$product
->setName("Test Product") // add string attribute
->setShortDescription('description') // add text attribute
;
}
if ($doSave)
$product->save();
return $product;
}
-
i need one more help : we have product name -
Custom Apple IPhone 4 Case - Make Your Own Apple IPhone 4 Case, once user click oncreate productlink , i want to create new product with this nameCustom Apple IPhone 4 Case, means i want to cut the part which is present after minussymbol [-]Baby in Magento– Baby in Magento2017年03月15日 09:19:44 +00:00Commented Mar 15, 2017 at 9:19 -
Please check updated answer.Jaimin Sutariya– Jaimin Sutariya2017年03月15日 09:27:21 +00:00Commented Mar 15, 2017 at 9:27
-
can you please help magento.stackexchange.com/questions/171287/…raj– raj2017年04月26日 09:23:57 +00:00Commented Apr 26, 2017 at 9:23
The answer provided by Jaimin should work, but for a general and extensible solution....
You can send to the create product link, the id of the product you are cloning.
Then you can just take whatever you need from the original product.
Make the link look like this:
<?php echo Mage::getUrl("example/amasty/createSimpleProductAndRedirect", array('id' => $_product->getId())); ?>
Then, in your controller action, load the original product (maybe in the current store view) and clone what you need to the new product.
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))
{
$this->_redirect("catalog/product/view/id/".$product->getId());
}
}
and then
protected function _createProduct($type, $doSave=true, $originalProduct)
{
// required for some versions
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
$product->setName($originalProduct->getName());
//clone the description if needed
//$product->setDescription($originalProduct->getName());
//or assign an other description
$product->setDescription('what ever you need here');
if ($doSave)
$product->save();
return $product;
}
-
this worked peRFectly fine......Baby in Magento– Baby in Magento2017年03月15日 10:14:05 +00:00Commented Mar 15, 2017 at 10:14
Explore related questions
See similar questions with these tags.
->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.....