在开发过程中,你经常需要一些可用的数据来做测试。Magento提供了包含一些产品的示例数据。可事实是,当你创建了自己的属性集或者产品类型,想要加入到你的产品里去时,这个示例数据就显得没什么用了。针对这点,你需要自己添加一些商品。

有时在后台添加就可以了,但当你要添加多种产品、继续开发新特征或者做一些大的改动时,用后台操作就变得很痛苦了。这个问题用编程方式来添加产品就能轻松解决了。我将用这种方法来添加一个简单产品。你可以修改一下这个代码来适应其它产品类型,也可以用它来作为产品导入脚本。

你可以通过一个observer来调用,设置为一个控制器方法,或者从一个视图文件中调用,随你喜欢。要注意的是,当你观测一些事件时不要使用调用每个控制器方法的那个。如果你使用了,那么确定产品中是否存在同样的SKU。针对这种情况,你可以用下面代码中第四行的“if”判断。

如果你没有按照我说的做,那么会出现下面的SQL报错:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '193-1' for key 'UNQ_CATALOGINVENTORY_STOCK_ITEM_PRODUCT_ID_STOCK_ID'

下面就是我提到的代码:

<?php
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
//    if(!$product->getIdBySku('testsku61')):
 
try{
$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());
}

我们设定这款产品在“默认值”范围。您可以为一个特定的商店取消第5行的代码来设置产品数据,。例如,你可以这样做:

...
    ->setMetaTitle('test meta title 2')
    ->setMetaKeyword('test meta keyword 2')
    ->setMetaDescription('test meta description 2')
    ->setStoreId(2)
    ->setMetaTitle('Some other meta title')
    ->setMetaKeyword('Some other meta keyword')
    ->setMetaDescription('Some other meta description')
$product->save();

上面的代码将为ID为2的商店设置不同的meta name。

你可以通过登录到产品存储传输数据来找到所有你能设置的产品值。我在考虑当你存储商品是后台调用的ProductController里的saveAction方法。 app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php

public function saveAction()
{
    $storeId        = $this->getRequest()->getParam('store');
    $redirectBack   = $this->getRequest()->getParam('back', false);
    $productId      = $this->getRequest()->getParam('id');
    $isEdit         = (int)($this->getRequest()->getParam('id') != null);
 
    $data = $this->getRequest()->getPost();
    if ($data) {
        Mage::log($data);

只需要把最后一行代码加入到相应的位置。当你完成开发以后记得删除。这将获取一个你存储的数据的数组。例如,为了设置 [meta_title],你会使用setMetaTitle(‘somevalue’)魔术方法。

360magento提供专业的基于magento系统的电商网站开发服务,如有需求或相关咨询,请与我们联系