2010-05-09 30 views
11

Tôi muốn tạo quy tắc giá giỏ hàng cho người dùng giảm 10% đơn đặt hàng của họ khi nào và nếu họ hoàn tất quy trình trên trang Magento của tôi.Tạo quy tắc giá giỏ mua hàng trong Magento tự động

Có phương thức here chèn quy tắc trực tiếp vào cơ sở dữ liệu. Đó là một chút xâm lấn cho thị hiếu của tôi.

Tôi sẽ sử dụng phương pháp Magento bằng cách nào?

Trả lời

18

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

+0

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

+0

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ọ). –

+0

Yup - trông giống như Mage_SalesRule_Model_Rule_Action_Product – Laizer

6

Hãy xem mã của tôi.Điều này sẽ thêm Điều kiện hành động.

$coupon_rule = Mage::getModel('salesrule/rule'); 
    $coupon_rule->setName($c_data[1]) 
    ->setDescription($c_data[2]) 
    ->setFromDate($fromDate) 
->setToDate($toDate) 
    ->setUsesPerCustomer(0) 
    ->setCustomerGroupIds(array(0,1,2,3)) //an array of customer grou pids 
    ->setIsActive(1) 
->setCouponType(2) 
->setCouponCode($c_data[0]) 
    ->setUsesPerCoupon(1) 

    //serialized conditions. the following examples are empty 
    ->setConditionsSerialized('') 

    ->setActionsSerialized('') 
    ->setStopRulesProcessing(0) 
    ->setIsAdvanced(1) 
->setProductIds('') 
    ->setSortOrder(0) 
    ->setSimpleAction('by_percent') 
    ->setDiscountAmount($c_data[5]) 
    ->setDiscountQty(1) 
    ->setDiscountStep('0') 
    ->setSimpleFreeShipping('0') 
    ->setApplyToShipping('1') 
    ->setIsRss(1) 
    ->setWebsiteIds(explode(',',$c_data[6])); 

$sku =$c_data[7];   // Put your product SKU here 
$skuCond = Mage::getModel('salesrule/rule_condition_product') 
      ->setType('salesrule/rule_condition_product') 
      ->setAttribute('sku') 
      ->setOperator('==') 
      ->setValue($sku); 
$coupon_rule->getActions()->addCondition($skuCond); 

    $coupon_rule->save(); 

echo "New Coupon was added and its ID is ".$coupon_rule->getId().'<br/>';<br/> 

Nếu bạn muốn thêm Điều kiện cho việc mua sắm nguyên tắc giá giỏ hàng sau đó làm theo ví dụ này.

$sku =$c_data[7];   // Put your product SKU here 
$found = Mage::getModel('salesrule/rule_condition_product_found') 
     ->setType('salesrule/rule_condition_product_found') 
     ->setValue(1)   // 1 == FOUND 
     ->setAggregator('all'); // match ALL conditions 
$coupon_rule->getConditions()->addCondition($found); 
$skuCond = Mage::getModel('salesrule/rule_condition_product') 
      ->setType('salesrule/rule_condition_product') 
      ->setAttribute('sku') 
      ->setOperator('==') 
      ->setValue($sku); 

$found->addCondition($skuCond);  
    $coupon_rule->save(); 



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