Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento 如何使用自定义模块创建产品?_Magento - Fatal编程技术网

Magento 如何使用自定义模块创建产品?

Magento 如何使用自定义模块创建产品?,magento,Magento,我想在安装自定义模块时创建一个产品。如何实现这一点? 这是我的自定义模块设置脚本 $this->startSetup(); $product= new Mage_Catalog_Model_Product(); $product->setSku("basic-plan"); $product->setName("Basic Plan"); $product->setDescription("Description for the product"); $product-&

我想在安装自定义模块时创建一个产品。如何实现这一点? 这是我的自定义模块设置脚本

$this->startSetup();
$product= new Mage_Catalog_Model_Product();
$product->setSku("basic-plan");
$product->setName("Basic Plan");
$product->setDescription("Description for the product");
$product->setShortDescription("Short Description for the product");
$product->setPrice(20);
$product->setTypeId('simple');
$product->setAttributeSetId(4); // enter the catalog attribute set id here
$product->setCategoryIds(array(16)); // id of categories
$product->setWeight(1.0);
$product->setTaxClassId(0); // taxable goods
$product->setVisibility(1); // catalog, search
$product->setStatus(1); // enabled
$product->setStoreIDs(array(1));
$product->setStockData(
    array(
        'is_in_stock' => 1,
        'qty' => 1000,
    'manage_stock' => 1
    )
);
$product->setIsRecurring('1');
$product->setRecurringProfile(array(
        'schedule_description' => 'Basic Plan',
        'period_unit' => 'week',
        'period_frequency' => 1

    ));
// assign product to the default website
//$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
$product->setWebsiteIds(array(1));

$product->save();

/*Subscription Plan Products Ends Here*/


$this->endSetup();
安装程序正在运行,但我无法使用此脚本创建产品

您好,请尝试此代码


我已经试过了,在某个地方它可以保存sku、产品类型、类别,但没有名称、价格和其他信息
<?php
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
//    if(!$product->getIdBySku('testsku61')):
$product
//    ->setStoreId(1) //you can set data in store scope
    ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
    ->setAttributeSetId(9) //ID of a attribute set named 'default'
    ->setTypeId('simple') //product type
    ->setCreatedAt(strtotime('now')) //product creation time
//    ->setUpdatedAt(strtotime('now')) //product update time
    ->setSku('testsku61') //SKU
    ->setName('test product21') //product name
    ->setWeight(4.0000)
    ->setStatus(1) //product status (1 - enabled, 2 - disabled)
    ->setTaxClassId(4) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
    ->setManufacturer(28) //manufacturer id
    ->setColor(24)
    ->setNewsFromDate('06/26/2014') //product set as new from
    ->setNewsToDate('06/30/2014') //product set as new to
    ->setCountryOfManufacture('AF') //country of manufacture (2-letter country code)
    ->setPrice(11.22) //price in form 11.22
    ->setCost(22.33) //price in form 11.22
    ->setSpecialPrice(00.44) //special price in form 11.22
    ->setSpecialFromDate('06/1/2014') //special price from (MM-DD-YYYY)
    ->setSpecialToDate('06/30/2014') //special price to (MM-DD-YYYY)
    ->setMsrpEnabled(1) //enable MAP
    ->setMsrpDisplayActualPriceType(1) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
    ->setMsrp(99.99) //Manufacturer's Suggested Retail Price
    ->setMetaTitle('test meta title 2')
    ->setMetaKeyword('test meta keyword 2')
    ->setMetaDescription('test meta description 2')
    ->setDescription('This is a long description')
    ->setShortDescription('This is a short description')
    ->setMediaGallery (array('images'=>array (), 'values'=>array ())) //media gallery initialization
    ->addImageToMediaGallery('media/catalog/product/1/0/10243-1.png', array('image','thumbnail','small_image'), false, false) //assigning image, thumb and small image to media gallery
    ->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
        )
    )
    ->setCategoryIds(array(3, 10)); //assign product to categories
$product->save();
//endif;
}catch(Exception $e){
    Mage::log($e->getMessage());
}