Showing results for 
Search instead for 
Did you mean: 

Create a product from phtml file

Create a product from phtml file

Hi Everybody!

I'm new to the forum and I'm also pretty new to Magento.


I was trying to create a simple product with some code wrote in a phtml file, and following some examples on the internet I finally did it.

The product is created and all is working fine, except that the price, the weight, the tax class id and the quantity are not inserted into the product created.

The Magento version is the Community

Which could be the cause?


Here's the code that I have and, if anyone could help, thanks in advance!




        $product = Mage::getModel('catalog/product');
        $rand = rand(1, 9999);
        ->setAttributeSetId(9) // attribute set = preventivi
        ->setSku('example_sku' . $rand) // generate a random SKU
        $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); // visible in catalog and search
            $product->setName('Test Product ' . $rand);
            $product->setTaxClassId(2); /* taxable goods */   
/*custom attributes */ $product->setNome('Ari'); $product->setCognome('Fra'); $product->setTelefono('123'); $product->setEmail(''); $product->setSopraluogo(1); $product->setDescri_lavoro('test'); ; $product->setStockData(array( 'use_config_manage_stock' => 0, // use global config or no 'manage_stock' => 1, // manage stock or no 'is_in_stock' => 1, //Stock Availability 'qty' => 1 ));
$product->save(); ?>



Re: Create a product from phtml file

Hi use code may be help you



->setStockData(array( 'use_config_manage_stock' => 0, //'Use config settings' checkbox 'manage_stock'=>1, //manage stock 'min_sale_qty'=>1, //Minimum Qty Allowed in Shopping Cart 'max_sale_qty'=>2, //Maximum Qty Allowed in Shopping Cart 'is_in_stock' => 1, //Stock Availability 'qty' => 999 //qty ) )