承接 ingenius/shopcart 相关项目开发

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

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

ingenius/shopcart

最新稳定版本:1.5.2

Composer 安装命令:

composer require ingenius/shopcart

包简介

Ingenius ShopCart Package

README 文档

README

A Laravel package for implementing shopping cart functionality with modifiers support.

Installation

composer require ingenius/shopcart

Features

  • Add products to cart (supports polymorphic relationships)
  • Remove products from cart
  • Delete cart items
  • Get cart items
  • Cart modifiers system for extending functionality
  • Session-based cart for guests
  • User-based cart for authenticated users

Configuration

Environment Variables

PRODUCT_MODEL=Ingenius\Products\Models\Product

Note: For backward compatibility, SHOPCART_PRODUCT_MODEL is still supported but PRODUCT_MODEL is preferred as it's used across all packages.

ShopCart Modifier System

The ShopCart package includes a modifier system that allows other packages to extend the cart's functionality without the ShopCart package needing to know about them.

Features

  • Add custom calculations to the cart total (shipping, discounts, taxes, etc.)
  • Extend the cart's JSON/array output with additional data
  • Control the execution order of modifiers using priorities
  • Easy to implement in other packages

How It Works

The ShopCart uses a pipeline approach with modifiers that:

  1. Calculate subtotals in priority order (lower numbers run first)
  2. Extend the toArray() output with custom values from each modifier
  3. Allow precise control over execution order

Creating Your Own Modifier

To create a new cart modifier in your package:

  1. Create a class that extends BaseCartModifier or implements CartModifierInterface
  2. Set its priority to control when it runs (lower = earlier)
  3. Register it with the CartModifierManager in your package's service provider

Example Modifier

<?php

namespace YourPackage\CartModifiers;

use Ingenius\ShopCart\Modifiers\BaseCartModifier;
use Ingenius\ShopCart\Services\ShopCart;

class YourModifier extends BaseCartModifier
{
    /**
     * Set priority - lower numbers run first
     */
    public function getPriority(): int
    {
        return 50; // Run after the base item subtotal calculations
    }
    
    /**
     * Modify the subtotal
     */
    public function calculateSubtotal(ShopCart $cart, float $currentSubtotal): float
    {
        // Your custom calculation logic here
        $yourModification = 10.00;
        
        // Store data for use in extendCartArray
        $this->yourData = $yourModification;
        
        // Return modified subtotal
        return $currentSubtotal + $yourModification;
    }
    
    /**
     * Add data to the cart array
     */
    public function extendCartArray(ShopCart $cart, array $cartArray): array
    {
        $cartArray['your_data'] = [
            'amount' => $this->yourData,
            'other_info' => 'Custom information'
        ];
        
        return $cartArray;
    }
    
    protected $yourData;
}

Registering Your Modifier

In your package's service provider:

<?php

namespace YourPackage\Providers;

use Illuminate\Support\ServiceProvider;
use Ingenius\ShopCart\Services\CartModifierManager;
use YourPackage\CartModifiers\YourModifier;

class YourPackageServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        // Register your cart modifier
        $this->app->afterResolving(CartModifierManager::class, function (CartModifierManager $manager) {
            $manager->register(new YourModifier());
        });
    }
}

Command Line Tools

The ShopCart package includes command line tools to help you manage and debug cart modifiers.

Listing Registered Cart Modifiers

To view all registered cart modifiers and their priorities, use the following command:

php artisan shopcart:modifiers

Usage

// Usage examples will go here

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-24