Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Magento :: Create Shopping cart rule using script
 
d8bhatta
Member
 
Total Posts:  44
Joined:  2009-06-11
 

Hello Guys,

Following code creates shopping cart rule using customer group id and websites ids extracting from database, so its just matter of changing coupon code, name and description for you.

$customer_group = new Mage_Customer_Model_Group();
$allGroups  $customer_group->getCollection()->getData();
 
$customerGroupIds = array();
 foreach(
$customer_group->getCollection() as $cust)
     $customerGroupIds[] 
=$cust->getCustomerGroupId();
 
}
 $websiteCollection 
Mage::getModel('core/website')->getCollection(); //website model
 
$websitesId = array();
 foreach(
$websiteCollection as $websites{
    $websitesId[] 
=$websites->getWebsiteId();
 
}

$coupon 
Mage::getModel('salesrule/rule')->load(8);
   
// print_r($coupon->getData());
   // exit;
    
    
$coupon Mage::getModel('salesrule/rule');
    
$coupon->setName(' Order Promo Code')
        ->
setDescription(' Order Promo Code')
        ->
setFromDate('2010-05-09')
        ->
setToDate('2015-05-09')
        ->
setCouponType(2)
        ->
setCouponCode('yourcode123')
        ->
setUsesPerCoupon(1)
        ->
setUsesPerCustomer(100000000000)
        ->
setCustomerGroupIds($customerGroupIds//an array of customer grou pids
        
->setIsActive(1)
        
//serialized conditions.  the following examples are empty
        
->setConditionsSerialized(' a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
        ->
setActionsSerialized('a:6:{s:4:"type";s:40:"salesrule/rule_condition_product_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
        ->
setStopRulesProcessing(0)
        ->
setIsAdvanced(1)
        ->
setProductIds('')
        ->
setSortOrder(0)
        ->
setSimpleAction('by_percent')
        ->
setDiscountAmount(100)
        ->
setDiscountQty(null)
        ->
setDiscountStep('0')
        ->
setSimpleFreeShipping('0')
        ->
setApplyToShipping('0')
        ->
setIsRss(0)
        ->
setWebsiteIds($websitesId);      
      
$coupon->save();
 
Magento Community Magento Community
Magento Community
Magento Community
 
Luge
Jr. Member
 
Avatar
Total Posts:  10
Joined:  2010-09-22
Belarus, Minsk
 

hello, your loops

customer_group = new Mage_Customer_Model_Group();
$allGroups  $customer_group->getCollection()->getData();
 
$customerGroupIds = array();
 foreach(
$customer_group->getCollection() as $cust)
     $customerGroupIds[] 
=$cust->getCustomerGroupId();
 
}
 $websiteCollection 
Mage::getModel('core/website')->getCollection(); //website model
 
$websitesId = array();
 foreach(
$websiteCollection as $websites{
    $websitesId[] 
=$websites->getWebsiteId();
 
}
do the same as
$customerGroupIds  Mage::getModel('customer/group')->getCollection()->getAllIds();
$websitesId  Mage::getModel('core/website')->getCollection()->getAllIds();
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top