0

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;
    }
  • Can you show the _createProduct method? From what i can see you are trying to call getId() on something that is not an object. Also does addtocart.phtml have access to $product Also if you could show the whole error you are getting too, so we can see which file it is complaining about :) – DanCarlyon Mar 10 '17 at 11:39
  • @DanCarlyon please check edit part in question – Baby in Magento Mar 10 '17 at 11:41
  • addtocart.phtml code : pastebin.com/7C12E2x4 – Baby in Magento Mar 10 '17 at 11:42
  • fatal error on line : <span><span><?php echo 'See <a href="/catalog/product/view/id/' . $product->getId() . '">created simple product</a>' ?> – Baby in Magento Mar 10 '17 at 11:43
4

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>
1

In the addtocart.phtml you posted, the product is saved to $_product rather than $product

<?php $_product = $this->getProduct(); ?>

You should be able to do

<?php echo 'See <a href="/catalog/product/view/id/' . $_product->getId() . '">created simple product</a>' ?>
  • Thanks , i added like this in addtocart.phtml, still same issue : <?php $_product = $this->getProduct(); ?> <?php $_product->getId(); ?> – Baby in Magento Mar 10 '17 at 11:48
  • The $this->getProduct() is already there at the top of your template. Is it still the same error message? – DanCarlyon Mar 10 '17 at 11:51
  • 1
    no fatal error now, after i followed your updated answer but product is not creating in DB after clciking link...... – Baby in Magento Mar 10 '17 at 11:54
  • Anything in system/exception logs ? – DanCarlyon Mar 10 '17 at 11:55
  • 1
    Thanks a lot for your support, i got solution here..... – Baby in Magento Mar 10 '17 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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