darkorsa / shop
灵活的购物包,包括库存验证、折扣、价格格式等。
Requires
- php: >=7.4.0
- moneyphp/money: ^3.2 | ^4.0
Requires (Dev)
- phpstan/phpstan: ^0.12.1
- phpunit/php-code-coverage: 7.0.*
- phpunit/phpunit: ^8
- squizlabs/php_codesniffer: ^2.8
- symfony/var-dumper: ^4.2
This package is auto-updated.
Last update: 2024-09-28 18:10:49 UTC
README
此库是一个易于使用的购物车实现,具有以下特性:
- 价格计算(含税/不含税)
- 税计算
- 多种价格格式
- 运费
- 支付费用
- 自定义折扣
- 库存验证
- 增加/减少项目数量
您也不必担心金钱操作的准确性,这是通过Money for PHP库实现的,该库是Martin Fowler的Money模式的实现。
此包符合PSR-2和PSR-4规范。
安装
通过Composer
$ composer require darkorsa/shop
使用
创建购物车对象需要几个步骤。
产品
产品表示商店中销售的货物。所需的参数包括:
- id
- 名称
- 库存(该产品的库存数量)
- 价格
- 税率
可选参数
- 重量
- 图片路径
use Plane\Shop\Product; $someProduct = new Product([ 'id' => '1', 'name' => 'Some product', 'stock' => 8, 'price' => 2.8, 'taxRate' => 0.10, // 10% ]);
如果您需要在购物车中包含额外的产品数据,您可以通过使用装饰器模式来扩展Product功能。
购物车项
购物车项表示购物车的内容。项可以逐个注入到购物车对象中,或者使用集合。
use Plane\Shop\CartItem; use Plane\Shop\CartItemCollection; // one by one $cart->add(new CartItem($someProduct)); // collection $cartItemCollection = new CartItemCollection(); $cartItemCollection->addItem(new CartItem($someProduct)); // cart item with 1 piece of a product $cartItemCollection->addItem(new CartItem($someOtherProduct, 4)); // cart item with 4 pieces of a product $cart->fill($cartItemCollection);
为了确保购物车中的产品数量不超过库存数量,可以使用验证器。
use Plane\Shop\Exception\QuanityException; use Plane\Shop\Validator\StockQuantityValidator; try { $cartItem = new CartItem($someProduct, 10, new StockQuantityValidator)); } catch (QuanityException $e) { // handle exception }
当您向购物车添加项时也会进行验证。当再次添加相同的项(即具有相同产品ID的项)时,该项不会被添加两次,而是增加其数量。
try { $cart->add($cartItem); $cart->add($cartItem); // if sum of items exceeds product stock an exception is thrown } catch (QuanityException $e) { // handle exception }
运输
可以定义运输,以便在购物车对象中可用运输数据。
use Plane\Shop\Shipping; $shipping = new Shipping([ 'id' => 1, 'name' => 'National Shipping Company', 'description' => 'Standart Ground Shipping', 'cost' => 7.50, ]); $cart->setShipping($shipping);
支付
可以定义支付,以便计算支付费用。有两种费用计算方法。固定价格和百分比。
use Plane\Shop\Payment; // fixed price $payment = Payment::createWithFixedFee([ 'id' => 1, 'name' => 'PayPal', 'description' => 'Payment with Paypal', 'fee' => 8.45 ]); // percentage of the total gross price after discounts $payment = Payment::createWithPercentageFee([ 'id' => 1, 'name' => 'PayPal', 'description' => 'Payment with Paypal', 'fee' => 2 // 2% ]); $cart->setPayment($payment);
购物车
购物车是一个表示购物车的对象,并提供管理它或获取计算数据所需的所有必要方法。
创建
use Plane\Shop\Cart; $cart = new Cart('USD'); $cart->fill($cartItemCollection); // fill cart with many items at once $cart->add($cartItem); // add single cart item
货币必须以ISO标准表示。
使用
$cart->itemsQuantity(); // total quantity of cart items $cart->totalNet(); // total net price $cart->totalGross(); // total gross price $cart->tax() // sum of taxes for all items; $cart->weight(); // total items weight $cart->shippingCost() // shipping cost; $cart->paymentFee(); // payment fee (percentage or fixed) $cart->totalAfterDiscounts(); // total gross price after applying all discounts
注意,所有价格都表示为Money对象。
折扣
可以将折扣应用于购物车对象。此库包含2个预定义的折扣,但也可以应用自定义折扣。
- 总价阈值折扣 - 当总价超过一定价格阈值时应用折扣
- 每第二个项目免费折扣 - 每第二个购物车项目免费
折扣示例
use Plane\Shop\Discount\TotalPriceThresholdDiscount; $priceTresholdDiscount = new TotalPriceThresholdDiscount('Discount description', $cart, [ 'treshold' => 100, 'discount' => 0.1 // ten percent discount for total gross price above 100 ]); $cart->addDiscount($priceTresholdDiscount);
展示
默认情况下,价格以Money对象的形式返回,但可以使用购物车展示器轻松格式化购物车中的所有价格。
默认格式化器
默认格式化器是Decimal Formatter。
use Plane\Shop\CartPresenter; $cartPresenter = new CartPresenter($cart); echo $cartPresenter->totalNet(); // 10.00 echo $cartPresenter->totalGross(); // 10.22 echo $cartPresenter->tax(); // 0.22
其他格式化器
可以使用随Money for PHP库提供的其他格式化器,或者编写自己的自定义格式化器。
use Money\Currencies\ISOCurrencies; use Money\Formatter\IntlMoneyFormatter; $numberFormatter = new \NumberFormatter('us_US', \NumberFormatter::CURRENCY); $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies()); $cartPresenter = new CartPresenter($cart, $moneyFormatter); echo $cartPresenter->totalNet(); // $10.00 echo $cartPresenter->totalGross(); // $10.22 echo $cartPresenter->tax(); // $0.22
购物车数据
要获取所有购物车数据,如价格、项、产品、运输细节、支付等,可以使用toArray方法。
然后将这些数据传递到展示层或作为API响应。
$cartData = $cartPresenter->toArray();
安全性
如果您发现任何与安全相关的问题,请通过电子邮件 dkorsak@gmail.com 告知,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。