Question

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

Was it helpful?

Solution

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;
}

OTHER TIPS

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top