sarga/packages/Webkul/API/Http/Controllers/Shop/CartController.php

228 lines
5.9 KiB
PHP
Raw Normal View History

2019-05-07 11:36:21 +00:00
<?php
namespace Webkul\API\Http\Controllers\Shop;
use Illuminate\Support\Facades\Event;
use Webkul\Checkout\Repositories\CartRepository;
use Webkul\Checkout\Repositories\CartItemRepository;
use Webkul\API\Http\Resources\Checkout\Cart as CartResource;
use Cart;
use Webkul\Customer\Repositories\WishlistRepository;
2019-05-07 11:36:21 +00:00
class CartController extends Controller
{
/**
* Contains current guard
*
* @var array
*/
protected $guard;
/**
* CartRepository object
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Checkout\Repositories\CartRepository
2019-05-07 11:36:21 +00:00
*/
protected $cartRepository;
/**
* CartItemRepository object
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Checkout\Repositories\CartItemRepository
2019-05-07 11:36:21 +00:00
*/
protected $cartItemRepository;
/**
* WishlistRepository object
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Checkout\Repositories\WishlistRepository
*/
protected $wishlistRepository;
2019-05-07 11:36:21 +00:00
/**
* Controller instance
*
2020-03-05 14:19:14 +00:00
* @param \Webkul\Checkout\Repositories\CartRepository $cartRepository
2020-03-05 05:34:57 +00:00
* @param \Webkul\Checkout\Repositories\CartItemRepository $cartItemRepository
* @param \Webkul\Checkout\Repositories\WishlistRepository $wishlistRepository
2019-05-07 11:36:21 +00:00
*/
public function __construct(
CartRepository $cartRepository,
CartItemRepository $cartItemRepository,
WishlistRepository $wishlistRepository
2020-02-24 12:57:24 +00:00
) {
2019-05-07 11:36:21 +00:00
$this->guard = request()->has('token') ? 'api' : 'customer';
auth()->setDefaultDriver($this->guard);
2019-09-27 09:53:58 +00:00
2019-05-07 11:36:21 +00:00
// $this->middleware('auth:' . $this->guard);
2019-09-27 09:53:58 +00:00
2019-05-07 11:36:21 +00:00
$this->_config = request('_config');
$this->cartRepository = $cartRepository;
$this->cartItemRepository = $cartItemRepository;
$this->wishlistRepository = $wishlistRepository;
2019-05-07 11:36:21 +00:00
}
/**
* Get customer cart
*
* @return \Illuminate\Http\Response
*/
public function get()
{
$customer = auth($this->guard)->user();
$cart = Cart::getCart();
return response()->json([
2020-03-04 06:32:53 +00:00
'data' => $cart ? new CartResource($cart) : null,
2019-05-07 11:36:21 +00:00
]);
}
/**
* Store a newly created resource in storage.
*
2020-03-05 05:34:57 +00:00
* @param int $id
2019-05-07 11:36:21 +00:00
* @return \Illuminate\Http\Response
*/
public function store($id)
{
if (request()->get('is_buy_now')) {
Event::dispatch('shop.item.buy-now', $id);
}
2020-02-24 12:57:24 +00:00
2019-12-24 14:01:13 +00:00
Event::dispatch('checkout.cart.item.add.before', $id);
2019-05-07 11:36:21 +00:00
2019-10-11 09:36:02 +00:00
$result = Cart::addProduct($id, request()->except('_token'));
2019-05-07 11:36:21 +00:00
if (! $result) {
$message = session()->get('warning') ?? session()->get('error');
2019-10-31 08:21:53 +00:00
2019-05-07 11:36:21 +00:00
return response()->json([
2020-03-04 06:32:53 +00:00
'error' => session()->get('warning'),
2020-02-24 12:57:24 +00:00
], 400);
2019-05-07 11:36:21 +00:00
}
2020-02-24 12:57:24 +00:00
if ($customer = auth($this->guard)->user()) {
2019-10-31 08:20:17 +00:00
$this->wishlistRepository->deleteWhere(['product_id' => $id, 'customer_id' => $customer->id]);
2020-02-24 12:57:24 +00:00
}
2019-12-24 14:01:13 +00:00
Event::dispatch('checkout.cart.item.add.after', $result);
2019-05-07 11:36:21 +00:00
Cart::collectTotals();
$cart = Cart::getCart();
return response()->json([
2020-02-24 12:57:24 +00:00
'message' => __('shop::app.checkout.cart.item.success'),
2020-03-04 06:32:53 +00:00
'data' => $cart ? new CartResource($cart) : null,
2020-02-24 12:57:24 +00:00
]);
2019-05-07 11:36:21 +00:00
}
/**
* Update the specified resource in storage.
*
* @return \Illuminate\Http\Response
*/
public function update()
{
2020-02-24 12:57:24 +00:00
foreach (request()->get('qty') as $qty) {
2019-05-07 11:36:21 +00:00
if ($qty <= 0) {
return response()->json([
2020-03-04 06:32:53 +00:00
'message' => trans('shop::app.checkout.cart.quantity.illegal'),
2020-02-24 12:57:24 +00:00
], 401);
2019-05-07 11:36:21 +00:00
}
}
foreach (request()->get('qty') as $itemId => $qty) {
$item = $this->cartItemRepository->findOneByField('id', $itemId);
2019-12-24 14:01:13 +00:00
Event::dispatch('checkout.cart.item.update.before', $itemId);
2019-05-07 11:36:21 +00:00
2019-10-15 13:35:46 +00:00
Cart::updateItems(request()->all());
2019-05-07 11:36:21 +00:00
2019-12-24 14:01:13 +00:00
Event::dispatch('checkout.cart.item.update.after', $item);
2019-05-07 11:36:21 +00:00
}
Cart::collectTotals();
$cart = Cart::getCart();
return response()->json([
2020-02-24 12:57:24 +00:00
'message' => __('shop::app.checkout.cart.quantity.success'),
2020-03-04 06:32:53 +00:00
'data' => $cart ? new CartResource($cart) : null,
2020-02-24 12:57:24 +00:00
]);
2019-05-07 11:36:21 +00:00
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\Response
*/
public function destroy()
{
2019-12-24 14:01:13 +00:00
Event::dispatch('checkout.cart.delete.before');
2019-09-27 09:53:58 +00:00
2019-05-07 11:36:21 +00:00
Cart::deActivateCart();
2019-12-24 14:01:13 +00:00
Event::dispatch('checkout.cart.delete.after');
2019-05-07 11:36:21 +00:00
$cart = Cart::getCart();
return response()->json([
2020-02-24 12:57:24 +00:00
'message' => __('shop::app.checkout.cart.item.success-remove'),
2020-03-04 06:32:53 +00:00
'data' => $cart ? new CartResource($cart) : null,
2020-02-24 12:57:24 +00:00
]);
2019-05-07 11:36:21 +00:00
}
/**
* Remove the specified resource from storage.
*
2020-03-05 05:34:57 +00:00
* @param int $id
2019-05-07 11:36:21 +00:00
* @return \Illuminate\Http\Response
*/
public function destroyItem($id)
{
2019-12-24 14:01:13 +00:00
Event::dispatch('checkout.cart.item.delete.before', $id);
2019-05-07 11:36:21 +00:00
Cart::removeItem($id);
2019-12-24 14:01:13 +00:00
Event::dispatch('checkout.cart.item.delete.after', $id);
2019-05-07 11:36:21 +00:00
Cart::collectTotals();
$cart = Cart::getCart();
return response()->json([
2020-02-24 12:57:24 +00:00
'message' => __('shop::app.checkout.cart.item.success-remove'),
2020-03-04 06:32:53 +00:00
'data' => $cart ? new CartResource($cart) : null,
2020-02-24 12:57:24 +00:00
]);
2019-05-07 11:36:21 +00:00
}
/**
2020-03-05 05:34:57 +00:00
* Function to move a already added product to wishlist will run only on customer authentication.
2019-05-07 11:36:21 +00:00
*
2020-03-05 05:34:57 +00:00
* @param \Webkul\Checkout\Repositories\CartItemRepository $id
2019-05-07 11:36:21 +00:00
*/
public function moveToWishlist($id)
{
2019-12-24 14:01:13 +00:00
Event::dispatch('checkout.cart.item.move-to-wishlist.before', $id);
2019-05-07 11:36:21 +00:00
Cart::moveToWishlist($id);
2019-12-24 14:01:13 +00:00
Event::dispatch('checkout.cart.item.move-to-wishlist.after', $id);
2019-05-07 11:36:21 +00:00
Cart::collectTotals();
$cart = Cart::getCart();
return response()->json([
2020-02-24 12:57:24 +00:00
'message' => __('shop::app.checkout.cart.move-to-wishlist-success'),
2020-03-04 06:32:53 +00:00
'data' => $cart ? new CartResource($cart) : null,
2020-02-24 12:57:24 +00:00
]);
2019-05-07 11:36:21 +00:00
}
2020-02-24 12:57:24 +00:00
}