Version Notes
Coupon Import-Export Extension
Download this release
Release Info
Developer | Sanjay |
Extension | Indianic_Couponimpexport |
Version | 1.0.0.1 |
Comparing to | |
See all releases |
Version 1.0.0.1
- app/code/local/Indianic/Couponimpexport/Model/Convert/Adapter/Couponimpexport.php +181 -0
- app/code/local/Indianic/Couponimpexport/Model/Convert/Parser/Couponimpexport.php +55 -0
- app/code/local/Indianic/Couponimpexport/Model/Couponimpexport.php +10 -0
- app/code/local/Indianic/Couponimpexport/etc/config.xml +26 -0
- app/etc/modules/Indianic_Couponimpexport.xml +17 -0
- package.xml +18 -0
app/code/local/Indianic/Couponimpexport/Model/Convert/Adapter/Couponimpexport.php
ADDED
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
class Indianic_Couponimpexport_Model_Convert_Adapter_Couponimpexport extends Mage_Catalog_Model_Convert_Adapter_Product
|
3 |
+
|
4 |
+
{
|
5 |
+
public function saveRow( array $data )
|
6 |
+
{
|
7 |
+
$code = $data['coupon'];
|
8 |
+
$customer= $data['customer'];
|
9 |
+
$amt = $data['amount'];
|
10 |
+
|
11 |
+
if($data['fromdate']){
|
12 |
+
$fromDate = strtotime($data['fromdate']);
|
13 |
+
} else {
|
14 |
+
$fromdate = date('d-m-Y');
|
15 |
+
}
|
16 |
+
|
17 |
+
$toDate = strtotime($data['todate']);
|
18 |
+
$description = $data['description'];
|
19 |
+
$skuStr = $data['sku'];
|
20 |
+
|
21 |
+
$deleteId = $this->isExistcode($code);
|
22 |
+
|
23 |
+
if($code != '' && $amt != ''){
|
24 |
+
if($deleteId) {
|
25 |
+
$this->deleteRule($deleteId); //delete existing coupon code
|
26 |
+
$this->generateRule($code,$customer,$amt,$fromDate,$toDate,$description,$sortOrder,$skuStr);
|
27 |
+
} else {
|
28 |
+
$this->generateRule($code,$customer,$amt,$fromDate,$toDate,$description,$sortOrder,$skuStr);
|
29 |
+
}
|
30 |
+
|
31 |
+
}
|
32 |
+
|
33 |
+
|
34 |
+
}
|
35 |
+
|
36 |
+
public function isExistcode($singlecode)
|
37 |
+
{
|
38 |
+
$allCode = $rule = Mage::getModel('salesrule/rule')->getCollection();
|
39 |
+
$codes = array();
|
40 |
+
foreach ($allCode as $row) {
|
41 |
+
$codes[$row->rule_id] = $row->code;
|
42 |
+
}
|
43 |
+
return array_search($singlecode, $codes);
|
44 |
+
}
|
45 |
+
|
46 |
+
public function deleteRule($id) {
|
47 |
+
$rule = Mage::getModel('salesrule/rule');
|
48 |
+
$rule->load($id);
|
49 |
+
$rule->delete();
|
50 |
+
}
|
51 |
+
|
52 |
+
public function generateRule($code,$customer_name,$disAmt,$fromDate,$toDate,$description,$sortOrder,$skuStr){
|
53 |
+
|
54 |
+
$rule = Mage::getModel('salesrule/rule');
|
55 |
+
$rule->setName($customer_name);
|
56 |
+
$rule->setDescription($description);
|
57 |
+
if($fromDate) {
|
58 |
+
$rule->setFromDate($fromDate);//starting today
|
59 |
+
} else {
|
60 |
+
$rule->setFromDate(date('d-m-Y'));//starting today
|
61 |
+
}
|
62 |
+
|
63 |
+
if($toDate != '') {
|
64 |
+
$rule->setToDate($toDate);//if you need an expiration date
|
65 |
+
}
|
66 |
+
$rule->setCouponType(2); //specific counpon
|
67 |
+
$rule->setCouponCode($code);
|
68 |
+
$rule->setUsesPerCoupon(1);//number of allowed uses for this coupon
|
69 |
+
$rule->setUsesPerCustomer(1);//number of allowed uses for this coupon for each customer
|
70 |
+
$rule->setCustomerGroupIds($this->getAllCustomerGroups());//if you want only certain groups replace getAllCustomerGroups() with an array of desired ids
|
71 |
+
$rule->setIsActive(1);
|
72 |
+
$rule->setStopRulesProcessing(0);//set to 1 if you want all other rules after this to not be processed
|
73 |
+
$rule->setIsRss(1);//set to 1 if you want this rule to be public in rss
|
74 |
+
$rule->setIsAdvanced(1);
|
75 |
+
|
76 |
+
$rule->setProductIds('');
|
77 |
+
|
78 |
+
if($sortOrder) {
|
79 |
+
$rule->setSortOrder($sortOrder);// order in which the rules will be applied
|
80 |
+
} else {
|
81 |
+
$rule->setSortOrder(0);// order in which the rules will be applied
|
82 |
+
}
|
83 |
+
|
84 |
+
|
85 |
+
$rule->setSimpleAction('by_percent');
|
86 |
+
//all available discount types
|
87 |
+
//by_percent - Percent of product price discount
|
88 |
+
//by_fixed - Fixed amount discount
|
89 |
+
//cart_fixed - Fixed amount discount for whole cart
|
90 |
+
//buy_x_get_y - Buy X get Y free (discount amount is Y)
|
91 |
+
|
92 |
+
$rule->setDiscountAmount($disAmt);//the discount amount/percent. if SimpleAction is by_percent this value must be <= 100
|
93 |
+
$rule->setDiscountQty(0);//Maximum Qty Discount is Applied to
|
94 |
+
$rule->setDiscountStep(0);//used for buy_x_get_y; This is X
|
95 |
+
$rule->setSimpleFreeShipping(0);//set to 1 for Free shipping
|
96 |
+
$rule->setApplyToShipping(1);//set to 0 if you don't want the rule to be applied to shipping
|
97 |
+
$rule->setWebsiteIds($this->getAllWbsites());//if you want only certain websites replace getAllWbsites() with an array of desired ids
|
98 |
+
|
99 |
+
$conditions = array();
|
100 |
+
$conditions[1] = array(
|
101 |
+
'type' => 'salesrule/rule_condition_combine',
|
102 |
+
'aggregator' => 'all',
|
103 |
+
'value' => "1", //[UPDATE] added quotes on the value(If ALL of these conditions are TRUE) set 0 for FALSE.
|
104 |
+
'new_child' => ''
|
105 |
+
);
|
106 |
+
//the conditions above are for 'if all of these conditions are true'
|
107 |
+
//for if any one of the conditions is true set 'aggregator' to 'any'
|
108 |
+
//for if all of the conditions are false set 'value' to 0.
|
109 |
+
//for if any one of the conditions is false set 'aggregator' to 'any' and 'value' to 0
|
110 |
+
/*$conditions['1--1'] = Array
|
111 |
+
(
|
112 |
+
'type' => 'salesrule/rule_condition_address',
|
113 |
+
'attribute' => 'base_subtotal',
|
114 |
+
'operator' => '>=',
|
115 |
+
'value' => 200
|
116 |
+
); */
|
117 |
+
|
118 |
+
//the constraints above are for 'Subtotal is equal or grater than 200'
|
119 |
+
//for 'equal or less than' set 'operator' to '<='... You get the idea other operators for numbers: '==', '!=', '>', '<'
|
120 |
+
//for 'is one of' set operator to '()';
|
121 |
+
//for 'is not one of' set operator to '!()';
|
122 |
+
//in this example the constraint is on the subtotal
|
123 |
+
//for other attributes you can change the value for 'attribute' to: 'total_qty', 'weight', 'payment_method', 'shipping_method', 'postcode', 'region', 'region_id', 'country_id'
|
124 |
+
|
125 |
+
//to add an other constraint on product attributes (not cart attributes like above) uncomment and change the following:
|
126 |
+
if($skuStr){
|
127 |
+
|
128 |
+
$conditions['1--2'] = array
|
129 |
+
(
|
130 |
+
'type' => 'salesrule/rule_condition_product_found',//-> means 'if all of the following are true' - same rules as above for 'aggregator' and 'value'
|
131 |
+
//other values for type: 'salesrule/rule_condition_product_subselect' 'salesrule/rule_condition_combine'
|
132 |
+
'value' => 1, //set 0 for not Found,1 is for Found
|
133 |
+
'aggregator' => 'all',
|
134 |
+
'new_child' => '',
|
135 |
+
);
|
136 |
+
|
137 |
+
|
138 |
+
$conditions['1--2--1'] = array
|
139 |
+
(
|
140 |
+
'type' => 'salesrule/rule_condition_product',
|
141 |
+
'attribute' => 'sku',
|
142 |
+
'operator' => '()',
|
143 |
+
'value' => $skuStr,
|
144 |
+
);
|
145 |
+
|
146 |
+
}
|
147 |
+
|
148 |
+
//$conditions['1--2--1'] means sku equals 12. For other constraints change 'attribute', 'operator'(see list above), 'value'
|
149 |
+
|
150 |
+
$rule->setData('conditions',$conditions);
|
151 |
+
$rule->loadPost($rule->getData());
|
152 |
+
$rule->save();
|
153 |
+
|
154 |
+
//[UPDATE]if you work with Mangento EE and you want to link banners to your rule uncomment the line of code below
|
155 |
+
//Mage::getResourceModel('enterprise_banner/banner')->bindBannersToSalesRule($rule->getId(), array(1,2));//the array(1,2, ...) is the array with all the banners you want to link to the rule.
|
156 |
+
//[/UPDATE]
|
157 |
+
}
|
158 |
+
|
159 |
+
public function getAllCustomerGroups(){
|
160 |
+
//get all customer groups
|
161 |
+
$customerGroups = Mage::getModel('customer/group')->getCollection();
|
162 |
+
$groups = array();
|
163 |
+
foreach ($customerGroups as $group){
|
164 |
+
$groups[] = $group->getId();
|
165 |
+
}
|
166 |
+
return $groups;
|
167 |
+
}
|
168 |
+
|
169 |
+
|
170 |
+
public function getAllWbsites(){
|
171 |
+
//get all wabsites
|
172 |
+
$websites = Mage::getModel('core/website')->getCollection();
|
173 |
+
$websiteIds = array();
|
174 |
+
foreach ($websites as $website){
|
175 |
+
$websiteIds[] = $website->getId();
|
176 |
+
}
|
177 |
+
return $websiteIds;
|
178 |
+
}
|
179 |
+
|
180 |
+
}
|
181 |
+
|
app/code/local/Indianic/Couponimpexport/Model/Convert/Parser/Couponimpexport.php
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
class Indianic_Couponimpexport_Model_Convert_Parser_Couponimpexport extends Mage_Eav_Model_Convert_Parser_Abstract
|
4 |
+
{
|
5 |
+
const MULTI_DELIMITER = ' , ';
|
6 |
+
|
7 |
+
public function unparse()
|
8 |
+
{
|
9 |
+
$salesRules = Mage::getModel('salesrule/rule')->getCollection();
|
10 |
+
$csv_fields = array();
|
11 |
+
|
12 |
+
$i = 1;
|
13 |
+
|
14 |
+
if($salesRules) {
|
15 |
+
foreach ($salesRules as $rules) {
|
16 |
+
$rule = Mage::getModel('salesrule/rule')->load($rules->getRuleId());
|
17 |
+
if($rules->getCode() != ''){
|
18 |
+
$conditionsCol = unserialize($rules->getConditionsSerialized());
|
19 |
+
$conditions = $conditionsCol['conditions'];
|
20 |
+
for ($i=0; $i< count($conditions); $i++){
|
21 |
+
if(isset($conditions[$i]['conditions'])){
|
22 |
+
$getProductsku = $conditions[$i]['conditions'];
|
23 |
+
for ($j=0;$j<count($getProductsku);$j++){
|
24 |
+
if($getProductsku[$j]['attribute'] == 'sku'){
|
25 |
+
$productSku = $getProductsku[$j]['value'];
|
26 |
+
}
|
27 |
+
}
|
28 |
+
}
|
29 |
+
}
|
30 |
+
}
|
31 |
+
$csv_fields['Code'] = $rule->getCouponCode();
|
32 |
+
$csv_fields['Customer'] = $rule->getName();
|
33 |
+
$csv_fields['Amount'] = $rule->getDiscountAmount();
|
34 |
+
$csv_fields['Fromdate'] = $rule->getFromDate();
|
35 |
+
$csv_fields['Todate'] = $rule->getToDate();
|
36 |
+
$csv_fields['Description'] = $rule->getDescription();
|
37 |
+
$csv_fields['Sku'] = $productSku;
|
38 |
+
|
39 |
+
$batchExport = $this->getBatchExportModel()
|
40 |
+
->setId(null)
|
41 |
+
->setBatchId($this->getBatchModel()->getId())
|
42 |
+
->setBatchData($csv_fields)
|
43 |
+
->setStatus(1)
|
44 |
+
->save();
|
45 |
+
|
46 |
+
}
|
47 |
+
}
|
48 |
+
|
49 |
+
return $this;
|
50 |
+
}
|
51 |
+
public function parse()
|
52 |
+
{
|
53 |
+
|
54 |
+
}
|
55 |
+
}
|
app/code/local/Indianic/Couponimpexport/Model/Couponimpexport.php
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
class Indianic_Couponimpexport_Model_Review extends Mage_Core_Model_Abstract
|
4 |
+
{
|
5 |
+
public function _construct()
|
6 |
+
{
|
7 |
+
parent::_construct();
|
8 |
+
$this->_init('couponimpexport/couponimpexport');
|
9 |
+
}
|
10 |
+
}
|
app/code/local/Indianic/Couponimpexport/etc/config.xml
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<!--
|
3 |
+
/**
|
4 |
+
* @category Indianic
|
5 |
+
* @package Indianic_Review
|
6 |
+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
7 |
+
*/
|
8 |
+
-->
|
9 |
+
<config>
|
10 |
+
<modules>
|
11 |
+
<Indianic_Couponimpexport>
|
12 |
+
<version>0.1.0</version>
|
13 |
+
</Indianic_Couponimpexport>
|
14 |
+
</modules>
|
15 |
+
|
16 |
+
|
17 |
+
<global>
|
18 |
+
<models>
|
19 |
+
<Couponimpexport>
|
20 |
+
<class>Indianic_Couponimpexport_Model</class>
|
21 |
+
<resourceModel>couponimpexport_mysql4</resourceModel>
|
22 |
+
</Couponimpexport>
|
23 |
+
|
24 |
+
</models>
|
25 |
+
</global>
|
26 |
+
</config>
|
app/etc/modules/Indianic_Couponimpexport.xml
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<!--
|
3 |
+
/**
|
4 |
+
* @category Indianic
|
5 |
+
* @package Indianic_Review
|
6 |
+
* @author ModuleCreator
|
7 |
+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
8 |
+
*/
|
9 |
+
-->
|
10 |
+
<config>
|
11 |
+
<modules>
|
12 |
+
<Indianic_Couponimpexport>
|
13 |
+
<active>true</active>
|
14 |
+
<codePool>local</codePool>
|
15 |
+
</Indianic_Couponimpexport>
|
16 |
+
</modules>
|
17 |
+
</config>
|
package.xml
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<package>
|
3 |
+
<name>Indianic_Couponimpexport</name>
|
4 |
+
<version>1.0.0.1</version>
|
5 |
+
<stability>stable</stability>
|
6 |
+
<license uri="http://www.opensource.org/licenses/osl-3.0.php">v3.0</license>
|
7 |
+
<channel>community</channel>
|
8 |
+
<extends/>
|
9 |
+
<summary>This module allows you to import and export your Shopping Cart Price Rules data such as code, customer name,from date, todate...etc..</summary>
|
10 |
+
<description>This module allows you to import and export your Shopping Cart Price Rules data such as code, customer name,from date, todate...etc..</description>
|
11 |
+
<notes>Coupon Import-Export Extension</notes>
|
12 |
+
<authors><author><name>Sanjay</name><user>sanju_patolia</user><email>sanjay.patolia@indianic.com</email></author></authors>
|
13 |
+
<date>2013-04-19</date>
|
14 |
+
<time>12:13:36</time>
|
15 |
+
<contents><target name="magelocal"><dir name="Indianic"><dir name="Couponimpexport"><dir name="Model"><dir name="Convert"><dir name="Adapter"><file name="Couponimpexport.php" hash="06c0247ec5de16aed0005e08cc39795a"/></dir><dir name="Parser"><file name="Couponimpexport.php" hash="869cc74fbb6829e2de2ad7a683365556"/></dir></dir><file name="Couponimpexport.php" hash="d4785c0014b45b0462cc713aa23c9bab"/></dir><dir name="etc"><file name="config.xml" hash="7a15664f09e938375423915bcbaf0235"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Indianic_Couponimpexport.xml" hash="b17ea9b4d7d04c65728a3678899c1348"/></dir></target></contents>
|
16 |
+
<compatible/>
|
17 |
+
<dependencies><required><php><min>5.1.0</min><max>6.0.0</max></php></required></dependencies>
|
18 |
+
</package>
|