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

228 lines
6.1 KiB
PHP
Raw Normal View History

2018-09-06 06:20:30 +00:00
<?php
2018-09-28 13:28:54 +00:00
namespace Webkul\Shop\Http\Controllers;
2018-09-06 06:20:30 +00:00
2019-04-02 07:54:11 +00:00
use Webkul\Customer\Repositories\WishlistRepository;
use Webkul\Product\Repositories\ProductRepository;
2020-02-06 13:51:24 +00:00
use Webkul\Checkout\Contracts\Cart as CartModel;
use Illuminate\Support\Facades\Event;
use Cart;
2018-09-06 06:20:30 +00:00
class CartController extends Controller
{
2019-06-28 14:18:52 +00:00
/**
* WishlistRepository Repository object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Customer\Repositories\WishlistRepository
2019-06-28 14:18:52 +00:00
*/
protected $wishlistRepository;
/**
* ProductRepository object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Product\Repositories\ProductRepository
*/
protected $productRepository;
2019-04-02 07:54:11 +00:00
/**
2019-06-28 14:18:52 +00:00
* Create a new controller instance.
2019-04-02 07:54:11 +00:00
*
2020-03-05 13:37:08 +00:00
* @param \Webkul\Customer\Repositories\CartItemRepository $wishlistRepository
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
2019-06-28 14:18:52 +00:00
* @return void
2019-04-02 07:54:11 +00:00
*/
public function __construct(
WishlistRepository $wishlistRepository,
ProductRepository $productRepository
)
{
$this->middleware('customer')->only(['moveToWishlist']);
2018-09-06 06:20:30 +00:00
2019-06-28 14:18:52 +00:00
$this->wishlistRepository = $wishlistRepository;
2019-04-02 07:54:11 +00:00
$this->productRepository = $productRepository;
2020-02-06 11:48:51 +00:00
parent::__construct();
2018-09-10 09:31:34 +00:00
}
2018-09-26 04:21:14 +00:00
/**
2018-10-04 06:43:49 +00:00
* Method to populate the cart page which will be populated before the checkout process.
2018-09-26 04:21:14 +00:00
*
* @return \Illuminate\View\View
2018-09-26 04:21:14 +00:00
*/
2019-01-07 10:30:28 +00:00
public function index()
{
2019-11-05 07:14:26 +00:00
Cart::collectTotals();
2018-09-26 04:21:14 +00:00
return view($this->_config['view'])->with('cart', Cart::getCart());
}
2018-09-11 11:19:40 +00:00
/**
2018-10-10 10:26:27 +00:00
* Function for guests user to add the product in the cart.
2018-09-11 11:19:40 +00:00
*
2020-03-05 13:37:08 +00:00
* @param int $id
* @return \Illuminate\Http\Response
2018-09-11 11:19:40 +00:00
*/
2019-01-07 10:30:28 +00:00
public function add($id)
{
try {
2019-08-19 09:30:24 +00:00
$result = Cart::addProduct($id, request()->all());
if ($this->onFailureAddingToCart($result)) {
return redirect()->back();
}
2020-02-06 13:51:24 +00:00
if ($result instanceof CartModel) {
2019-01-18 11:10:40 +00:00
session()->flash('success', trans('shop::app.checkout.cart.item.success'));
2020-02-20 07:05:51 +00:00
if ($customer = auth()->guard('customer')->user()) {
2019-10-31 08:20:17 +00:00
$this->wishlistRepository->deleteWhere(['product_id' => $id, 'customer_id' => $customer->id]);
2020-02-20 07:05:51 +00:00
}
2019-04-02 07:54:11 +00:00
if (request()->get('is_buy_now')) {
Event::dispatch('shop.item.buy-now', $id);
2020-03-17 06:55:04 +00:00
2019-08-19 09:30:24 +00:00
return redirect()->route('shop.checkout.onepage.index');
}
}
} catch(\Exception $e) {
session()->flash('error', trans($e->getMessage()));
$product = $this->productRepository->find($id);
return redirect()->route('shop.productOrCategory.index', $product->url_key);
}
2019-08-19 09:30:24 +00:00
return redirect()->back();
2018-09-06 06:20:30 +00:00
}
2018-09-28 12:55:48 +00:00
/**
2018-10-10 10:26:27 +00:00
* Removes the item from the cart if it exists
2018-09-28 12:55:48 +00:00
*
2020-03-05 13:37:08 +00:00
* @param int $itemId
* @return \Illuminate\Http\Response
2018-09-28 12:55:48 +00:00
*/
2019-01-07 10:30:28 +00:00
public function remove($itemId)
{
2019-08-19 09:30:24 +00:00
$result = Cart::removeItem($itemId);
2019-01-07 10:30:28 +00:00
2020-02-20 07:05:51 +00:00
if ($result) {
2019-08-19 09:30:24 +00:00
session()->flash('success', trans('shop::app.checkout.cart.item.success-remove'));
2020-02-20 07:05:51 +00:00
}
2018-10-18 09:31:19 +00:00
2018-09-28 12:55:48 +00:00
return redirect()->back();
}
/**
2018-10-10 10:26:27 +00:00
* Updates the quantity of the items present in the cart.
2018-09-28 12:55:48 +00:00
*
2020-03-05 13:37:08 +00:00
* @return \Illuminate\Http\Response
2018-09-28 12:55:48 +00:00
*/
2019-01-07 10:30:28 +00:00
public function updateBeforeCheckout()
{
2019-06-07 11:06:50 +00:00
try {
2019-08-19 09:30:24 +00:00
$result = Cart::updateItems(request()->all());
2018-09-28 12:55:48 +00:00
2020-02-20 07:05:51 +00:00
if ($result) {
2019-08-19 09:30:24 +00:00
session()->flash('success', trans('shop::app.checkout.cart.quantity.success'));
2020-02-20 07:05:51 +00:00
}
2019-06-07 11:06:50 +00:00
} catch(\Exception $e) {
session()->flash('error', trans($e->getMessage()));
2019-03-28 10:06:48 +00:00
}
2018-09-14 13:15:49 +00:00
return redirect()->back();
2018-09-11 11:19:40 +00:00
}
2018-10-30 04:31:01 +00:00
/**
2019-08-19 09:30:24 +00:00
* Function to move a already added product to wishlist will run only on customer authentication.
2018-10-30 04:31:01 +00:00
*
2020-03-05 13:37:08 +00:00
* @param int $id
* @return \Illuminate\Http\Response
2018-10-30 04:31:01 +00:00
*/
2019-01-07 10:30:28 +00:00
public function moveToWishlist($id)
{
2018-10-30 04:31:01 +00:00
$result = Cart::moveToWishlist($id);
2019-08-19 09:30:24 +00:00
if ($result) {
2020-01-13 07:14:36 +00:00
session()->flash('success', trans('shop::app.checkout.cart.move-to-wishlist-success'));
2018-10-30 11:22:36 +00:00
} else {
2020-01-13 07:14:36 +00:00
session()->flash('warning', trans('shop::app.checkout.cart.move-to-wishlist-error'));
2018-10-30 11:22:36 +00:00
}
2019-08-19 09:30:24 +00:00
return redirect()->back();
2018-10-30 04:31:01 +00:00
}
/**
* Apply coupon to the cart
*
2020-03-05 13:37:08 +00:00
* @return \Illuminate\Http\Response
*/
public function applyCoupon()
{
$couponCode = request()->get('code');
try {
if (strlen($couponCode)) {
Cart::setCouponCode($couponCode)->collectTotals();
if (Cart::getCart()->coupon_code == $couponCode) {
return response()->json([
'success' => true,
2020-03-04 06:32:53 +00:00
'message' => trans('shop::app.checkout.total.success-coupon'),
]);
}
}
return response()->json([
'success' => false,
2020-03-04 06:32:53 +00:00
'message' => trans('shop::app.checkout.total.invalid-coupon'),
]);
} catch (\Exception $e) {
2020-01-27 16:06:03 +00:00
report($e);
return response()->json([
'success' => false,
2020-03-04 06:32:53 +00:00
'message' => trans('shop::app.checkout.total.coupon-apply-issue'),
]);
}
}
/**
* Apply coupon to the cart
*
2020-03-05 13:37:08 +00:00
* @return \Illuminate\Http\Response
*/
public function removeCoupon()
{
Cart::removeCouponCode()->collectTotals();
return response()->json([
'success' => true,
2020-03-04 06:32:53 +00:00
'message' => trans('shop::app.checkout.total.remove-coupon'),
]);
}
/**
* Returns true, if result of adding product to cart
* is an array and contains a key "warning" or "info"
*
2020-03-17 06:55:04 +00:00
* @param array $result
*
2020-03-17 06:55:04 +00:00
* @return boolean
*/
private function onFailureAddingToCart($result): bool
2020-03-17 06:55:04 +00:00
{
if (is_array($result) && isset($result['warning'])) {
session()->flash('warning', $result['warning']);
return true;
}
if (is_array($result) && isset($result['info'])) {
session()->flash('info', $result['info']);
return true;
}
return false;
}
}