gpt4 book ai didi

magento - 通过 API 创建 Magento 优惠券

转载 作者:行者123 更新时间:2023-12-02 10:26:41 25 4
gpt4 key购买 nike

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

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

有人知道解决办法吗?

谢谢!

最佳答案

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

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

关于magento - 通过 API 创建 Magento 优惠券,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11621194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com