承接 revotale/shopping-cart 相关项目开发

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

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

revotale/shopping-cart

Composer 安装命令:

composer require revotale/shopping-cart

包简介

PHP library providing basic shopping cart object and interfaces to implement any type of cart items + promotions combination.

README 文档

README

A powerful and flexible PHP library for implementing shopping cart functionality with advanced promotion system and precise decimal calculations.

Notice! This README was "vibe coded". If you encouter any errors please feel free to open the issue.

PHP Version License: MIT

Features

  • 🛒 Flexible Cart Management: Add, remove, and manage cart items with quantity control
  • 🏷️ Advanced Promotion System: Support for multiple promotion types (percentage discounts, fixed amount discounts, free products, etc.)
  • 💰 Precise Decimal Calculations: Built-in arbitrary precision decimal arithmetic using BCMath
  • 🔧 Extensible Architecture: Easy to extend with custom items and promotions
  • 📊 Comprehensive Totals: Detailed cart totals with promotion impacts and item subtotals
  • 🎯 Promotion Stacking: Support for multiple promotions that can interact with each other
  • 🧮 Mathematical Functions: Full-featured decimal class with trigonometric, logarithmic, and power functions

Installation

composer require revotale/shopping-cart

Requirements

  • PHP ^8.2
  • BCMath extension

Quick Start

Basic Cart Operations

<?php

use RevoTale\ShoppingCart\Cart;
use RevoTale\ShoppingCart\CartItemInterface;

// Create a cart item (implement CartItemInterface)
class Product implements CartItemInterface
{
    public function __construct(
        private string $id,
        private string $name,
        private int $priceInCents
    ) {}

    public function getCartId(): string
    {
        return $this->id;
    }

    public function getCartType(): string
    {
        return 'product';
    }

    public function getUnitPrice(): int
    {
        return $this->priceInCents; // Price in smallest currency unit (cents)
    }
}

// Create cart and add items
$cart = new Cart();
$product = new Product('SKU123', 'T-Shirt', 2999); // $29.99

$cart->addItem($product, 2); // Add 2 t-shirts

// Get cart totals
$totals = $cart->performTotals();
echo $totals->getTotal()->asFloat(); // 59.98

Adding Promotions

use RevoTale\ShoppingCart\PromotionTemplates\CartPercentageDiscount;
use RevoTale\ShoppingCart\CartInterface;

// Create a 10% discount promotion
class TenPercentDiscount extends CartPercentageDiscount
{
    public function getCartId(): string
    {
        return 'ten_percent_off';
    }

    public function getCartType(): string
    {
        return 'discount';
    }

    public function isEligible(CartInterface $cart): bool
    {
        // Apply if cart total is over $50
        return $cart->getItems() !== [] && 
               $cart->performTotals()->getTotal()->isGreaterThan(
                   \RevoTale\ShoppingCart\Decimal::fromInteger(5000)
               );
    }

    public function getDiscountMultiplier(): float
    {
        return 0.9; // 90% of original price = 10% discount
    }
}

// Add promotion to cart
$cart->addPromotion(new TenPercentDiscount());

$totals = $cart->performTotals();
echo $totals->getTotal()->asFloat(); // 53.98 (10% off $59.98)

Core Components

Cart

The main cart class that manages items and promotions.

$cart = new Cart();

// Item management
$cart->addItem($item, $quantity);
$cart->removeItem($item, $quantity);
$cart->hasItem($item);
$cart->getItemQuantity($item);

// Promotion management
$cart->addPromotion($promotion);
$cart->removePromotion($promotion);
$cart->hasPromo($promotion);

// Clear operations
$cart->clearItems();
$cart->clearPromotions();
$cart->clear(); // Clear both items and promotions

// Calculate totals
$totals = $cart->performTotals();

Cart Items

Items must implement CartItemInterface:

interface CartItemInterface
{
    public function getCartId(): string;      // Unique identifier
    public function getCartType(): string;    // Type category
    public function getUnitPrice(): int;      // Price in smallest currency unit
}

Promotions

Promotions implement PromotionInterface and can:

  • Reduce item prices: Modify individual item subtotals
  • Add/remove items: Add free products or remove items
  • Control other promotions: Enable/disable other promotions
  • Apply cart-wide effects: Fixed amount discounts, shipping rules

Built-in Promotion Templates

  1. CartPercentageDiscount: Apply percentage discounts
class MyPercentageDiscount extends CartPercentageDiscount
{
    public function getDiscountMultiplier(): float
    {
        return 0.85; // 15% discount
    }
    
    public function isEligible(CartInterface $cart): bool
    {
        return true; // Always eligible
    }
    
    // ... implement required methods
}
  1. CartFixedSumDiscount: Apply fixed amount discounts
class MyFixedDiscount extends CartFixedSumDiscount
{
    public function getDiscountAmount(): float
    {
        return 10.00; // $10 off
    }
    
    public function isEligible(CartInterface $cart): bool
    {
        return true;
    }
    
    // ... implement required methods
}

Custom Promotions

For complex promotion logic, implement PromotionInterface directly:

