Search code examples
magento

Create Magento Coupon via API


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!


Solution

  • 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.