rariteth/laravel-cart

该软件包已被废弃,不再维护。未建议替代软件包。

Laravel 购物车

v0.13 2023-05-22 03:52 UTC

README

Build Status Latest Stable Version Total Downloads License

安装

通过 Composer 安装此软件包。

在终端运行 Composer require 命令

composer require rariteth/laravel-cart

如果您使用的是 Laravel 5.5,这就足够了。

现在您已经准备好在应用程序中使用 laravel-cart。

配置

要将购物车保存到数据库中以便稍后检索,该软件包需要知道要使用哪个数据库连接以及表名。默认情况下,软件包将使用默认数据库连接并使用名为 laravel-cart 的表。如果您想更改这些选项,您必须发布 config 文件。

php artisan vendor:publish --provider="Rariteth\LaravelCart\CartServiceProvider" --tag="laravel-cart-config"

这将为您提供一个 cart.php 配置文件,您可以在其中进行更改。

为了方便您,该软件包还包括一个可立即使用的 migration,您可以通过运行以下命令来发布它:

php artisan vendor:publish --provider="Rariteth\LaravelCart\CartServiceProvider" --tag="migrations"

这将把 laravel-cart 表的迁移文件放入 database/migrations 目录。现在您只需运行 php artisan migrate 来迁移您的数据库。

实例

为了使用多个实例,如 '心愿单'、'some-other-items' 等,请向容器中注入具有 'instanceName' 和 'guardName' 参数的 CartInstance。

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Instance 'whishlist' with guard 'web'
        $this->app
             ->when(
                [
                    Whishlist\ManageController::class,
                    Whishlist\CheckoutController::class
                ]
             )
             ->needs(CartRepositoryInterface::class)
                ->give(function () {
                    return new CartRepository(new CartInstance('whishlist', 'web'));
                });

        // Instance 'other-cart' with guard 'frontend'
        $this->app
             ->when(OtherCartController::class)
             ->needs(CartInstanceInterface::class)
             ->give(function () {
                return new CartInstance('other-cart', 'frontend');
             });
             
...

模型

所有产品都应实现 BuyableInterface 接口。

示例

    class CartController extends Controller
    {
        /**
         * @var CartRepositoryInterface
         */
        private $cartRepository;

        /**
         * CartController constructor.
         *
         * @param CartRepositoryInterface $cartRepository
         */
        public function __construct(CartRepositoryInterface $cartRepository)
        {
            $this->cartRepository = $cartRepository;
        }

        /**
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
         */
        public function index()
        {
            $items = $this->cartRepository->getItems();

            return view('cart.index', compact('items'));
        }

        /**
         * @param Product $product
         *
         * @return \Illuminate\Http\RedirectResponse
         */
        public function add(Product $product)
        {
            $qty = 1;
            $this->cartRepository->add($product, $qty);

            return redirect()->route('cart.index');
        }

        /**
        * @return \Illuminate\Http\RedirectResponse
        */
        public function clear()
        {
           $this->cartRepository->clear();

           return redirect()->route('cart.index');
        }

        /**
        * @param string $rowId
        *
        * @return \Illuminate\Http\RedirectResponse
        */
        public function remove(string $rowId)
        {
           if ($cartItem = $this->cartRepository->get($rowId)) {
               $this->cartRepository->remove($cartItem);

               return redirect()->route('cart.index');
           }

           return abort(404);
        }

        /**
        * @param string $rowId
        * @param int    $qty
        *
        * @return \Illuminate\Http\RedirectResponse
        */
        public function updateQty(string $rowId, int $qty)
        {
           if ($cartItem = $this->cartRepository->get($rowId)) {

               $cartItem->setQty($qty);
               $this->cartRepository->update($cartItem);

               return redirect()->route('cart.index');
           }

           return abort(404);
        }
        
        /**
         * @param CartRepositoryInterface $cartRepository
         */
        public function refreshCartItems(CartRepositoryInterface $cartRepository): void
        {
            $shouldRefreshItems = $cartRepository->search(function (CartItem $cartItem) {
                return $this->shouldRefreshCartItem($cartItem);
            });

            $cartRepository->refresh($shouldRefreshItems);
        }
        
        /**
         * @param CartRepositoryInterface $cartRepository
         */
        public function refreshCartItems(CartRepositoryInterface $cartRepository): void
        {
            $shouldRefreshItems   = $cartRepository->getGuestItems();

            $cartRepository->refresh($shouldRefreshItems);
        }
        
        /**
         * @param CartRepositoryInterface $cartRepository
         */
        public function removeOldCartItems(CartRepositoryInterface $cartRepository): void
        {
            $itemsForRemove = $cartRepository->search(function (CartItem $cartItem) {
                return $this->shouldRemoveCartItem($cartItem);
            });

            $cartRepository->removeBatch($itemsForRemove);
        }
        
        /**
         * @param CartItem $cartItem
         *
         * @return bool
         */
        private function shouldRefreshCartItem(CartItem $cartItem): bool
        {
            return $cartItem->updatedAt < $this->expireAt()->subDay();
        }

        /**
         * @param CartItem $cartItem
         *
         * @return bool
         */
        private function shouldRemoveCartItem(CartItem $cartItem): bool
        {
            return $cartItem->addedAt < now()->subDays(5);
        }
    }