3

有没有一种已知的方法可以通过 API 在 Magento 社区版中创建新的优惠券代码(购物车价格规则)?

我希望能够使用另一个 web 应用程序自动生成优惠券代码,并通过一些后端通信同时在 Magento 中创建它们。据我所知,默认 API 对此不提供支持。

有谁知道如何去做?

谢谢!

4

1 回答 1

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不支持销售规则。

于 2012-07-24T16:55:11.273 回答