Magento Export Shopping Cart Rules Programmatically

You can get the sample code below to export Shopping Cart Rules in Magento.
<?php
/**********Exporting shopping cart rules to csv************/

require_once('app/Mage.php');
umask(0);

if (!Mage::isInstalled()) {
echo "Application is not installed yet, please complete install wizard first.";
exit;
}

// Only for urls // Don't remove this
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);

Mage::app('admin')->setUseSessionInUrl(false);
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
error_reporting(E_ALL);

try {
Mage::getConfig()->init();
Mage::app();
} catch (Exception $e) {
Mage::printException($e);
}
ini_set('memory_limit', '500M');

$customerCount = 0;
try {

$readAdapter = Mage::getSingleton('core/resource')->getConnection('core_read');
$results = $readAdapter->fetchAll('SELECT `s`.`rule_id` FROM `salesrule` as s
ORDER BY `s`.`rule_id` ASC');
$csvToCreate = 'exported-rules.csv';
$csvObj = new Varien_File_Csv();
$csvData = array();
$rulesHeader = array('rule_id', 'name', 'description', 'from_date', 'to_date', 'uses_per_customer', 'is_active'
, 'conditions_serialized', 'actions_serialized', 'stop_rules_processing', 'is_advanced', 'product_ids', 'sort_order',
'simple_action', 'discount_amount', 'discount_qty', 'discount_step', 'simple_free_shipping', 'apply_to_shipping',
'times_used', 'is_rss', 'coupon_type', 'use_auto_generation', 'uses_per_coupon', 'customer_group_ids', 'website_ids',
'coupon_code','autogenerated_coupon_codes');
$ruleCount = 0;
$headerFlag = 0;

foreach ($results as $ruleId) {
$ruleModel = Mage::getModel('salesrule/rule')->load($ruleId['rule_id']);
if (!$headerFlag) {
$csvData[] = $rulesHeader;
$headerFlag = 1;
}

if ($ruleModel->getRuleId()) {
$data = array(
'rule_id' => $ruleModel->getData('rule_id'),
'name' => $ruleModel->getData('name'),
'description' => $ruleModel->getData('description'),
'from_date' => $ruleModel->getData('from_date'),
'to_date' => $ruleModel->getData('to_date'),
'uses_per_customer' => $ruleModel->getData('uses_per_customer'),
'is_active' => $ruleModel->getData('is_active'),
'conditions_serialized' => $ruleModel->getData('conditions_serialized'),
'actions_serialized' => $ruleModel->getData('actions_serialized'),
'stop_rules_processing' => $ruleModel->getData('stop_rules_processing'),
'is_advanced' => $ruleModel->getData('is_advanced'),
'product_ids' => $ruleModel->getData('product_ids'),
'sort_order' => $ruleModel->getData('sort_order'),
'simple_action' => $ruleModel->getData('simple_action'),
'discount_amount' => $ruleModel->getData('discount_amount'),
'discount_qty' => $ruleModel->getData('discount_qty'),
'discount_step' => $ruleModel->getData('discount_step'),
'simple_free_shipping' => $ruleModel->getData('simple_free_shipping'),
'apply_to_shipping' => $ruleModel->getData('apply_to_shipping'),
'times_used' => $ruleModel->getData('times_used'),
'is_rss' => $ruleModel->getData('is_rss'),
'coupon_type' => $ruleModel->getData('coupon_type'),
'use_auto_generation' => $ruleModel->getData('use_auto_generation'),
'uses_per_coupon' => $ruleModel->getData('uses_per_coupon')
);
$customerGroupIds = $ruleModel->getData('customer_group_ids');
$commaSeparatedCustomerIds = '';
if (is_array($customerGroupIds)) {
foreach ($customerGroupIds as $customerGroupId) {
if ($commaSeparatedCustomerIds != '') {
$commaSeparatedCustomerIds .= ',' . $customerGroupId;
} else {
$commaSeparatedCustomerIds .= $customerGroupId;
trim($commaSeparatedCustomerIds);
}
}
}

$websiteIds = $ruleModel->getData('website_ids');
$commaSeparatedWebsiteIds = '';
if (is_array($websiteIds)) {
foreach ($websiteIds as $websiteId) {
if ($commaSeparatedWebsiteIds != '') {
$commaSeparatedWebsiteIds .= ',' . $websiteId;
} else {
$commaSeparatedWebsiteIds .= $websiteId;
trim($commaSeparatedWebsiteIds);
}

}
}
$data['customer_group_ids'] = $commaSeparatedCustomerIds;
$data['website_ids'] = $commaSeparatedWebsiteIds;
$data['coupon_code'] = $ruleModel->getData('coupon_code');

/** @var Mage_SalesRule_Model_Resource_Coupon_Collection */
$collections = Mage::getResourceModel('salesrule/coupon_collection');
$collections->getSelect()->joinLeft(array('sl'=> 'salesrule'),
'sl.rule_id = main_table.rule_id', array('sl.use_auto_generation'))
->where("sl.use_auto_generation = 1 and main_table.rule_id = " . $ruleModel->getData('rule_id'));
$autogeneratedCoupons = '';
if ($collections->getSize() > 0) {
$autogeneratedCoupons = array();
foreach ($collections as $key => $collection) {
$autogeneratedCoupons[$key]['coupon_id'] = $collection->getData('coupon_id');
$autogeneratedCoupons[$key]['rule_id'] = $collection->getData('rule_id');
$autogeneratedCoupons[$key]['code'] = $collection->getData('code');
$autogeneratedCoupons[$key]['usage_limit'] = $collection->getData('usage_limit');
$autogeneratedCoupons[$key]['usage_per_customer'] = $collection->getData('usage_per_customer');
$autogeneratedCoupons[$key]['times_used'] = $collection->getData('times_used');
$autogeneratedCoupons[$key]['expiration_date'] = $collection->getData('expiration_date');
$autogeneratedCoupons[$key]['is_primary'] = $collection->getData('is_primary');
$autogeneratedCoupons[$key]['created_at'] = $collection->getData('created_at');
$autogeneratedCoupons[$key]['type'] = $collection->getData('type');
}
$autogeneratedCoupons = serialize($autogeneratedCoupons);
}
$data['autogenerated_coupon_codes'] = $autogeneratedCoupons;
$csvData[] = $data;
$ruleModel->unsetData();
$ruleCount++;
}

}
$csvObj->saveData($csvToCreate, $csvData);
} catch (Exception $e) {
Mage::printException($e);
}
echo "Saved $ruleCount rules to csv file n";
?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s