php 网上商城促销设计实例代码
大体的思想,每一个促销要新建一个促销类,有专门的开关来控制是否生效。
用商品里面的促销识别码来判断具体调用哪一个促销实例。
首先,在添加商品的时候,分两步,第一步是添加状态,第二步是把购物车内的商品显示这个状态。
一,添加步骤几个重要的点:
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;
}
第三,提交订单之前的操作
有一些类型的促销,比如某人有打折的权限,当下完订单后,这个打折的权限就被用掉了;或者在下单之前要先检查这个订单的金额,如果小于多少就不准下这个订单等等。
以上这些都会用到提交订单之前的操作。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
