如何使用Controller在Magento中创建产品?

时间:2014-07-04 07:38:48

标签: php magento

我创建了一个自定义模块,在Admin中显示Menu选项卡 然后,当我点击菜单标签 它转到自定义模块的管理员控制器

这是控制器代码。

class Test_Createproduct_Adminhtml_CreatesampleController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {  
        $this->_initAction() 
     ->renderLayout();
    } 


    public function createsamplesAction(){

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

                    $newProduct = new Mage_Catalog_Model_Product();
                    $newProduct->setAttributeSetId(9)
                    ->setTypeId('simple')
                    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                    ->setTaxClassId(2)
                    ->setCreatedAt(strtotime('now'))
                    ->setName('Advanced')
                    ->setSku('advanced-plan')
                    ->setWeight(1)
                    ->setStatus(1)
                    ->setPrice(50)
                    ->setCategoryIds(array(2,16))
                    ->setWebsiteIds(array(1))
                    ->setDescription('check')
                    ->setShortDescription('check')
                    ->setStockData(array(
                    'manage_stock'=>1,
                    'min_sale_qty'=>1,
                    'max_sale_qty'=>100));

                    $newProduct->save();                




    }  




}

但是当我检查产品网格时。它只显示我点击管理类别>类别产品它显示了产品网格与创建的产品。它只是显示没有任何名称的sku和id 我在数据库表中检查了它没有保存价格,名称,描述,状态和其他东西请帮助

1 个答案:

答案 0 :(得分:0)

试试这个 -

public function createsamplesAction(){
                    $product = Mage::getModel('catalog/product');
                    $product_sku = time();
                    $product->setSku($product_sku);
                    $product->setName('New Product');
                    $product->setDescription('description');
                    $product->setShortDescription("High-end widget.");
                    //-- price section
                    $product->setPrice(50);
                    $product->setSpecialPrice(10);
                    //----

                    //----- company address and map

                    $product->setTypeId('simple');
                    $product->setAttributeSetId(4); // need to look this up
                    $product->setCategoryIds(array(24)); // need to look these up


                    $product->setWeight(1.0);
                    $product->setTaxClassId(2); // taxable goods
                    $product->setVisibility(4); // catalog, search
                    $product->setStatus(1); // enabled
                    $productQty = '1000';

                    //$stockData=$product->getStockData();
                    $stockData['qty']=$productQty;
                    $stockData['is_in_stock']=1;
                    $stockData['manage_stock']=1;
                    $stockData['use_config_manage_stock']=0;
                    $product->setStockData($stockData);
                    $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
                    $product->save();
                    echo "product has been saved";
}

在运行此脚本之前,您需要检查属性集ID和类别ID。