1.准备好coupon code数据表放到
$file_path = BP . DS . 'media' .DS . 'coupon' . DS . 'coupon.csv';
2.执行以下shell代码
php shell/importCoupon.php
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Shell
* @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
require_once 'abstract.php';
/**
* Magento Log Shell Script
*
* @category Mage
* @package Mage_Shell
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Shell_ImportCoupon extends Mage_Shell_Abstract
{
/**
* Run script
*
*/
public function run()
{
$file_path = BP . DS . 'media' .DS . 'coupon' . DS . 'coupon.csv';
$i = 0;
if (($gestor = fopen($file_path, "r")) !== FALSE) {
while (($data = fgetcsv($gestor)) !== FALSE) {
$i++;
if ($i != 1) {
$oCoupon = Mage::getModel('salesrule/coupon')->load($data[1], 'code');
$oRule = Mage::getModel('salesrule/rule')->load($oCoupon->getRuleId());
if ($oRule->getData('coupon_code') == null) {
$this->createCoupon(trim($data[0]),trim($data[1]),trim($data[2]),'by_fixed',(float)trim($data[3]),array('to'=>$data[4]));
var_dump($data[1] . ' coupon add successed!');
} else {
var_dump($data[1] . ' had been created!');
}
}
}
fclose($gestor);
}
}
function createCoupon($name,$code, $description, $type, $amount, $options = array())
{
// Create coupon:
/* @var $rule Mage_SalesRule_Model_Rule */
$rule = Mage::getModel('salesrule/rule');
$rule->setName($name);
$rule->setCouponCode($code);
$rule->setDescription($description);
// Default options:
if(!isset($options['from'])) { $options['from'] = date('Y-m-d'); }
$rule->setFromDate($options['from']); // From date
// To date:
if(isset($options['to'])) {
$rule->setToDate($options['to']);//if you need an expiration date
}
$rule->setUsesPerCoupon(1);//number of allowed uses for this coupon
$rule->setUsesPerCustomer(1);//number of allowed uses for this coupon for each customer
$rule->setCustomerGroupIds($this->getAllCustomerGroups());//if you want only certain groups replace getAllCustomerGroups() with an array of desired ids
$rule->setIsActive(1);
$rule->setStopRulesProcessing(0);//set to 1 if you want all other rules after this to not be processed
$rule->setIsRss(0);//set to 1 if you want this rule to be public in rss
$rule->setIsAdvanced(1);//have no idea what it means :)
$rule->setProductIds('');
$rule->setSortOrder(0);// order in which the rules will be applied
$rule->setSimpleAction($type);
$rule->setDiscountAmount($amount);//the discount amount/percent. if SimpleAction is by_percent this value must be <= 100
$rule->setDiscountQty(0);//Maximum Qty Discount is Applied to
$rule->setDiscountStep(0);//used for buy_x_get_y; This is X
$rule->setSimpleFreeShipping(0);//set to 1 for Free shipping
$rule->setApplyToShipping(1);//set to 0 if you don't want the rule to be applied to shipping
$rule->setWebsiteIds($this->getAllWbsites());//if you want only certain websites replace getAllWbsites() with an array of desired ids
$labels = array();
$labels[0] = $description;
$rule->setStoreLabels($labels);
$rule->setCouponType(2);
$rule->save();
}
/**
* Get all customer groups
* @return array
*/
function getAllCustomerGroups(){
//get all customer groups
$customerGroups = Mage::getModel('customer/group')->getCollection();
$groups = array();
foreach ($customerGroups as $group){
$groups[] = $group->getId();
}
return $groups;
}
/**
* Get all websites
* @return array
*/
function getAllWbsites(){
//get all wabsites
$websites = Mage::getModel('core/website')->getCollection();
$websiteIds = array();
foreach ($websites as $website){
$websiteIds[] = $website->getId();
}
return $websiteIds;
}
}
$shell = new Mage_Shell_ImportCoupon();
$shell->run();