PHP code example of zgldh / discount-and-coupon
1. Go to this page and download the library: Download zgldh/discount-and-coupon library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zgldh / discount-and-coupon example snippets
use zgldh\DiscountAndCoupon\Calculator;
$calculator = new Calculator(); // 初始化计算器
$result = $calculator
->setBenefits($discountCollection) // 设置打算应用的 Benefit 可选 Optional
->setProducts([ // 设置要买的货物 必填
[
'sku' => 123, // 货物 SKU 或 ID 必填
'price' => 2.80 // 货物原价 必填
'category' => 456, // 货物分类 ID 可选。如不填写,无法参与针对分类的活动。
'name' => 'Coke Cola' // 货物名 可选
'foo' => 'bar' // 其他参数 可选
],
... // more products
])
->calculate(); // 开始计算
$result->getPrice(); // 原始总价, 两位小数
$result->getFinalPrice(); // 最终总价, 两位小数
$result->getProfit(); // final_price 减去 price
$result->getBenefits(); // 实际应用的 benefits, 内含每个 benefit 提供了多少 profit。
foreach($result->getProducts() as $product) // 最终的货物集合,每个元素对应一个货物
{
$product->getSku(); // 该货物 SKU
$product->getPrice(); // 货物原价
$product->getFinalPrice(); // 货物均摊最终价 (仅供参考)
$product->getCategory(); // 货物分类 ID
$product->getName(); // 货物名字
$product->foo; // 其他参数
$product->getBenefits(); // 该货物应用的 benefits
}
use zgldh\DiscountAndCoupon\Benefit;
class FlatDiscountWhenPurchaseExceed extends Benefit{
private $above = null; // 满多少钱
private $deduction = null; // 减多少钱
protected $priority = 100; // 默认优先级
protected function isScopeQualified($scopeProducts, $scopeTotalPrice)
{
return $scopeTotalPrice >= $this->above;
}
protected function newScopePrice($scopeProducts, $scopeTotalPrice)
{
return $scopeTotalPrice - $this->deduction;
}
}
// 满20减2
$d2a20 = new FlatDiscountWhenPurchaseExceed(['above'=>20,'deduction'=>2,'priority'=>101]);
// 满50减10
$d10a50 = new FlatDiscountWhenPurchaseExceed(['above'=>50,'deduction'=>10,'priority'=>102]);
// 满100减30
$d30a100 = new FlatDiscountWhenPurchaseExceed(['above'=>100,'deduction'=>30,'priority'=>103]);
use zgldh\DiscountAndCoupon\Benefit;
const CATEGORY_BREAKFAST = 'breakfast';
class MidAutumnDayBreakfast80Discount extends Benefit{
protected $priority = 200; // 优先级
protected $exclusive = true; // 不与其他活动同享
protected function scope(Product $product){
return $product->category === CATEGORY_BREAKFAST;
}
protected function newScopePrice($scopeProducts, $scopeTotalPrice)
{
return $scopeTotalPrice * 0.8;
}
}
$midAutumnDayDiscountEvent = new MidAutumnDayBreakfast80Discount();
use zgldh\DiscountAndCoupon\Benefit;
const SKU_YOGURT = 'yogurt';
class YogurtBuyOneGetOne extends Benefit{
protected $priority = 500; // 优先级
protected $exclusive = true; // 不与其他活动同享
protected function scope(Product $product){
return $product->sku === SKU_YOGURT;
}
protected function newScopePrice($scopeProducts, $scopeTotalPrice)
{
$productsCount = count($scopeProducts);
$fairCount = ceil($productsCount / 2);
$fairProductPrice = $scopeTotalPrice / $productsCount;
return $fairProductPrice * $fairCount;
}
}
$yogurtPromotion = new YogurtBuyOneGetOne();
use zgldh\DiscountAndCoupon\Benefit;
use zgldh\DiscountAndCoupon\Product;
const SKU_YOGURT = 'yogurt';
class YogurtBuyOneGetOne extends Benefit{
protected $priority = 500; // 优先级
protected $exclusive = true; // 不与其他活动同享
protected function scope(Product $product){
return $product->sku === SKU_YOGURT;
}
protected function newScopeProducts($scopeProducts, $scopeTotalPrice)
{
$count = count($scopeProducts);
for($i = 0; $i<$count; $i++)
{
array_push($scopeProducts, new Product([
'sku'=> SKU_YOGURT,
'price'=>0
]));
}
return $scopeProducts;
}
}
$yogurtPromotion = new YogurtBuyOneGetOne();
use zgldh\DiscountAndCoupon\Benefit;
class FlatDeduction extends Benefit{
protected $priority = 0; // 优先级
protected $deduction = 0;
protected $couponId = null;
protected $groupMaxApplyTime = 2; // 最多可以同时用两张代金券
protected function newScopePrice($scopeProducts, $scopeTotalPrice)
{
return max(0, $scopeTotalPrice - $this->deduction);
}
}
$flatDeduction10= new FlatDeduction([ 'deduction'=>10, 'coupon_id'=>123 ]); // 10元代金券
$flatDeduction20= new FlatDeduction([ 'deduction'=>20, 'coupon_id'=>124 ]); // 20元代金券
$flatDeduction50= new FlatDeduction([ 'deduction'=>50, 'coupon_id'=>125 ]); // 50元代金券
use zgldh\DiscountAndCoupon\Benefit;
const SMALL_COKE = 'small-coke';
const BIG_COKE = 'big-coke';
class UpgradeBeverage extends Benefit{
protected $priority = 500; // 优先级
protected function scope(Product $product){
return $product->sku === SMALL_COKE;
}
protected function newScopeProducts($scopeProducts, $scopeTotalPrice)
{
$firstCoke = $scopeProducts[0];
if($firstCoke)
{
$firstCoke['sku'] = BIG_COKE;
$firstCoke['name'] = 'Big Coke';
}
return $scopeProducts;
}
}