Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8286325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:42:48+00:00 2026-06-08T11:42:48+00:00

Is there a known way to create a new coupon code (Shopping Cart Price

  • 0

Is there a known way to create a new coupon code (Shopping Cart Price Rule) in Magento Community Edition through the API?

I would like to be able to auto-generate coupon codes using another webapp, and have them created in Magento simultaneously via some back-end communication. From what I can gather, there is no support in the default API for this.

Does anyone know of a way to go about it?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:42:52+00:00Added an answer on June 8, 2026 at 11:42 am

    Here is my script that I use to create multiple discount codes.

    require_once '../app/Mage.php';
    Varien_Profiler::enable();
    Mage::setIsDeveloperMode(true);
    ini_set('display_errors', 1);
    
    umask(0);
    Mage::app();
    
    $code = $code = generateUniqueId(10); //coupon code
    $amount = 10;   // discount amount
    
    generateRule( $code, $amount, 'label', date('Y-m-d'));
    
    function generateRule($code, $amount, $label, $from_date = '', $to_date = '', $name = ''){
    
        $name = (empty($name))? $label : $name;
        $labels[0] = $label;//default store label
    
    
        $coupon = Mage::getModel('salesrule/rule');
        $coupon->setName($name)
        ->setDescription($name)
        ->setFromDate($from_date)
        ->setToDate($to_date)
        ->setCouponCode($code)
        ->setUsesPerCoupon(1)
        ->setUsesPerCustomer(1)
        ->setCustomerGroupIds(getAllCustomerGroups()) //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('cart_fixed')
        ->setDiscountAmount($amount)
        ->setDiscountQty(null)
        ->setDiscountStep('0')
        ->setSimpleFreeShipping('0')
        ->setApplyToShipping('0')
        ->setIsRss(0)
        ->setWebsiteIds(getAllWbsites())
        ->setCouponType(2)
        ->setStoreLabels($labels)
        ;
        $coupon->save();
    }
    
    
    function getAllCustomerGroups(){
        //get all customer groups
        $customerGroupsCollection = Mage::getModel('customer/group')->getCollection();
        $customerGroupsCollection->addFieldToFilter('customer_group_code',array('nlike'=>'%auto%'));
    //    $customerGroupsCollection->load();
        $groups = array();
        foreach ($customerGroupsCollection as $group){
        $groups[] = $group->getId();
        }
        return $groups;
    }
    
    function getAllWbsites(){
        //get all wabsites
        $websites = Mage::getModel('core/website')->getCollection();
        $websiteIds = array();
        foreach ($websites as $website){
        $websiteIds[] = $website->getId();
        }
        return $websiteIds;
    }
    
    function generateUniqueId($length = null){
        $rndId = crypt(uniqid(rand(),1));
        $rndId = strip_tags(stripslashes($rndId));
        $rndId = str_replace(array(".", "$"),"",$rndId);
        $rndId = strrev(str_replace("/","",$rndId));
        if (!is_null($rndId)){
            return strtoupper(substr($rndId, 0, $length));
        }
        return strtoupper($rndId);
    }
    

    The code is pretty much documented.

    Magento API does not support sales rules.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone know if there is a standard way to create a List from an
I need to know if there is a way to create a custom shortcut
I would like to know if there is a way to create web pages
I'm a beginner in F#. I know that there is the way to create
I would like to know if there is a way to take create a
I wish to know is there any way that I can create the threads
Using jquery UI 1.8's autocomplete, is there any known way to take the results
Hi I was wondering if there is any known way to get rid of
There is a widely known way of locking multiple locks, which relies on choosing
I'm new to java and I wonder if there is simple way to know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.