The previous article is about the design of the shopping cart, which is about promotion. The supported promotions are similar to discounts, how many gifts to buy, and how much money to give gifts. The general idea of the rule mode is used. each promotion requires a new promotion class and a special switch 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.
The 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;
}
The 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;
}
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.
The 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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.