Home  >  Article  >  Backend Development  >  PHP online mall promotion design example code_PHP tutorial

PHP online mall promotion design example code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:21:09827browse

The general idea is that each promotion needs to create a new promotion category, with a special switch to control whether it takes effect.
Use the promotion identification code in the product to determine which promotion instance to call.
First of all, when adding products, there are two steps. The first step is to add the status, and the second step is to display the status of the products in the shopping cart.
1. Several important points in adding steps:
1. Before adding products, traverse all promotion mutually exclusive conditions.
For example, a certain product cannot be in the same shopping cart as another product at the same time; or a certain user permission cannot purchase a specific product, etc.
2. Before adding products, select a specific promotion instance to perform the operations before adding.
Note: The difference between the second point and the first point is that 1 is to traverse all promotion instances, while 2 is a separate one.
3. After adding the product, the operations to be performed are based on the specific promotion instance.

Copy code The code is as follows:

/**
* Add product to shopping cart
* @param int $goods_id product ID
* @param string $goods_spec product specification
* @param int $goods_number product quantity
* @param string $promote_name Product participation event
* @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)
{
//Mutual exclusion judgment for gift exchange
if(!$instance->goodsExclusion($goods_id, $goods_spec))
{
return false;
}
}
//Get the individual promotion instance of the product
$rule = $this->_getRuleInstance($promote_name);
//Operation before adding the product
if($rule ->beforeGoodsAdd())
{
$rule->goodsAdd($goods_id, $goods_spec, $goods_number);
//Operation after adding goods
return $rule->afterGoodsAdd ();
}
return false;
}

Copy code The code is as follows:

/**
* Get 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 code The code is as follows:

/**
* Get 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('Shopping rule object loading exception. rule_name:'.$rule_name);
throw new Exception('Shopping rule object loading exception.');
}
}

The main promotion used here is to determine whether a certain person has the permission to add this product, discounts, etc.
Second, the operation of traversing the shopping cart items
The key operation to be performed in this step is to traverse the check list function of all promotion strategies.
Promotions that can often be used here are free gifts, buy two get one free, etc.
Copy code 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 list of items in the shopping cart when listing
$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, operations before submitting an order
There are some types of promotions. For example, if someone has discount permission, the discount permission will be used after completing the order; or you need to check before placing the order. If the amount of this order is less than the amount, the order will not be placed, etc.
The above will all be used before submitting the order.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324963.htmlTechArticleThe general idea is that each promotion needs to create a new promotion class, with a special switch to control whether it takes effect. Use the promotion identification code in the product to determine which promotion instance to call. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn