Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product bundles #1814

Draft
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/core/database/factories/BundleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Lunar\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Lunar\Models\Product;
use Lunar\Models\Bundle;
use Lunar\Models\TaxClass;
use Lunar\Models\TaxRateAmount;

class BundleFactory extends Factory
{
protected $model = Bundle::class;

public function definition(): array
{
return [
'tax_class_id' => TaxClass::factory()->hasTaxRateAmounts(
TaxRateAmount::factory()
),
'sku' => Str::random(12),
'unit_quantity' => 1,
'gtin' => $this->faker->unique()->isbn13,
'mpn' => $this->faker->unique()->isbn13,
'ean' => $this->faker->unique()->ean13,
'shippable' => true,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Lunar\Base\Migration;

return new class extends Migration
{
public function up(): void
{
Schema::create($this->prefix.'bundles', function (Blueprint $table) {
$table->id();
$table->foreignId('tax_class_id')->constrained($this->prefix.'tax_classes');
$table->string('tax_ref')->index()->nullable();
$table->json('attribute_data');
$table->integer('unit_quantity')->unsigned()->index()->default(1);
$table->string('sku')->nullable()->index();
$table->string('gtin')->nullable()->index();
$table->string('mpn')->nullable()->index();
$table->string('ean')->nullable()->index();
$table->boolean('shippable')->default(true)->index();
$table->integer('stock')->default(0)->index();
$table->integer('backorder')->default(0)->index();
$table->string('purchasable')->default('always')->index();
$table->timestamps();
$table->softDeletes();
});
}

public function down(): void
{
Schema::dropIfExists($this->prefix.'bundles');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Lunar\Base\Migration;

return new class extends Migration
{
public function up(): void
{
Schema::create($this->prefix.'bundleables', function (Blueprint $table) {
$table->id();
$table->foreignId('bundle_id')->constrained($this->prefix.'bundles');
$table->morphs('bundleable');
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists($this->prefix.'bundleables');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Lunar\Base\Migration;

return new class extends Migration
{
public function up(): void
{
Schema::create($this->prefix.'collection_bundle', function (Blueprint $table) {
$table->id();
$table->foreignId('collection_id')->constrained($this->prefix.'collections');
$table->foreignId('bundle_id')->constrained($this->prefix.'bundles');
$table->integer('position')->default(1)->index();
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists($this->prefix.'collection_bundle');
}
};
33 changes: 33 additions & 0 deletions packages/core/src/Base/Traits/HasBundles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Lunar\Base\Traits;

use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Collection;
use Lunar\Models\Bundle;

trait HasBundles
{
/**
* Get all the models bundles.
*/
public function bundles(): MorphToMany
{
$prefix = config('lunar.database.table_prefix');

return $this->morphToMany(
Bundle::class,
'bundleable',
"{$prefix}bundleables",
)->withTimestamps();
}


/**
* Add a bundle to the model.
*/
public function addToBundle($bundle)
{
$this->bundles()->attach($bundle);
}
}
7 changes: 7 additions & 0 deletions packages/core/src/DiscountTypes/AmountOff.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Lunar\Base\ValueObjects\Cart\DiscountBreakdown;
use Lunar\Base\ValueObjects\Cart\DiscountBreakdownLine;
use Lunar\DataTypes\Price;
use Lunar\Models\Bundle;
use Lunar\Models\Cart;
use Lunar\Models\Collection;

Expand Down Expand Up @@ -180,6 +181,12 @@ protected function getEligibleLines(Cart $cart): \Illuminate\Support\Collection

if ($collectionIds->count()) {
$lines = $lines->filter(function ($line) use ($collectionIds) {
if ($line->purchasable instanceof Bundle) {
return $line->purchasable->whereHas('collections', function ($query) use ($collectionIds) {
$query->whereIn((new Collection)->getTable().'.id', $collectionIds);
})->exists();
}

return $line->purchasable->product()->whereHas('collections', function ($query) use ($collectionIds) {
$query->whereIn((new Collection)->getTable().'.id', $collectionIds);
})->exists();
Expand Down
184 changes: 184 additions & 0 deletions packages/core/src/Models/Bundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

namespace Lunar\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Collection;
use Lunar\Base\BaseModel;
use Lunar\Base\Purchasable;
use Lunar\Base\Traits\HasAttributes;
use Lunar\Base\Traits\HasMacros;
use Lunar\Base\Traits\HasPrices;
use Lunar\Base\Traits\HasTranslations;
use Lunar\Base\Traits\LogsActivity;
use Lunar\Database\Factories\BundleFactory;
use Spatie\LaravelBlink\BlinkFacade as Blink;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

/**
* @property int $id
* @property int $tax_class_id
* @property array $attribute_data
* @property int $unit_quantity
* @property int $min_quantity
* @property int $quantity_increment
* @property ?string $sku
* @property ?string $gtin
* @property ?string $mpn
* @property ?string $ean
* @property bool $shippable
* @property int $stock
* @property int $backorder
* @property string $purchasable
* @property ?\Illuminate\Support\Carbon $created_at
* @property ?\Illuminate\Support\Carbon $updated_at
* @property ?\Illuminate\Support\Carbon $deleted_at
*/
class Bundle extends BaseModel implements Purchasable
{
use HasAttributes;
use HasFactory;
use HasMacros;
use HasPrices;
use HasTranslations;
use LogsActivity;

/**
* Define the guarded attributes.
*
* @var array
*/
protected $guarded = [];

/**
* {@inheritDoc}
*/
protected $casts = [
'requires_shipping' => 'bool',
];

/**
* Return a new factory instance for the model.
*/
protected static function newFactory(): BundleFactory
{
return BundleFactory::new();
}

public function bundleable(): MorphTo
{
return $this->morphTo();
}

/**
* Return the product collections relation.
*/
public function collections(): BelongsToMany
{
return $this->belongsToMany(
\Lunar\Models\Collection::class,
config('lunar.database.table_prefix').'collection_bundle'
)->withPivot(['position'])->withTimestamps();
}


public function items(): MorphToMany
{
$prefix = config('lunar.database.table_prefix');

return $this->morphedByMany(
ProductVariant::class,
'bundleable',
"{$prefix}bundleables"
);
}

/**
* Return the tax class relationship.
*/
public function taxClass(): BelongsTo
{
return $this->belongsTo(TaxClass::class);
}

public function getPrices(): Collection
{
return $this->prices;
}

/**
* Return the unit quantity for the variant.
*/
public function getUnitQuantity(): int
{
return $this->unit_quantity;
}

/**
* Return the tax class.
*/
public function getTaxClass(): TaxClass
{
return Blink::once("tax_class_{$this->tax_class_id}", function () {
return $this->taxClass;
});
}

public function getTaxReference(): ?string
{
return $this->tax_ref;
}

/**
* {@inheritDoc}
*/
public function getType(): string
{
return $this->shippable ? 'physical' : 'digital';
}

/**
* {@inheritDoc}
*/
public function isShippable(): bool
{
return $this->shippable;
}

/**
* {@inheritDoc}
*/
public function getDescription(): string
{
return $this->product->translateAttribute('name');
}

/**
* {@inheritDoc}
*/
public function getOption(): void
{
return;
}

/**
* {@inheritDoc}
*/
public function getIdentifier(): string
{
return $this->items->map(function ($item) {
return $item->getIdentifier();
})->implode(',');
}

public function getThumbnail(): ?Media
{
return $this->images?->first(function ($media) {
return (bool) $media->pivot?->primary;
});
}
}
2 changes: 2 additions & 0 deletions packages/core/src/Models/ProductVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Lunar\Base\Casts\AsAttributeData;
use Lunar\Base\Purchasable;
use Lunar\Base\Traits\HasAttributes;
use Lunar\Base\Traits\HasBundles;
use Lunar\Base\Traits\HasDimensions;
use Lunar\Base\Traits\HasMacros;
use Lunar\Base\Traits\HasPrices;
Expand Down Expand Up @@ -53,6 +54,7 @@
class ProductVariant extends BaseModel implements Purchasable
{
use HasAttributes;
use HasBundles;
use HasDimensions;
use HasFactory;
use HasMacros;
Expand Down
Loading