1

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

enter image description here

2
  • Please have a look into inchoo.net/magento/programming-magento/… Mar 15, 2017 at 7:23
  • @Nalin Thanks for link, but i already refrerred that, in that link they mentioned this code to display product names : ->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..... Mar 15, 2017 at 7:25

2 Answers 2

8
+50

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;
}
3
  • i need one more help : we have product name - Custom Apple IPhone 4 Case - Make Your Own Apple IPhone 4 Case , once user click on create product link , i want to create new product with this name Custom Apple IPhone 4 Case , means i want to cut the part which is present after minus symbol [-] Mar 15, 2017 at 9:19
  • Please check updated answer. Mar 15, 2017 at 9:27
  • can you please help magento.stackexchange.com/questions/171287/…
    – raj
    Apr 26, 2017 at 9:23
5

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;
}
1
  • this worked peRFectly fine...... Mar 15, 2017 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.