How to Use Controller Code in Template .phtml File in Magento 1.9

magento-1.9

after visit this url : sitename.com/example/module/createSimpleProduct ,
we are displaying link : "create products" , once we click on link, it will create new products in Database.

app/code/local/module/Example/controllers – Modulecontroller.php

class Module_Example_ModuleController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
        {
            echo 'This is index controller.   
                <br/>
                <a href="/example/module/createSimpleProduct">Create Simple Product</a>';

        }

    public function createSimpleProductAction()
        {
            $product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
            echo 'See <a href="/catalog/product/view/id/' . $product->getId() . '">create products</a>';
        }   

we want to display this link & achieve same feature in product view page, so we added below code & gave href link to the button in addtocart.phtml file , but its giving fatal error :

 <button type="button" >
    <span>
    <?php echo 'See <a href="/catalog/product/view/id/' . $product->getId() . '">created simple product</a>' ?>
    </span>
    </button>

enter image description here

Edit

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

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

        $product = Mage::getModel('catalog/product');

        // set madatory system attributes
        $rand = rand(1, 9999);
        $product
            ->setTypeId($type)     // e.g. Mage_Catalog_Model_Product_Type::TYPE_SIMPLE
            ->setAttributeSetId(17) // default attribute set
            ->setSku('CDSB_sku' . $rand) // generate some random SKU 
            ->setWebsiteIDs(array(1))
        ;           

        // make the product visible
        $product
            ->setCategoryIds(array(2,3))
            ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
            ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) // visible in catalog and search
        ;
        // configure stock
        $product->setStockData(array(
            'use_config_manage_stock' => 1, // use global config ?
            'manage_stock'            => 0, // shoudl we manage stock or not?
            'is_in_stock'             => 1, 
            'qty'                     => 50,
        ));     

        // optimize performance, tell Magento to not update indexes
        $product
            ->setIsMassupdate(true)
            ->setExcludeUrlRewrite(true)
        ;

        // finally set custom data
        $product
            ->setName('Test Product #' . $rand) // add string attribute
            ->setShortDescription('Description') // add text attribute

            // set up prices
            ->setPrice(599)
            //->setSpecialPrice(19.99)
            ->setTaxClassId(2)    // Taxable Goods by default
            ->setWeight(1)
        ;

        // add product images
        $images = array(
            'thumbnail'   => 'image.png',
            'small_image' => 'image.png',
            'image'       => 'image.png',
        );

        $dir = Mage::getBaseDir('media') . DS . 'example/amasty/';

        foreach ($images as $imageType => $imageFileName) {
            $path = $dir . $imageFileName;
            if (file_exists($path)) {
                try {
                    $product->addImageToMediaGallery($path, $imageType, false);
                } catch (Exception $e) {
                    echo $e->getMessage();
                }
            } else {
                echo "Can not find image by path: `{$path}`<br/>";
            }
        }

        if ($doSave)
            $product->save();

        return $product;
    }

Best Answer

I think you need to create a new controller action and then call it when you click on "Create custom product" button in your phtml

public function createSimpleProductAndRedirectAction()
{
    if($product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)){
        $this->_redirect("catalog/product/view/id/".$product->getId());
    }
}

Now use this action in your button href

<button type="button" > 
    <span> 
        <a href="<?php echo Mage::getUrl("example/amasty/createSimpleProductAndRedirect"); ?>">created simple product</a>
    </span> 
</button>