定制 yiddishe-kop/laravel-commerce 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

yiddishe-kop/laravel-commerce

最新稳定版本:v2.3.0

Composer 安装命令:

composer require yiddishe-kop/laravel-commerce

包简介

Simple commerce package for Laravel

README 文档

README

laravel-commerce

Open in Visual Studio Code

A simple commerce package for Laravel

Latest Version on Packagist Total Downloads

After searching for a simple ecommerce package for Laravel and not finding a lightweight simple to use solution - I decided to attempt to create one myself.

Read the official documentation here: https://laravel-commerce.yiddishe-kop.com/

Features

  • Cart (stored in the session - so guests can also have a cart)
  • Orders
  • Coupons
  • Special Offers
  • Multiple Currencies
  • Multiple Payment Gateways

This package only implements the backend logic, and leaves you with full control over the frontend.

Installation

You can install the package via composer:

composer require yiddishe-kop/laravel-commerce

To publish the commerce.php config file:

php artisan vendor:publish --provider="YiddisheKop\LaravelCommerce\CommerceServiceProvider" --tag="config"

You can also publish the migrations if you need to customize them:

php artisan vendor:publish --provider="YiddisheKop\LaravelCommerce\CommerceServiceProvider" --tag="migrations"

Usage

Cart

You can access the cart anywhere, regardless if the user is logged in or a guest, using the facade:

use YiddisheKop\LaravelCommerce\Facades\Cart;

$cart = Cart::get();

When the guest logs in, the cart will be attached to his account 👌.

Note: If you want the cart to still be available after logout, you need to override the following method in Auth\LoginController:

public function logout(Request $request) {
    $this->guard()->logout();

    // keep cart data for after logout
    $cartId = session()->get('cart');
    $request->session()->invalidate();
    $request->session()->regenerateToken();
    session()->put('cart', $cartId);

    if ($response = $this->loggedOut($request)) {
        return $response;
    }

    return $request->wantsJson()
        ? new JsonResponse([], 204)
        : redirect('/');
}

Products

You can make any model purchasable - by implementing the Purchasable contract:

use YiddisheKop\LaravelCommerce\Contracts\Purchasable;
use YiddisheKop\LaravelCommerce\Traits\Purchasable as PurchasableTrait;

class Product implements Purchasable {
  use PurchasableTrait;

    // the title of the product
    public function getTitle(): string {
        return $this->name;
    }

    // the price
    public function getPrice(): int {
        return $this->price;
    }
}
Add products to cart

Adding a product to the cart couldn't be simpler:

Cart::add(Purchasable $product, int $quantity = 1);

Alternatively:

$product->addToCart($quantity = 1);

If you add a product that already exists in the cart, we'll automatically just update the quantity 😎 .

Remove products from the cart
Cart::remove(Purchasable $product);

Alternatively:

$product->removeFromCart();

To empty the whole cart:

Cart::empty();

Access cart items

You can access the cart items using the items relation:

$cartItems = $cart->items;

To access the Product model from the cartItem, use the model relation (morphable):

$product = $cart->items[0]->model;

Calculate Totals

To calculate and persist the totals of the cart, use the calculateTotals() method:

Cart::calculateTotals();

Now the cart has the following data up to date:

[
  "items_total" => 3552
  "tax_total" => 710.0
  "coupon_total" => "0"
  "grand_total" => 4262.0
]

Deleted products will automatically get removed from the cart upon calculating the totals.

Orders

You can use the HasOrders trait on the User model, to get a orders relationship:

use YiddisheKop\LaravelCommerce\Traits\HasOrders;

class User {
  use HasOrders;
  // ...
}

// you can now get all the users' orders (status complete)
$orders = $user->orders;

Testing

This package has extensive tests - with the delightful Pest framework. To run the tests:

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email yehuda@yiddishe-kop.com instead of using the issue tracker.

Credits

License

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

统计信息

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

GitHub 信息

  • Stars: 26
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固