2013-03-06 33 views
6

Dựa trên các bài đăng trên stackoverflow khác, tôi đã có thể sử dụng mã sau đây để tạo các phiếu giảm giá quy tắc giá giỏ hàng cá nhân theo lập trình trong Magento.Lập trình tự động tạo mã phiếu giảm giá được tạo tự động trong Magento?

Làm cách nào tôi có thể gọi tính năng "Tự động tạo phiếu thưởng" theo chương trình để tạo 100 phiếu thưởng duy nhất cho từng quy tắc giá mà tôi thực hiện? Cảm ơn!

$coupon = Mage::getModel('salesrule/rule'); 
$coupon->setName($_coupon['name']) 
     ->setDescription('this is a description') 
     ->setFromDate(date('Y-m-d')) 
     ->setCouponType(2) 
     ->setCouponCode($_coupon['code']) 
     ->setUsesPerCoupon(1000) 
     ->setUsesPerCustomer(100) 
     ->setCustomerGroupIds(array(1)) //an array of customer groupids 
     ->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('by_percent') 
     ->setDiscountAmount(100) 
     ->setDiscountQty(null) 
     ->setDiscountStep('0') 
     ->setSimpleFreeShipping('0') 
     ->setApplyToShipping('0') 
     ->setIsRss(0) 
     ->setWebsiteIds(array(1));  
$coupon->save(); 

Ví dụ, một quy tắc mức giá này có thể có một danh sách toàn bộ mã số Coupon Auto-Generated (htgf-7774, htgf-2345, vv) sử dụng các chức năng có sẵn khi tự tạo ra quy tắc giá trong quản trị bảng điều khiển.

EDIT:

tôi đã nhận được gần hơn, sử dụng đoạn mã sau. Vẫn không biết làm thế nào để đặc biệt gán mô hình thế hệ tự động

->setName('Name') 
->setDescription('this is a description') 
->setFromDate('2013-03-06') 
->setToDate(NULL) 
->setUsesPerCustomer('100') 
->setIsActive('1') 
->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(NULL) 
->setSortOrder('0') 
->setSimpleAction('by_percent') 
->setDiscountAmount('100.0000') 
->setDiscountQty(NULL) 
->setDiscountStep('0') 
->setSimpleFreeShipping('0') 
->setApplyToShipping('0') 
->setTimesUsed('1') 
->setIsRss('0') 
->setCouponType('2') 
->setUseAutoGeneration('1') 
->setUsesPerCoupon('1000') 
->setCustomerGroupIds(array('1',)) 
->setWebsiteIds(array('1',)) 
->setCouponCode(NULL) 
+0

phiên bản Magento này là gì? – Meabed

+0

phiên bản 1.702 (cài đặt gần đây) –

+0

Tôi vui mừng báo cáo rằng điều này hoạt động trong Magento 1.9. yay. –

Trả lời

15

Nhờ một bài tiện lợi Tôi tìm thấy trong khi googling này (http://fragmentedthought.com/fragments/programatically-creating-sales-rule-coupon-code), tôi đã trả lời câu hỏi của riêng tôi:

// Get the rule in question 
$rule = Mage::getModel('salesrule/rule')->load(21); //21 = 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'=>5, 
    'format'=>'alphanumeric', 
    'dash_every_x_characters'=>4, 
    'prefix'=>'ABCD-EFGH-', 
    'suffix'=>'-WXYZ', 
    '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; 

này tạo ra thành công các mã sau:

ABCD-EFGH-ZC6V-ZJWD-WXYZ 
ABCD-EFGH-4XMX-353L-WXYZ 
ABCD-EFGH-XCJB-5GQI-WXYZ 
ABCD-EFGH-UEAO-L1NJ-WXYZ 
ABCD-EFGH-59B3-50T2-WXYZ 
+7

Điều này làm việc cho tôi như được mô tả, mã được tạo, nhưng trên giao diện người dùng tôi không thể sử dụng mã phiếu giảm giá vì theo Magento các mã không hợp lệ, mặc dù chúng hiển thị trong Bảng điều khiển quản trị Magento. Những gì tôi đã phải thay đổi để làm cho họ làm việc là để thêm những điều sau đây từ liên kết bạn cung cấp: '$ coupon-> setType (Mage_SalesRule_Helper_Coupon :: COUPON_TYPE_SPECIFIC_AUTOGENERATED) -> save();'. Điều này phải được thực hiện cho mỗi phiếu giảm giá, vì vậy lý tưởng nó sẽ được thêm ngay sau dòng sau trong đoạn mã của bạn: '$ coupon = $ rule-> obtainCoupon()'. –

+0

Làm thế nào an toàn là điều này? Điều này có thể không tạo ra một quy tắc đã tồn tại? –

+0

Cảm ơn @LouisBataillard, thậm chí nó không lưu các phiếu giảm giá mà không có dòng này. V1.9.2.4 –

Các vấn đề liên quan