Là nguyên tắc chung, bạn có thể làm bất cứ điều gì mà hệ thống Magento tự làm mà không cần viết một dòng SQL. Hầu như tất cả các cấu trúc dữ liệu của Magento đều sử dụng các lớp Mô hình Magento.
Chạy mã sau ở đâu đó để xem mô hình bán hàng/quy tắc trông như thế nào. Điều này giả định bạn đã tạo một đơn Giỏ hàng Giá Rule trong quản trị với một ID của 1
$coupon = Mage::getModel('salesrule/rule')->load(1);
var_dump($coupon->getData());
Sử dụng dữ liệu đổ như một hướng dẫn, chúng tôi programatically có thể tạo ra một mô hình bằng cách sử dụng sau đây
$coupon = Mage::getModel('salesrule/rule');
$coupon->setName('test coupon')
->setDescription('this is a description')
->setFromDate('2010-05-09')
->setCouponCode('CODENAME')
->setUsesPerCoupon(1)
->setUsesPerCustomer(1)
->setCustomerGroupIds(array(1)) //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('by_percent')
->setDiscountAmount(10)
->setDiscountQty(null)
->setDiscountStep('0')
->setSimpleFreeShipping('0')
->setApplyToShipping('0')
->setIsRss(0)
->setWebsiteIds(array(1));
$coupon->save();
Đối với bất kỳ ai tò mò, ở trên là mã được tạo, sử dụng kỹ thuật được thảo luận here
Nguồn
2010-05-09 17:12:24
Tôi chỉ cần chạy vào cùng một vấn đề chính xác như Ken đăng, bên dưới. Các hành động không được thiết lập bởi setActionsSerialized() – Laizer
Vâng, có vẻ như các hành động được lưu trữ dưới dạng Mô hình riêng biệt trong hệ thống ở đâu đó và sau đó được thêm vào salesrule/rule. Tôi đoán là trường serialized tồn tại để truy cập nhanh hơn. Vì vậy, sau khi thực hiện ở trên, bạn sẽ tự thêm chúng (thông qua một số phương pháp, hoặc có thể thiết lập id quy tắc của họ). –
Yup - trông giống như Mage_SalesRule_Model_Rule_Action_Product – Laizer