我已經制作了一個小模塊,我正在測試哪一種編程方式基於默認優惠券創建優惠券代碼,該優惠券代碼爲顧客提供5%的折扣。Magento:停止使用自動應用的優惠券代碼
優惠券適用於一個用戶和一個客戶。優惠券可生成罰款,並在促銷>購物車價格規則下顯示,規則名稱與優惠券代碼相同。
我遇到的問題是,我想它來創建優惠券,但直到客戶在折扣碼輸入優惠券代碼領域的前端,而是當我添加不將它應用到籃下產品到購物籃,優惠券適用折扣。
我的問題是如何阻止它自動添加優惠券。
如果有更好的方式創建優惠券代碼和規則編程,我想知道。
我,如果它的事項代碼如下:
public function createCoupon() {
// Get the rule in question
$rule = Mage::getModel('salesrule/rule')->load(1); //1 = ID of coupon in question
// Define a coupon code generator model instance
// Look at Mage_SalesRule_Model_Coupon_Massgenerator for options
$generator = Mage::getModel('salesrule/coupon_massgenerator');
$parameters = array(
'count'=> 1,
'format'=> 'alphanumeric',
'dash_every_x_characters'=> 4,
'prefix'=> 'XXX-',
'suffix'=> '-CODE5',
'length'=> 8
);
if(!empty($parameters['format'])) {
switch(strtolower($parameters['format'])) {
case 'alphanumeric':
case 'alphanum':
$generator->setFormat(Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC);
break;
case 'alphabetical':
case 'alpha':
$generator->setFormat(Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHABETICAL);
break;
case 'numeric':
case 'num':
$generator->setFormat(Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_NUMERIC);
break;
}
}
$generator->setDash(!empty($parameters['dash_every_x_characters'])? (int) $parameters['dash_every_x_characters'] : 0);
$generator->setLength(!empty($parameters['length']) ? (int) $parameters['length'] : 6);
$generator->setPrefix(!empty($parameters['prefix']) ? $parameters['prefix'] : '');
$generator->setSuffix(!empty($parameters['suffix']) ? $parameters['suffix'] : '');
// Set the generator, and coupon type so it's able to generate
$rule->setCouponCodeGenerator($generator);
$rule->setCouponType(Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO);
// Get as many coupons as you required
$count = !empty($parameters['count'])? (int) $parameters['count'] : 1;
$codes = array();
for($i = 0; $i < $count; $i++) {
$coupon = $rule->acquireCoupon();
$code = $coupon->getCode();
$codes[] = $code;
}
return $codes[0];
}
public function createCouponForLike($couponCode) {
$model = Mage::getModel('salesrule/rule');
$model->setName($couponCode);
$model->setDescription('Discount coupon for liking us on Facebook.');
$model->setFromDate(date('Y-m-d'));
$model->setCouponCode($couponCode);
$model->setUsesPerCoupon(1);
$model->setUsesPerCustomer(1);
$model->setCustomerGroupIds('0,1');
$model->setIsActive(1);
$model->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";}');
$model->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";}');
$model->setStopRulesProcessing(0);
$model->setIsAdvanced(1);
$model->setProductIds('');
$model->setSortOrder(1);
$model->setSimpleAction('by_percent');
$model->setDiscountAmount(5);
$model->setDiscountStep(0);
$model->setSimpleFreeShipping(0);
$model->setTimesUsed(0);
$model->setIsRss(0);
$model->setWebsiteIds('1');
$model->save();
}
請記住要添加一項可以清理舊使用過的作業,否則最終會遇到大量購物車價格規則,這些規則可能會導致添加購物車操作的速度變慢。 – McNab