darkorsa/shop
Composer 安装命令:
composer require darkorsa/shop
包简介
Flexible shop package including stock validation, discounts, price formats, etc.
README 文档
README
This library is a convenient to use implementation of the shopping cart with the following features:
- prices calculation (gross/net)
- tax calculation
- multiple price formats
- shipping costs
- payment costs
- customized discounts
- stock validation
- increasing/decreasing items quantity
Also you do not have to worry about the correctness of operations on the amounts of money, this is carried out with the help of the Money for PHP library which is the implementation of the Money pattern by Martin Fowler.
This package is PSR-2 and PSR-4 compliant.
Install
Via Composer
$ composer require darkorsa/shop
Usage
A few steps are required to create a cart object.
Products
Products represent the goods sold in the shop. The required parameters are:
- id
- name
- stock (how much of that product is available on stock)
- price
- taxRate
optional params:
- weight
- imagePath
use Plane\Shop\Product; $someProduct = new Product([ 'id' => '1', 'name' => 'Some product', 'stock' => 8, 'price' => 2.8, 'taxRate' => 0.10, // 10% ]);
If you need to include additional product data in your shopping cart, you can extend Product functionality by using the decorator pattern.
Cart items
Cart items represent the content of the shopping cart. Items can be injected into Cart object one by one or with use of collection.
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);
To ensure that the amount of products in the cart is not greater than the amount in stock, a validator can be used.
use Plane\Shop\Exception\QuanityException; use Plane\Shop\Validator\StockQuantityValidator; try { $cartItem = new CartItem($someProduct, 10, new StockQuantityValidator)); } catch (QuanityException $e) { // handle exception }
Validation also takes place when you add an item to the Cart. When the same item is added again (i.e. an item with the same product ID), the item is not added twice, but it's quantity is incremented.
try { $cart->add($cartItem); $cart->add($cartItem); // if sum of items exceeds product stock an exception is thrown } catch (QuanityException $e) { // handle exception }
Shipping
Shipping can be defined so the shipping data will be available within the Cart object.
use Plane\Shop\Shipping; $shipping = new Shipping([ 'id' => 1, 'name' => 'National Shipping Company', 'description' => 'Standart Ground Shipping', 'cost' => 7.50, ]); $cart->setShipping($shipping);
Payment
Payment can be defined in order to calculate payment fee. There are two methods of fee calculation. Fixed price and percentage.
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);
Cart
Cart is an object representing a shopping cart and provides all the necessary methods to manage it or obtain calculated data.
Creation
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
Currency must be in ISO standard.
Usage
$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
Note that all prices are represented as Money object.
Discounts
Discount can be applied to the Cart object. This library comes with 2 predefined discounts, however custom discounts can be applied as well.
- TotalPriceThresholdDiscount - the discount will be applied when the total price exceeds a certain price threshold
- EverySecondItemFreeDiscount - every second cart item is free
Discount example:
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);
Presenation
By default the prices are returned as Money object but one can easly format all the prices within Cart with use of CartPresenter.
Default formatter
Default formatter is 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
Other formatters
One can use other formatters shipped with Money for PHP library or write own custom formmatter.
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
Cart data
To obtain all the cart data like prices, items, products, shipping details, payment, etc. toArray method can be used.
This data can then be passed on to the presentation layers or as an API response.
$cartData = $cartPresenter->toArray();
Security
If you discover any security related issues, please email dkorsak@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
darkorsa/shop 适用场景与选型建议
darkorsa/shop 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 261 次下载、GitHub Stars 达 4, 最近一次更新时间为 2016 年 10 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「shop」 「darkorsa」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 darkorsa/shop 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 darkorsa/shop 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 darkorsa/shop 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Admin Hub for GetCandy. A modern headless e-commerce solution for Laravel PHP framework.
Submodule for SilverShop which allows you to compare products in a table.
Product catalog for your Symfony2 applications.
A shopping system for APPUI
Package set to provide shop or e-commerce functionality (such as CART, ORDERS, TRANSACTIONS and ITEMS) to Laravel for customizable builds.
StrongShop 是一款免费开源的跨境电商商城网站。基于 PHP Laravel6 框架开发,遵循 BSD-3-Clause 开源协议,免费商用。支持多语言,多货币,多种国际配送方式。PayPal 支付,国际信用卡支付。PC Web 端和移动端自适应。
统计信息
- 总下载量: 261
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-10