承接 darkorsa/shop 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

darkorsa/shop

Composer 安装命令:

composer require darkorsa/shop

包简介

Flexible shop package including stock validation, discounts, price formats, etc.

关键字:

README 文档

README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 darkorsa/shop 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 261
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-10-10