通过API创建Magento优惠券

时间:2012-07-23 21:59:35

标签: magento

是否有通过API在Magento Community Edition中创建新优惠券代码(购物车价格规则)的已知方法?

我希望能够使用其他网络应用程序自动生成优惠券代码,并通过一些后端通信同时在Magento中创建优惠券代码。从我可以收集的内容来看,默认API中没有支持。

有没有人知道如何去做?

谢谢!

1 个答案:

答案 0 :(得分:5)

这是我用来创建多个折扣代码的脚本。

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);
}

代码几乎都有记录。

Magento API不支持销售规则。