The general idea is that a promotion class should be created for each promotion, and a special switch is provided to control whether the promotion is effective.
Use the sales identification code in the product to determine which sales promotion instance to call.
First, you can add a product in two steps. The first step is to add the product status. The second step is to display the product status in the shopping cart.
1. Add steps:
1. traverse all promotion mutex conditions before adding products.
For example, a product cannot be in the same shopping cart with another product; or a user can not buy a specific product.
2. Select a specific promotion instance before adding the product.
Note: The difference between the second and the first is that 1 is to traverse all promotion instances, and 2 is a separate one.
3. After adding a product, perform the operations based on the specific promotion instance.
Copy codeThe Code is as follows :/**
* Add a product to the shopping cart.
* @ Param int $ goods_id product ID
* @ Param string $ goods_spec Product Specification
* @ Param int $ goods_number number of items
* @ Param string $ promote_name participate in the activity
* @ Return bool
*/
Public function goodsAdd ($ goods_id, $ goods_spec, $ goods_number, $ promote_name)
{
// Obtain all valid promotion instances
$ Rules = $ this-> _ getAllRuleInstance ();
Foreach ($ this-> _ rules as $ instance)
{
// Mutual exclusion judgment
If (! $ Instance-> goodsExclusion ($ goods_id, $ goods_spec ))
{
Return false;
}
}
// Obtain a separate promotion instance for the product
$ Rule = $ this-> _ getRuleInstance ($ promote_name );
// Before adding a product
If ($ rule-> beforeGoodsAdd ())
{
$ Rule-> goodsAdd ($ goods_id, $ goods_spec, $ goods_number );
// Perform operations after adding a product
Return $ rule-> afterGoodsAdd ();
}
Return false;
}
Copy codeThe Code is as follows :/**
* Getting the list of available rule instances
* @ Return array
*/
Private function _ getAllRuleInstance ()
{
If (empty ($ this-> _ rules ))
{
$ Dir = dirname (_ FILE _). '/Cart/Rule /';
$ Dir_handle = opendir ($ dir );
While ($ file = readdir ($ dir_handle ))
{
If (is_file ($ dir. $ file ))
{
$ Instance = $ this-> _ getRuleInstance (substr ($ file, 0, strpos ($ file ,'.')));
If ($ instance-> enabled ())
{
$ This-> _ rules [] = $ instance;
}
}
}
}
Return $ this-> _ rules;
}
Copy codeThe Code is as follows :/**
* Getting shopping cart rules
* @ Param string $ name rule name
* @ Return Bll_Shop_Cart_Rule
*/
Private function _ getRuleInstance ($ name)
{
$ Rule_name = 'bll _ Shop_Cart_Rule _ '. $ name;
Try
{
Zend_Loader: loadClass ($ rule_name );
$ This-> _ rule = new $ rule_name ();
$ This-> _ rule-> setCart ($ this );
Return $ this-> _ rule;
} Catch (Exception $ e)
{
Bll_LogWriter: logException ('shopping rule object loading exception. rule_name: '. $ rule_name );
Throw new Exception ('shopping rule object loading Exception .');
}
}
The main purpose of the promotion here is to determine whether a person has the permission to add the product, such as discounts.
2. Traverse shopping cart operations
In this step, the key operation is to traverse all the inspection list functions of the promotion policy.
Here, we often use promotions such as full money, gifts, buy two get one free, and so on.Copy codeThe Code is as follows :/**
* Get the list of items in the shopping cart.
* @ Return array Bll_Shop_Cart_Rule
*/
Public function goodsViewList ()
{
$ List = $ this-> getGoodsList ();
// Check the item list in the shopping cart when listing
$ Rules = $ this-> _ getAllRuleInstance ();
Foreach ($ this-> _ rules as $ instance)
{
$ Instance-> setGoodsList ($ list)-> goodsCheckList ();
$ This-> _ tip_rules [] = $ instance;
}
// Obtain the latest shopping cart list
$ Goods_list = $ this-> _ cart-> getGoodsList ();
Return $ goods_list;
}
Third, the operations before submitting the order
There are some types of promotions. For example, if someone has the discount permission, after the order is placed, the discount permission is used up. Or, before placing an order, check the order amount, if the number is less than the number, you are not allowed to place this order.
All of the above operations will be performed before the order is submitted.