大体的思想,每一个促销要新建一个促销类,有专门的开关来控制是否生效。
用商品里面的促销识别码来判断具体调用哪一个促销实例。
首先,在添加商品的时候,分两步,第一步是添加状态,第二步是把购物车内的商品显示这个状态。
一,添加步骤几个重要的点:
1,添加商品之前,遍历所有的促销互斥条件。
例如,某一款商品不可以和另一个商品同时在一个购物车内;或者某个用户权限,不可以购买特定的一件商品等等。
2,添加商品之前,选择特定的促销实例,来进行添加之前的操作。
注:第二点与第一点的区别在于1是要遍历所有的促销实例,而2是单独的一条。
3,添加商品后,根据特定的促销实例,所要进行的操作。
复制代码 代码如下:
/**
* 向购物车内添加商品
* @param int $goods_id 商品ID
* @param string $goods_spec 商品规格
* @param int $goods_number 商品数量
* @param string $promote_name 商品参加活动
* @return bool
*/
public function goodsAdd($goods_id, $goods_spec, $goods_number, $promote_name)
{
//获取所有有效的促销实例
$rules = $this->_getAllRuleInstance();
foreach($this->_rules as $instance)
{
//换礼互斥判断
if(!$instance->goodsExclusion($goods_id, $goods_spec))
{
return false;
}
}
//获取商品单独的促销实例
$rule = $this->_getRuleInstance($promote_name);
//添加商品之前操作
if($rule->beforeGoodsAdd())
{
$rule->goodsAdd($goods_id, $goods_spec, $goods_number);
//添加商品之后操作
return $rule->afterGoodsAdd();
}
return false;
}
复制代码 代码如下:
/**
* 获取可用规则实例列表
* @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;
}
复制代码 代码如下:
/**
* 获取购物车规则类
* @param string $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('购物规则对象加载异常. rule_name:'.$rule_name);
throw new Exception('购物规则对象加载异常.');
}
}
这里主要用到的促销是,判断某一个人是否有添加这个商品的权限,打折等。
二,遍历购物车商品的操作
这一步要执行关键操作是遍历所有促销策略的检查列表函数。
这里常常可以用到的促销是满多少钱,送赠品,买二送一等等。
复制代码 代码如下:
/**
* 获取购物车内商品清单对象列表
* @return array Bll_Shop_Cart_Rule
*/
public function goodsViewList()
{
$list = $this->getGoodsList();
// 在列表时检查购物车内商品列表
$rules = $this->_getAllRuleInstance();
foreach($this->_rules as $instance)
{
$instance->setGoodsList($list)->goodsCheckList();
$this->_tip_rules[] = $instance;
}
//获取最新购物车列表
$goods_list = $this->_cart->getGoodsList();
return $goods_list;
}
第三,提交订单之前的操作
有一些类型的促销,比如某人有打折的权限,当下完订单后,这个打折的权限就被用掉了;或者在下单之前要先检查这个订单的金额,如果小于多少就不准下这个订单等等。
以上这些都会用到提交订单之前的操作。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),