ingenius/shopcart 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

ingenius/shopcart

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.

ingenius/shopcart 适用场景与选型建议

ingenius/shopcart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 82 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 ingenius/shopcart 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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