class BuyOneGetOneFree implements PromotionInterface
{
    public function isEligible(CartInterface $cart): bool
    {
        return $cart->getItemQuantity($this->targetItem) >= 2;
    }

    public function reduceItems(ModifiedCartData $cart, array $itemCounters): array
    {
        // Add free items based on cart contents
        foreach ($itemCounters as $counter) {
            if ($counter->item === $this->targetItem) {
                $freeQuantity = intval($counter->quantity / 2);
                if ($freeQuantity > 0) {
                    $itemCounters[] = new CartItemCounter($this->freeItem, $freeQuantity);
                }
            }
        }
        return $itemCounters;
    }
    
    // ... implement other required methods
}

Decimal Arithmetic

The library includes a comprehensive Decimal class for precise calculations:

use RevoTale\ShoppingCart\Decimal;

// Create decimals
$price = Decimal::fromFloat(29.99);
$quantity = Decimal::fromInteger(3);
$discount = Decimal::fromString("0.1");

// Arithmetic operations
$subtotal = $price->mul($quantity);           // 89.97
$discountAmount = $subtotal->mul($discount);  // 8.997
$total = $subtotal->sub($discountAmount);     // 80.973

// Rounding and formatting
$finalTotal = $total->round(2);               // 80.97
echo $finalTotal->asFloat();                  // 80.97

// Comparisons
if ($total->isGreaterThan(Decimal::fromInteger(80))) {
    echo "Total exceeds $80";
}

// Mathematical functions
$sqrt = Decimal::fromInteger(16)->sqrt();     // 4.0
$log = Decimal::fromInteger(100)->log10();    // 2.0
$power = Decimal::fromInteger(2)->pow(Decimal::fromInteger(8)); // 256.0

Advanced Usage

Promotion Context

Use PromoCalculationsContext to share data between promotions during calculation:

public function reduceItemSubtotal(
    ModifiedCartData $cart, 
    CartItemInterface $item, 
    Decimal $subTotal, 
    PromoCalculationsContext $context
): Decimal {
    // Store data for later use
    $context->setValue($this, 'discount_applied', true);
    
    // Retrieve data from other promotions
    $previousDiscount = $context->getValue($otherPromotion, 'discount_amount');
    
    return $subTotal->mul(Decimal::fromFloat(0.9));
}

Cart Totals Analysis

The CartTotals object provides detailed information:

$totals = $cart->performTotals();

// Basic totals
$grandTotal = $totals->getTotal();
$items = $totals->getItems();
$promotions = $totals->getPromotions();

// Detailed item information
foreach ($totals->getItemSubTotals() as $itemSubTotal) {
    echo sprintf(
        "Item: %s, Qty: %d, Before: %s, After: %s\n",
        $itemSubTotal->item->getCartId(),
        $itemSubTotal->quantity,
        $itemSubTotal->subTotalBeforePromo->asFloat(),
        $itemSubTotal->subTotalAfterPromo->asFloat()
    );
}

// Promotion impacts
foreach ($totals->getPromotionItemsImpact() as $impact) {
    echo sprintf(
        "Promotion %s affected %s by %s\n",
        $impact->promotion->getCartId(),
        $impact->item->getCartId(),
        $impact->priceImpact->asFloat()
    );
}

// Check for changes
if ($totals->isPromotionDiff()) {
    echo "Promotions were added or removed during calculation\n";
}

if ($totals->isItemsDiff()) {
    echo "Items were added or removed during calculation\n";
}

Promotion Execution Order

Promotions are executed in the order they were added to the cart. Later promotions can modify the effects of earlier ones. Use reducePromotions() to control which other promotions are active:

public function reducePromotions(ModifiedCartData $cart, array $promotions): array
{
    // Remove conflicting promotions
    return array_filter($promotions, function($promo) {
        return !($promo instanceof ConflictingPromotionType);
    });
}

Error Handling

The library throws specific exceptions for various error conditions:

try {
    // Division by zero
    $result = $decimal->div(Decimal::fromInteger(0));
} catch (DomainException $e) {
    echo "Mathematical error: " . $e->getMessage();
}

try {
    // Invalid number format
    $decimal = Decimal::fromString("not-a-number");
} catch (UnexpectedValueException $e) {
    echo "Invalid input: " . $e->getMessage();
}

Performance Considerations

  • The library uses BCMath for precise decimal calculations, which is slower than float arithmetic but provides exact results
  • Promotion calculations are performed each time performTotals() is called
  • For high-performance scenarios, consider caching totals when cart contents haven't changed
  • The Decimal class is immutable, so operations create new instances

Testing

# Run tests
composer run phpunit

# Run static analysis
composer run phpstan

# Run code style fixes
composer run rector:fix

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

License

This library is licensed under the MIT License. See the LICENSE file for details.

Support

For questions, issues, or feature requests, please use the GitHub issue tracker.

revotale/shopping-cart 适用场景与选型建议

revotale/shopping-cart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.23k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 12 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 revotale/shopping-cart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 revotale/shopping-cart 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.23k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-04