Learn how to import coupons into Magento. How to import coupons in Magento
How to import multiple coupons in Magento from a CSV
A customer asked how to import product into Magento from a nice CSV.
There is an easy trick that solves this matter.
We found code on the internet and modified it to import using a percentage.
Upload the CSV to your server in the same folder as the script.
Here is the template for the CSV coupon import.
1 2 3 |
is_active,coupon_code,description,discount_amount 1,1c5e8b24,serie1 coupon,20 1,593d90a1,serie1 coupon,20 |
And here is the import code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
<?php // Import coupon codes // Thanks go out to Marius Strajeru // Original URL: http://marius-strajeru.blogspot.nl/2010/04/create-bulk-discount-rules.html // modified to use fixed percentage and one time use require 'app/Mage.php'; Mage::app(); // Import CSV from ViArt format: $handle = fopen('all_coupons1.csv', 'r'); $cols = array_flip(fgetcsv($handle)); while($data = fgetcsv($handle)) { if($data[$cols['is_active']] == 1) { echo 'Importing coupon with code: '.$data[$cols['coupon_code']].'<br />'; createCoupon( $data[$cols['coupon_code']], $data[$cols['description']], 'by_fixed', $data[$cols['discount_amount']] ); } else { echo 'Not imported (not active): '.$data[$cols['coupon_code']].'<br />'; } } /** * Import coupon * @param $code String Coupon code * @param $description String Description * @param $type String by_percent, by_fixed, cart_fixed, buy_x_get_y (not implemented) * @param $amount int The amount * @param array $options Optional options (from, to) */ function createCoupon($code, $description, $type, $amount, $options = array()) { // Create coupon: /* @var $rule Mage_SalesRule_Model_Rule */ $rule = Mage::getModel('salesrule/rule'); // set name can be code coupon by using ($code) $rule->setName('20procent'); $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(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('by_percent'); $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(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; } |