README.md 3.7 KB

Longyi Core - Flexible Variant Product Type

Flexible variant product type for Bagisto with option-based variant generation.

Features

  • Flexible Variant Generation - Manually select which variants to create
  • Global Option Templates - Reusable options across multiple products
  • Many-to-Many Relations - Products can share options
  • Simple Inventory - Direct quantity field in variants table
  • Soft Deletes - Safe deletion with recovery option
  • Price Management - Price, compare price, special price support
  • Meta Data - JSON fields for extensible data
  • Optimized Indexes - 23 indexes for maximum performance

Installation

1. Register the package in main composer.json

{
    "repositories": [
        {
            "type": "path",
            "url": "packages/Longyi/Core"
        }
    ],
    "require": {
        "longyi/core": "*"
    },
    "autoload": {
        "psr-4": {
            "Longyi\\Core\\": "packages/Longyi/Core/src/"
        }
    }
}

2. Update autoloader

composer dump-autoload

3. Run migrations

php artisan migrate

4. Publish assets (optional)

# Publish config
php artisan vendor:publish --tag=longyi-config

# Publish views
php artisan vendor:publish --tag=longyi-views

# Publish translations
php artisan vendor:publish --tag=longyi-lang

Database Schema

Tables (5)

  1. product_options - Global option templates (Color, Size, etc.)
  2. product_product_options - Product-Option pivot table
  3. product_option_values - Option values (Red, Blue, S, M, L)
  4. product_variants - Product variants with inventory
  5. product_variant_option_values - Variant-OptionValue pivot table

Indexes (23)

Optimized indexes for all critical queries:

  • Unique indexes for data integrity
  • Composite indexes for multi-column queries
  • Single indexes for common filters

Usage

Create Option Templates

use Longyi\Core\Models\ProductOption;

// Create a color option
$colorOption = ProductOption::create([
    'type' => 'color',
    'code' => 'color',
    'label' => '颜色',
]);

// Add color values
$colorOption->values()->create([
    'code' => 'red',
    'label' => '红色',
    'position' => 0,
    'meta' => ['color_code' => '#FF0000'],
]);

Attach Options to Product

$product->options()->attach($colorOption->id, [
    'position' => 0,
    'is_required' => true,
    'meta' => ['show_swatch' => true],
]);

Create Variants

use Longyi\Core\Models\ProductVariant;

$variant = ProductVariant::create([
    'product_id' => $product->id,
    'sku' => 'TSHIRT-RED-M',
    'name' => 'T-Shirt Red Medium',
    'price' => 99.99,
    'compare_price' => 149.99,
    'quantity' => 100,
    'status' => true,
]);

// Link to option values
$variant->optionValues()->attach([$redValue->id, $mediumValue->id]);

Query Saleable Variants

$saleableVariants = ProductVariant::where('product_id', 1)
    ->where('status', true)
    ->where('quantity', '>', 0)
    ->with('optionValues')
    ->orderBy('sort_order')
    ->get();

Architecture

No Foreign Key Constraints

  • All relationships managed via Eloquent models
  • Cascade deletes handled in model boot methods
  • More flexible for testing and migrations

No Translation Tables

  • Labels stored directly in main tables
  • Simpler schema and faster queries
  • Multi-language can be added via JSON fields if needed

Model Events

All cascade operations handled via model events:

  • Deleting option → deletes values
  • Deleting product → deletes options and variants
  • Deleting variant → detaches option values

License

MIT License

Support

For issues and questions, please open an issue on GitHub.