cancel
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 1.9.2.4.

Which could be the cause?

 

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

 

Arianna

 

<?php 
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
        $product = Mage::getModel('catalog/product');
        $rand = rand(1, 9999);
        $product
        ->setTypeId($type)
        ->setAttributeSetId(9) // attribute set = preventivi
        ->setSku('example_sku' . $rand) // generate a random SKU
        ->setWebsiteIDs(array(1))
        ;
		
        $product->setCategoryIds(array(8,9));
        $product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
        $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); // visible in catalog and search
	
		
		
            $product->setName('Test Product ' . $rand);
            $product->setShortDescription('Description'); 
            $product->setPrice(12); 
            $product->setTaxClassId(2); /* taxable goods */   
            $product->setWeight(8);    
/*custom attributes */ $product->setNome('Ari'); $product->setCognome('Fra'); $product->setTelefono('123'); $product->setEmail('test@test.it'); $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(); ?>

 

1 REPLY

Re: Create a product from phtml file

Hi use code may be help you

->setTaxClassId(2)

->setPrice(11.22)

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