嗨,我正试图通过csv将优惠券导入magento-mysql,当我尝试下面的解决方案时,我得到了一个错误:
分析错误:语法错误,意外的T_STRING,应为"、"或";"在第19行上的/home/wwwwevolv/public_html兰开斯特/bulk.php中
有什么想法吗?是代码引号吗?
您需要编写自定义php脚本,我在下面尝试过链接博客工作,需要根据您的要求进行一些更改
http://www.gielberkers.com/bulk-import-coupon-codes-in-magento/
试试这个脚本,我用它来导入优惠券:
<?php
$mageFilename = '../../app/Mage.php';
require_once $mageFilename;
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('default');
Mage::register('isSecureArea', 1);
function getAllWbsites(){
//get all wabsites
$websites = Mage::getModel('core/website')->getCollection();
$websiteIds = array();
foreach ($websites as $website){
$websiteIds[] = $website->getId();
}
return $websiteIds;
}
//read comments for each line
function generateRule($rulename, $desc, $status, $customerGroups, $couponCode, $fromDate, $toDate, $discountType, $discountAmount){
$couponCheck = Mage::getModel('salesrule/rule')->getCollection()
->addFieldToFilter('code',$couponCode)
->load();
$couponCheckArr = $couponCheck->getData();
if(count($couponCheckArr)>0) {
return false;
}
$rule = Mage::getModel('salesrule/rule');
$rule->setName($rulename);
$rule->setDescription($desc);
$rule->setFromDate($fromDate);//starting today
if($toDate!="") {
$rule->setToDate($toDate);//if you need an expiration date
}
$rule->setCouponCode($couponCode);
$rule->setUsesPerCoupon(1);//number of allowed uses for this coupon
$rule->setUsesPerCustomer(1);//number of allowed uses for this coupon for each customer
$customerGroups = explode(',',$customerGroups);
$rule->setCustomerGroupIds($customerGroups);//if you want only certain groups replace getAllCustomerGroups() with an array of desired ids
$rule->setIsActive($status);
$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($discountType);
//all available discount types
//by_percent - Percent of product price discount
//by_fixed - Fixed amount discount
//cart_fixed - Fixed amount discount for whole cart
//buy_x_get_y - Buy X get Y free (discount amount is Y)
$rule->setDiscountAmount($discountAmount);//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(0);//set to 0 if you don't want the rule to be applied to shipping
$rule->setWebsiteIds(getAllWbsites());//if you want only certain websites replace getAllWbsites() with an array of desired ids
$conditions = array();
$conditions[1] = array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
'new_child' => ''
);
$rule->setData('conditions',$conditions);
$rule->loadPost($rule->getData());
$rule->setCouponType(2);
if($rule->save()) {
return true;
}
else {
return false;
}
}
$fp = fopen('../data/coupon-import.csv','r') or die("can't open file");
$count = 0;
$countNotImpt = 0;
$i = 0;
while($csv_line = fgetcsv($fp,1024,"'t")) {
if($i>0) {
$rulename = $csv_line[0];
$desc = $csv_line[1];
$status = $csv_line[2];
$customerGroups = $csv_line[3];
$couponCode = $csv_line[4];
$fromDate = $csv_line[5];
$toDate = $csv_line[6];
$discountType = $csv_line[7];
$discountAmount = $csv_line[8];
if(generateRule($rulename, $desc, $status, $customerGroups, $couponCode, $fromDate, $toDate, $discountType, $discountAmount)) {
$count++;
}
else{
$countNotImpt++;
}
}
$i++;
}
fclose($fp) or die("can't close file");
echo $count.' coupon successfully added.<br>';
echo $countNotImpt.' coupon already exits.<br>';
?>
CSV文件格式:
RULENAME DESCRIPTION STATUS CUSTOMERGROUP COUPONCODE FROMDATE TODATE DISCOUNTTYPE DISCOUNTAMOUNT
Thank You $ 10 OFF! 1 0,1 GC10ADZZ 12/14/2009 11:25:17 AM by_fixed 10
Thank You $ 10 OFF! 1 0,1 GC10CEED 12/14/2009 11:25:17 AM by_fixed 10
Thank You $ 10 OFF! 1 0,1 GC10DEGE 12/14/2009 11:25:17 AM by_fixed 10
Thank You $ 10 OFF! 1 0,1 GC10DHGD 12/14/2009 11:25:17 AM by_fixed 10
Thank You $ 10 OFF! 1 0,1 GC10EHGE 12/14/2009 11:25:17 AM by_fixed 10
Thank You $ 10 OFF! 1 0,1 GC10HBDB 12/14/2009 11:25:17 AM by_fixed 10
Thank You $ 10 OFF! 1 0,1 GC10ZCCB 12/14/2009 11:25:17 AM by_fixed 10
Thank You $ 10 OFF! 1 0,1 GC10FHGZ 12/14/2009 11:25:17 AM by_fixed 10
Thank You $ 10 OFF! 1 0,1 GC10EAFA 12/14/2009 11:25:17 AM by_fixed 10
希望会有所帮助!