Php magento-优惠券代码批量导入
您好,我正在尝试通过csv将优惠券导入magento mysql,当我尝试以下解决方案时,我收到一个错误: 分析错误:语法错误,意外的T_字符串,应为“,”或“;” 在第19行的/home/wwwevolv/public_html/Lancaster/bulk.php中Php magento-优惠券代码批量导入,php,magento,Php,Magento,您好,我正在尝试通过csv将优惠券导入magento mysql,当我尝试以下解决方案时,我收到一个错误: 分析错误:语法错误,意外的T_字符串,应为“,”或“;” 在第19行的/home/wwwevolv/public_html/Lancaster/bulk.php中 有什么想法吗?是代码引用吗?您需要编写我在下面尝试过的自定义php脚本链接博客正常工作需要根据您的要求进行一些更改 尝试此脚本,我正在使用此脚本导入优惠券: <?php $mageFilename = '../../app
有什么想法吗?是代码引用吗?您需要编写我在下面尝试过的自定义php脚本链接博客正常工作需要根据您的要求进行一些更改
尝试此脚本,我正在使用此脚本导入优惠券:
<?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>';
?>
希望会有帮助 下面的解决方案是什么?非常感谢,您是否可以将优惠券应用于某些SKU或产品ID?是的,您需要创建销售规则条件以应用特定产品。要为特定产品应用优惠券,您需要更新SKU属性设置。设置促销用途,并为任何特定优惠券选择产品属性。我需要查看特定于产品SKU的条件数组。
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