yeknava/simple-shop

laravel 的简单商店包

1.6 2021-01-11 00:27 UTC

This package is auto-updated.

Last update: 2024-09-11 05:00:51 UTC


README

Laravel 简单商店包。

安装

使用包管理器 composer 安装简单商店包。

composer require yeknava/simple-shop

用法

在您的终端运行此命令

php artisan vendor:publish

如果有的话,将 SimpleShopOwner 特性添加到 Shop 所有者模型中,并将 SimpleShopCustomer 特性添加到客户模型中。

<?php

use Yeknava\SimpleShop\SimpleShopCustomer;

class User extends Model {
    use SimpleShopCustomer;
}
<?php

use Yeknava\SimpleShop\SimpleShopOwner;

class OwnerModel extends Model {
    use SimpleShopOwner;
}
$customer = (new UserModel([]));
$customer->save();
$shopOwner = (new OwnerModel([]));
$shopOwner->save();

$shopOwner->newShop('shop title');
$this->assertEquals($shopOwner->shops()->count(), 1);

$shop = $shopOwner->shop();
$product = $shop->addProduct('product 1 title', 10000, null, [
    'description' => 'product 1 desc',
    'quantity' => 100
]);

$customer->addToCart($product, 90, ['color'=>'red']);
$customer->addToCart($product, 1, ['color'=>'blue']);
$this->assertEquals(2, count($customer->cart->items));

$customer->addToCart($product, 10, ['color'=>'red']);
$this->assertEquals(3, count($customer->cart()->first()->items));

// clearing cart won't decrease product quantity
$customer->clearCart();
$this->assertEquals(0, count($customer->cart()->first()->items));

$customer->addToCart($product, 10, ['color'=>'red']);
$this->assertEquals(1, count($customer->cart()->first()->items));

$product = $shop->searchByTitle('product 1 title');
$this->assertEquals('product 1 title', $product->title);
$this->assertEquals(100, $product->quantity);

// purchasing cart will decrease product quantity
$customer->cartPurchased();

$product = $shop->searchByTitle('product 1 title');
$this->assertEquals(90, $product->quantity);

// shop specific cart instance for each customer 
$customer->addToCart($product, 1, ['color'=>'red'], $shop->id);
$this->assertEquals(1, $customer->cart($shop->id)->first()->items[0]['quantity']);

// default cart instance for each customer 
$customer->addToCart($product, 10, ['color'=>'red']);
$this->assertEquals(10, $customer->cart()->first()->items[0]['quantity']);

贡献

欢迎拉取请求。对于重大更改,请先打开一个问题以讨论您想要进行更改的内容。

许可

MIT