Cart Dependency Resolved From Composer to Global helpers and removed the form field check for the cart operations of add, remove and update

This commit is contained in:
prashant-webkul 2018-10-04 19:46:23 +05:30
parent 9018d5cadf
commit adc7052afa
14 changed files with 133 additions and 170 deletions

View File

@ -96,7 +96,7 @@ class Cart {
unset($data['_token']);
//Check if the product is salable
//Check if the product is saleable
if(!isset($data['product']) ||!isset($data['quantity'])) {
session()->flash('error', trans('shop::app.checkout.cart.integrity.missing_fields'));
@ -171,7 +171,7 @@ class Cart {
$cartData['channel_id'] = core()->getCurrentChannel()->id;
// this will auto set the customer id for the cart instances if customer is authenticated
//auth user details else they will be set when the customer is guest
if(auth()->guard('customer')->check()) {
$cartData['customer_id'] = auth()->guard('customer')->user()->id;
@ -191,7 +191,7 @@ class Cart {
if($cart = $this->cart->create($cartData)) {
$itemData['parent']['cart_id'] = $cart->id;
if ($data['is_configurable'] == "true") {
if ($this->product->find($id)->type == "configurable") {
//parent item entry
$itemData['parent']['additional'] = json_encode($data);
if($parent = $this->cartItem->create($itemData['parent'])) {
@ -211,7 +211,7 @@ class Cart {
return redirect()->back();
}
}
} else if($data['is_configurable'] == "false") {
} else if($this->product->find($id)->type != "configurable") {
if($result = $this->cartItem->create($itemData['parent'])) {
session()->put('cart', $cart);
@ -294,13 +294,13 @@ class Cart {
$itemData = $this->prepareItemData($id, $data);
if(session()->has('cart')) {
$cart = session()->get('cart');
$cart = $this->getCart();
$cartItems = $cart->items()->get();
if(isset($cartItems)) {
foreach($cartItems as $cartItem) {
if($data['is_configurable'] == "false") {
if($this->product->find($id)->type == "simple") {
if($cartItem->product_id == $id) {
$prevQty = $cartItem->quantity;
@ -328,7 +328,7 @@ class Cart {
return redirect()->back();
}
} else if($data['is_configurable'] == "true") {
} else if($this->product->find($id)->type == "configurable") {
if($cartItem->type == "configurable") {
$temp = $this->cartItem->findOneByField('parent_id', $cartItem->id);
if($temp->product_id == $data['selected_configurable_option']) {
@ -366,7 +366,7 @@ class Cart {
}
}
if($data['is_configurable'] == "true") {
if($this->product->find($id)->type == "configurable") {
$parent = $cart->items()->create($itemData['parent']);
$itemData['child']['parent_id'] = $parent->id;
@ -374,7 +374,7 @@ class Cart {
// $this->canAddOrUpdate($parent->child->id, $parent->quantity);
$cart->items()->create($itemData['child']);
} else if($data['is_configurable'] == "false"){
} else if($this->product->find($id)->type != "configurable"){
// $this->canAddOrUpdate($parent->id, $parent->quantity);
$parent = $cart->items()->create($itemData['parent']);
@ -406,7 +406,7 @@ class Cart {
public function update($itemIds)
{
if(session()->has('cart')) {
$cart = session()->get('cart');
$cart = $this->getCart();
$items = $cart->items;
@ -446,7 +446,7 @@ class Cart {
public function removeItem($itemId)
{
if(session()->has('cart')) {
$cart = session()->get('cart');
$cart = $this->getCart();
$items = $cart->items;
@ -666,10 +666,10 @@ class Cart {
if(session()->has('cart')) {
$cart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id);
$guestCart = session()->get('cart');
$guestCart = $this->getCart();
if(!isset($cart)) {
$guestCart->update(['customer_id' => auth()->guard('customer')->user()->id]);
$guestCart->update(['customer_id' => auth()->guard('customer')->user()->id, 'is_guest' => 0]);
session()->forget('cart');

View File

@ -1,96 +0,0 @@
<?php
namespace Webkul\Checkout\Http\ViewComposers;
use Illuminate\View\View;
use Illuminate\Support\Collection;
use Webkul\Checkout\Repositories\CartRepository;
use Webkul\Checkout\Repositories\CartItemRepository;
//Product Image Helper Class
use Webkul\Product\Product\ProductImage;
use Cart;
/**
* cart List Composer on Navigation Menu
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CartComposer
{
/**
* The cart implementation
* for shop bundle's navigation
* menu
*/
protected $cart;
/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function __construct(CartRepository $cart, CartItemRepository $cartItem, ProductImage $productImage) {
$this->cart = $cart;
$this->cartItem = $cartItem;
$this->productImage = $productImage;
}
public function compose(View $view) {
// session()->forget('cart');
// return redirect()->back();
if(auth()->guard('customer')->check()) {
$cart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id);
if(isset($cart)) {
$cartItems = $this->cart->items($cart['id']);
$products = array();
foreach($cartItems as $cartItem) {
$image = $this->productImage->getGalleryImages($cartItem->product);
if(isset($image[0]['small_image_url'])) {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, $image[0]['small_image_url'], $cartItem->quantity];
}
else {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, 'null', $cartItem->quantity];
}
}
session()->put('cart', $cart);
$view->with('cart', $products);
}
} else {
if($cart = session()->get('cart')) {
if(isset($cart)) {
$cartItems = $this->cart->items($cart['id']);
$products = array();
foreach($cartItems as $cartItem) {
$image = $this->productImage->getGalleryImages($cartItem->product);
if(isset($image[0]['small_image_url'])) {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, $image[0]['small_image_url'], $cartItem->quantity];
}
else {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, 'null', $cartItem->quantity];
}
}
$view->with('cart', $products);
}
}
}
}
}

View File

@ -1,10 +1,10 @@
<?php
use Webkul\Checkout\Cart;
if (! function_exists('cart')) {
function cart()
{
return new Cart;
return app()->make(Cart::class);
}
}
?>

View File

@ -23,8 +23,6 @@ class CheckoutServiceProvider extends ServiceProvider
$router->aliasMiddleware('admin', RedirectIfNotAdmin::class);
$router->aliasMiddleware('customer', RedirectIfNotCustomer::class);
$this->app->register(ComposerServiceProvider::class);
}
/**

View File

@ -1,32 +0,0 @@
<?php
namespace Webkul\Checkout\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use View;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* @return void
*/
public function boot()
{
//using the class based composers...
View::composer(['shop::layouts.header.index'], 'Webkul\Checkout\Http\ViewComposers\CartComposer');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
//
}
}

View File

@ -60,7 +60,7 @@ class WishlistController extends Controller
*
* @param integer $itemId
*/
public function add() {
public function add($itemId) {
dd('adding item to wishlist');
}

View File

@ -0,0 +1,12 @@
<?php
namespace Webkul\Customer\Models;
use Illuminate\Database\Eloquent\Model;
class Wishlist extends Model
{
protected $table = 'wishlist';
protected $fillable = ['channel_id', 'product_id', 'customer_id', 'item_options','moved_to_cart','shared','time_of_moving'];
}

View File

@ -0,0 +1,54 @@
<?php
namespace Webkul\Customer\Repositories;
use Webkul\Core\Eloquent\Repository;
/**
* Wishlist Reposotory
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class WishlistRepository extends Repository
{
/**
* Specify Model class name
*
* @return mixed
*/
function model()
{
return 'Webkul\Customer\Models\Wishlist';
}
/**
* @param array $data
* @return mixed
*/
public function create(array $data)
{
$wishlist = $this->model->create($data);
return $wishlist;
}
/**
* @param array $data
* @param $id
* @param string $attribute
* @return mixed
*/
public function update(array $data, $id, $attribute = "id")
{
$wishlist = $this->find($id);
$wishlist->update($data);
return $wishlist;
}
}

View File

@ -395,7 +395,7 @@ class ProductRepository extends Repository
]));
$params = request()->input();
return $this->scopeQuery(function($query){
return $query->distinct()->addSelect('products.*');
})->paginate(isset($params['limit']) ? $params['limit'] : 9, ['products.id']);

View File

@ -122,7 +122,6 @@ class CartController extends Controller
$data = request()->except('_token');
foreach($data['qty'] as $id => $quantity) {
// dd($id, $quantity);
if($quantity <= 0) {
session()->flash('warning', trans('shop::app.checkout.cart.quantity.illegal'));
@ -135,5 +134,6 @@ class CartController extends Controller
}
public function test() {
$result = Cart::isConfigurable(9);
}
}

View File

@ -10,6 +10,7 @@ Route::group(['middleware' => ['web']], function () {
'view' => 'shop::products.index'
]);
//checkout and cart
Route::get('checkout/cart', 'Webkul\Shop\Http\Controllers\CartController@index')->defaults('_config', [
'view' => 'shop::checkout.cart.index'
])->name('shop.checkout.cart.index');
@ -25,7 +26,6 @@ Route::group(['middleware' => ['web']], function () {
Route::get('/checkout/cart/remove/{id}', 'Webkul\Shop\Http\Controllers\CartController@remove')->defaults('_config',[
'redirect' => 'shop.checkout.cart.index'
])->name('shop.checkout.cart.remove');
//Routes for product cart ends
Route::get('/checkout/onepage', 'Webkul\Shop\Http\Controllers\OnepageController@index')->defaults('_config', [
'view' => 'shop::checkout.onepage'
@ -98,6 +98,11 @@ Route::group(['middleware' => ['web']], function () {
'redirect' => 'customer.session.index'
])->name('customer.session.destroy');
//wishlist
Route::get('wishlist/add/{id}', 'Webkul\Customer\Http\Controllers\WishlistController@add')->name('customer.wishlist.add');
Route::get('wishlist/remove/{id}', 'Webkul\Customer\Http\Controllers\WishlistController@remove')->name('customer.wishlist.remove');
//customer account
Route::prefix('account')->group(function () {
@ -119,12 +124,9 @@ Route::group(['middleware' => ['web']], function () {
Route::post('profile/edit', 'Webkul\Customer\Http\Controllers\CustomerController@edit')->defaults('_config', [
'view' => 'shop::customers.account.profile.edit'
])->name('customer.profile.edit');
/* Profile Routes Ends Here */
/* Routes for Addresses */
Route::get('address/index', 'Webkul\Customer\Http\Controllers\AddressController@index')->defaults('_config', [
'view' => 'shop::customers.account.address.address'
])->name('customer.address.index');
@ -162,7 +164,6 @@ Route::group(['middleware' => ['web']], function () {
Route::get('reviews', 'Webkul\Customer\Http\Controllers\CustomerController@reviews')->defaults('_config', [
'view' => 'shop::customers.account.reviews.reviews'
])->name('customer.reviews.index');
});
});
});

View File

@ -5,6 +5,7 @@ namespace Webkul\Shop\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Webkul\Product\Product\ProductImage;
use View;
class ComposerServiceProvider extends ServiceProvider

View File

@ -83,49 +83,75 @@
</li>
</ul>
<ul class="cart-dropdown-container">
<?php
$cart = cart()->getCart();
?>
@inject ('productImageHelper', 'Webkul\Product\Product\ProductImage')
<li class="cart-dropdown">
<span class="icon cart-icon"></span>
@if(isset($cart) && session()->has('cart'))
@php
$cartInstance = session()->get('cart');
$items = $cart->items;
@endphp
<div class="dropdown-toggle">
<div style="display: inline-block; cursor: pointer;">
@if($cartInstance->items_qty - intval($cartInstance->items_qty) > 0)
<span class="name"><span class="count"> {{ $cartInstance->items_qty }} Products</span>
@if($cart->items_qty - intval($cart->items_qty) > 0)
<span class="name"><span class="count"> {{ $cart->items_qty }} Products</span>
@else
<span class="name"><span class="count"> {{ intval($cartInstance->items_qty) }} Products</span>
<span class="name"><span class="count"> {{ intval($cart->items_qty) }} Products</span>
@endif
</div>
<i class="icon arrow-down-icon active"></i>
</div>
<div class="dropdown-list" style="display: none; top: 50px; right: 0px">
<div class="dropdown-container">
<div class="dropdown-cart">
<div class="dropdown-header">
<p class="heading">Cart Subtotal - {{ $cartInstance->sub_total }}</p>
<p class="heading">Cart Subtotal - {{ $cart->sub_total }}</p>
</div>
<div class="dropdown-content">
@foreach($items as $item)
@if($item->type == "configurable")
<div class="item">
<div class="item-image" >
@php
$images = $productImageHelper->getProductBaseImage($item->child->product);
@endphp
<img src="{{ $images['small_image_url'] }}" />
</div>
@foreach($cart as $product)
<div class="item-details">
<div class="item">
<div class="item-image" >
<img src="{{$product['2']}}" />
<div class="item-name">{{ $item->child->name }}</div>
<div class="item-price">{{ $item->total }}</div>
<div class="item-qty">Quantity - {{ $item->quantity }}</div>
</div>
</div>
@else
<div class="item">
<div class="item-image" >
@php
$images = $productImageHelper->getProductBaseImage($item->product);
@endphp
<img src="{{ $images['small_image_url'] }}" />
</div>
<div class="item-details">
<div class="item-details">
<div class="item-name">{{$product['0']}}</div>
<div class="item-name">{{ $item->name }}</div>
<div class="item-price">{{$product['1']}}</div>
<div class="item-price">{{ $item->total }}</div>
<div class="item-qty">Quantity - {{$product['3']}}</div>
<div class="item-qty">Quantity - {{ $item->quantity }}</div>
</div>
</div>
</div>
@endif
@endforeach
</div>
@ -206,7 +232,7 @@
<li class="cart-dropdown">
@if(isset($cart) && session()->has('cart'))
@php
$cartInstance = session()->get('cart');
$cart = session()->get('cart');
@endphp
<div class="dropdown-toggle">
<span class="icon cart-icon"></span>
@ -216,7 +242,7 @@
<div class="dropdown-container">
<div class="dropdown-cart">
<div class="dropdown-header">
<p class="heading">Cart Subtotal - {{ $cartInstance->sub_total }}</p>
<p class="heading">Cart Subtotal - {{ $cart->sub_total }}</p>
</div>
<div class="dropdown-content">

View File

@ -1,5 +1,4 @@
@inject ('productImageHelper', 'Webkul\Product\Product\ProductImage')
<?php $images = $productImageHelper->getGalleryImages($product); ?>
<div class="product-image-group">