Example code of php online shopping mall promotion design

  • 2020-05-16 06:25:37
  • OfStack

General idea, every promotion to create a new promotion class, there is a special switch to control whether to take effect.
Use the promotion identification number inside the product to determine which promotion instance to call.
First, there are two steps when you add a product. The first step is to add the status, and the second step is to display the status of the product in the shopping cart.
1. Add several important points in the steps:
1. Walk through all the promotional mutex before adding the item.
For example, a certain product cannot be in a shopping cart at the same time as another product. Or a user permission, can not buy a specific item and so on.
2. Before adding the product, select the specific promotion instance to do the operation before adding the product.
Note: the difference between point 2 and point 1 is that 1 traverses all promotion instances and 2 is a separate item.
3. The operation to be carried out after adding the product according to the specific promotion instance.
 
/** 
*  Add items to the shopping cart  
* @param int $goods_id  goods ID 
* @param string $goods_spec  Product specifications  
* @param int $goods_number  The number  
* @param string $promote_name  Commodity participation activities  
* @return bool 
*/ 
public function goodsAdd($goods_id, $goods_spec, $goods_number, $promote_name) 
{ 
// Get all valid promotion instances  
$rules = $this->_getAllRuleInstance(); 
foreach($this->_rules as $instance) 
{ 
// Exchange of gifts is mutually exclusive  
if(!$instance->goodsExclusion($goods_id, $goods_spec)) 
{ 
return false; 
} 
} 
// Get a separate promotional instance of the item  
$rule = $this->_getRuleInstance($promote_name); 
// Action before adding the item  
if($rule->beforeGoodsAdd()) 
{ 
$rule->goodsAdd($goods_id, $goods_spec, $goods_number); 
// After adding the item  
return $rule->afterGoodsAdd(); 
} 
return false; 
} 

 
/** 
*  Gets a 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; 
} 

 
/** 
*  Gets the shopping cart rule class  
* @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(' The shopping rule object loads an exception . rule_name:'.$rule_name); 
throw new Exception(' The shopping rule object loads an exception .'); 
} 
} 

The main promotion used here is to judge whether a certain person has the right to add this product, discount and so on.
2. Traverse the operation of the shopping cart
The key action to perform in this 1 step is to traverse the checklist function for all promotion strategies.
One of the things that you can use a lot here is how much money is it, give something away, buy 2 get 1 free, etc.
 
/** 
*  Gets the goods list object list of the shopping cart  
* @return array Bll_Shop_Cart_Rule 
*/ 
public function goodsViewList() 
{ 
$list = $this->getGoodsList(); 
//  Check the list of items in the shopping cart while making a list  
$rules = $this->_getAllRuleInstance(); 
foreach($this->_rules as $instance) 
{ 
$instance->setGoodsList($list)->goodsCheckList(); 
$this->_tip_rules[] = $instance; 
} 
// Get the latest shopping cart list  
$goods_list = $this->_cart->getGoodsList(); 
return $goods_list; 
} 

Third, the operation before submitting the order
There are some types of promotion, for example, someone has the right to discount, after the order is completed, the discount is used; Or check the amount of the order before placing the order, and if it is less than that, you are not allowed to place the order, etc.
All of this is done before the order is submitted.

Related articles: