Just add more logics to cart and cart items from here

This commit is contained in:
prashant-webkul 2018-09-20 14:03:28 +05:30
parent e3d1805043
commit 2721080a1b
17 changed files with 216 additions and 109 deletions

View File

@ -6,7 +6,7 @@ use Carbon\Carbon;
//Cart repositories
use Webkul\Cart\Repositories\CartRepository;
use Webkul\Cart\Repositories\CartProductRepository;
use Webkul\Cart\Repositories\CartItemRepository;
//Customer repositories
use Webkul\Customer\Repositories\CustomerRepository;
@ -24,67 +24,75 @@ use Cookie;
class Cart {
protected $_config;
protected $cart;
protected $cartProduct;
protected $cartItem;
protected $customer;
//Cookie expiry limit in minutes
protected $minutes = 150;
protected $minutes;
public function __construct(CartRepository $cart, CartProductRepository $cartProduct, CustomerRepository $customer ,$minutes = 150) {
public function __construct(CartRepository $cart, CartItemRepository $cartItem, CustomerRepository $customer, $minutes = 150) {
$this->customer = $customer;
$this->cart = $cart;
$this->cartProduct = $cartProduct;
$this->cartItem = $cartItem;
$this->minutes = $minutes;
}
public function guestUnitAdd($id) {
public function guestUnitAdd($id, $data) {
//empty array for storing the products
//empty array for storing the products.
$products = array();
if(Cookie::has('cart_session_id')) {
//getting the cart session id from cookie
//getting the cart session id from cookie.
$cart_session_id = Cookie::get('cart_session_id');
//finding current cart instance in the database table.
$current_cart = $this->cart->findOneByField('session_id', $cart_session_id);
//check there is any cart or not
//check there is any cart or not.
if(isset($current_cart)) {
$current_cart_id = $current_cart['id'] ?? $current_cart->id;
$current_cart_session_id = $current_cart['session_id'] ?? $current_cart->session_id;
} else {
//if someone deleted then take the flow to the normal
//if someone deleted handle flow to the normal.
$this->repairCart($cart_session_id, $id);
}
//matching the session id present in the cookie and database are same or not.
//Matching the session id present in the cookie and database are same or not.
if((session()->get('cart_session_id') == Cookie::get('cart_session_id')) && ($current_cart_session_id == session()->get('cart_session_id'))) {
$current_cart_items = $this->cart->items($current_cart_id);
$current_cart_products = array();
$current_cart_products = $this->cart->getProducts($current_cart_id);
foreach($current_cart_items as $current_cart_item) {
array_push($current_cart_products, $this->cartItem->getProduct($current_cart_item->id));
}
//checking new product coming in the cart is new or previously added item.
foreach($current_cart_products as $key => $value) {
$product_id = $value['id'] ?? $value->id;
$product_id = $value;
if($product_id == $id) {
//create status code to communicate with session flash
//create status code to communicate with session flash.
$cartItemId = $this->cartItem->findOneByField('product_id', $id)->id;
$cartItemQuantity = $this->cartItem->findOneByField('product_id', $id)->quantity;
$this->cartItem->update(['quantity' => $cartItemQuantity + $data['quantity']], $cartItemId);
session()->flash('error', 'Item Already In Cart');
//remove this its temporary
//remove this its temporary.
dump('Item Already In Cart');
return redirect()->back();
@ -92,29 +100,27 @@ class Cart {
}
//cart data being attached to the instace.
$cart_data = $this->cart->attach($current_cart_id, $id, 1);
$cart_data = $this->cart->attach($current_cart_id, $id, $data['quantity']);
//getting the products after being attached to cart instance
$cart_products = $this->cart->getProducts($current_cart_id);
//getting the products after being attached to cart instance.
$cart_products = $this->cart->items($current_cart_id);
//storing the information in session.
session()->put('cart_data', [$current_cart, $cart_products]);
session()->flash('Success', 'Item Added To Cart Successfully');
dump($cart_products);
//return the control to the controller
//return the control to the controller.
return redirect()->back();
} else {
//repair the cart, will remake the session
//repair the cart, will remake the session.
//and add the product in the new cart instance.
$this->repairCart($cart_session_id, $id);
}
} else {
//function call
$this->createNewCart($id);
$this->createNewCart($id, $data);
}
}
@ -128,11 +134,12 @@ class Cart {
* @return mixed
*/
public function createNewCart($id) {
public function createNewCart($id, $data) {
$fresh_cart_session_id = session()->getId();
$data['session_id'] = $fresh_cart_session_id;
if(!auth()->guard('customer')->check())
$data['session_id'] = $fresh_cart_session_id;
$data['channel_id'] = core()->getCurrentChannel()->id;
@ -144,10 +151,12 @@ class Cart {
$cart_product['product_id'] = $id;
$cart_product['quantity'] = 1;
$cart_product['quantity'] = $data['quantity'];
$cart_product['cart_id'] = $new_cart_id;
$cart_product['price'] = $new_cart_id;
if($cart_product = $this->cart->attach($new_cart_id, $cart_product['product_id'], $cart_product['quantity'])) {
session()->put('cart_data', [$cart, $cart_product]);
@ -248,7 +257,7 @@ class Cart {
of the logged in user.
*/
public function add($id) {
public function add($id, $itemdata) {
$products = array();
@ -272,24 +281,32 @@ class Cart {
* table.
*/
if(isset($customer_cart)) {
$customer_cart_products = $this->cart->getProducts($customer_cart_id);
$customer_cart_items = $this->cart->items($customer_cart_id);
$customer_cart_products = array();
foreach($customer_cart_items as $customer_cart_item) {
array_push($customer_cart_products, $this->cartItem->getProduct($customer_cart_item->id));
}
if (isset($customer_cart_products)) {
foreach ($customer_cart_products as $customer_cart_product) {
if($customer_cart_product->id == $id) {
dump('Item already exists in cart');
if($customer_cart_product == $id) {
$cartItemId = $this->cartItem->findOneByField('product_id', $id)->id;
$cartItemQuantity = $this->cartItem->findOneByField('product_id', $id)->quantity;
$this->cartItem->update(['quantity' => $cartItemQuantity + $itemdata['quantity']], $cartItemId);
session()->flash('error', 'Item already exists in cart');
return redirect()->back();
//maybe increase the quantity in here
}
}
//add the product in the cart
$this->cart->attach($customer_cart_id, $id, 1);
$this->cart->attach($customer_cart_id, $id, $itemdata['quantity']);
session()->flash('success', 'Item Added To Cart Successfully');
@ -315,7 +332,7 @@ class Cart {
if($new_cart = $this->cart->create($data)) {
$new_cart_id = $new_cart->id ?? $new_cart['id'];
$this->cart->attach($new_cart_id, $id, 1);
$this->cart->attach($new_cart_id, $id, $itemdata['quantity']);
session()->flash('success', 'Item Added To Cart Successfully');
@ -370,46 +387,60 @@ class Cart {
$guest_cart = $this->cart->findOneByField('session_id', $cart_session_id);
if(!isset($guest_cart)) {
dd('Some One Deleted Cart or it wasn\'t there from the start');
dd('Some One Deleted Cart');
return redirect()->back();
}
$guest_cart_items = $this->cart->items($guest_cart->id);
$guest_cart_products = $this->cart->getProducts($guest_cart->id);
$guest_cart_products = array();
foreach($guest_cart_items as $guest_cart_item) {
array_push($guest_cart_products, $this->cartItem->getProduct($guest_cart_item->id));
}
//check if the current logged in customer is also
//having any previously saved cart instances.
$customer_cart = $this->cart->findOneByField('customer_id', $customer_id);
if(isset($customer_cart)) {
$customer_cart_products = $this->cart->getProducts($customer_cart->id);
$customer_cart_items = $this->cart->items($customer_cart->id);
$customer_cart_products = array();
foreach($customer_cart_items as $customer_cart_item) {
array_push($customer_cart_products, $this->cartItem->getProduct($customer_cart_item->id));
}
foreach($guest_cart_products as $key => $guest_cart_product) {
foreach($customer_cart_products as $customer_cart_product) {
if($guest_cart_product == $customer_cart_product) {
$cartItemId = $this->cartItem->findWhere(['cart_id'=> $guest_cart->id, 'product_id' => $guest_cart_product])[0]->id;
if($guest_cart_product->id == $customer_cart_product->id) {
$cartItemQuantity = $this->cartItem->findWhere(['cart_id'=> $guest_cart->id, 'product_id' => $guest_cart_product])[0]->quantity;
$quantity = $guest_cart_product->toArray()['pivot']['quantity'] + 1;
$customerItemId = $this->cartItem->findWhere(['cart_id'=> $customer_cart->id, 'product_id' => $customer_cart_product])[0]->id;
$pivot = $guest_cart_product->toArray()['pivot'];
$customerItemQuantity = $this->cartItem->findWhere(['cart_id'=> $customer_cart->id, 'product_id' => $customer_cart_product])[0]->quantity;
$saveQuantity = $this->cart->updateRelatedForMerge($pivot, 'quantity', $quantity);
$this->cartItem->delete($cartItemId);
$this->cartItem->update(['quantity' => $cartItemQuantity + $customerItemQuantity], $customerItemId);
unset($guest_cart_products[$key]);
}
}
}
//insert the new products here.
foreach ($guest_cart_products as $key => $guest_cart_product) {
$product = $guest_cart_product->toArray();
// dd($this->cartItem->findWhere(['cart_id'=> $guest_cart->id, 'product_id' => $guest_cart_product])[0]);
$cartItemId = $this->cartItem->findWhere(['cart_id'=> $guest_cart->id, 'product_id' => $guest_cart_product])[0]->id;
$this->cart->updateRelatedForMerge($product['pivot'], 'cart_id', $customer_cart->id);
$this->cartItem->update(['cart_id' => $customer_cart->id], $cartItemId);
}
//detach with guest cart records
$this->cart->detachAndDeleteParent($guest_cart->id);
$this->cart->deleteParent($guest_cart->id);
Cookie::queue(Cookie::forget('cart_session_id'));

View File

@ -13,7 +13,7 @@ class CreateCartProductsTable extends Migration
*/
public function up()
{
Schema::create('cart_products', function (Blueprint $table) {
Schema::create('cart_items', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');

View File

@ -7,7 +7,7 @@ use Illuminate\Http\Response;
//Cart repositories
use Webkul\Cart\Repositories\CartRepository;
use Webkul\Cart\Repositories\CartProductRepository;
use Webkul\Cart\Repositories\CartItemRepository;
//Customer repositories
use Webkul\Customer\Repositories\CustomerRepository;
@ -36,11 +36,11 @@ class CartController extends Controller
protected $cart;
protected $cartProduct;
protected $cartItem;
protected $customer;
public function __construct(CartRepository $cart, CartProductRepository $cartProduct, CustomerRepository $customer) {
public function __construct(CartRepository $cart, CartItemRepository $cartItem, CustomerRepository $customer) {
$this->middleware('customer')->except(['add', 'remove', 'test']);
@ -48,7 +48,7 @@ class CartController extends Controller
$this->cart = $cart;
$this->cartProduct = $cartProduct;
$this->cartItem = $cartItem;
}
/**
@ -60,11 +60,11 @@ class CartController extends Controller
*/
public function add($id) {
$data = request()->input();
if(auth()->guard('customer')->check()) {
Cart::add($id);
Cart::add($id, $data);
} else {
Cart::guestUnitAdd($id);
Cart::guestUnitAdd($id, $data);
}
return redirect()->back();
@ -81,20 +81,26 @@ class CartController extends Controller
return redirect()->back();
}
// public function test() {
// $cookie = Cookie::get('cart_session_id');
/**
* This is a test for
* relationship existence
* from cart item to product
*
* @return Array
*/
public function test() {
$cartItems = $this->cart->items(75);
// $cart = $this->cart->findOneByField('session_id', $cookie);
$products = array();
foreach($cartItems as $cartItem) {
$cartItemId = $cartItem->id;
// $cart_products = $this->cart->getProducts($cart->id);
$this->cart->updateItem(75, $cartItemId, 'quantity', $cartItem->quantity+1);
// foreach($cart_products as $cart_product) {
// $quantity = $cart_product->toArray()['pivot']['quantity'] + 1;
array_push($products, ['product_id' => $this->cartItem->getProduct($cartItemId), 'quantity' => $cartItem->quantity]);
}
// $pivot = $cart_product->toArray()['pivot'];
// $saveQuantity = $this->cart->saveRelated($pivot, 'quantity', $quantity+1);
// }
// dd('done');
// }
dd($products);
return response()->json($products, 200);
}
}

View File

@ -7,6 +7,8 @@ use Illuminate\Support\Collection;
use Webkul\Cart\Repositories\CartRepository;
use Webkul\Cart\Repositories\CartItemRepository;
use Cookie;
use Cart;
/**
@ -32,27 +34,42 @@ class CartComposer
* @param View $view
* @return void
*/
public function __construct(CartRepository $cart) {
public function __construct(CartRepository $cart, CartItemRepository $cartItem) {
$this->cart = $cart;
$this->cartItem = $cartItem;
}
public function compose(View $view) {
if(auth()->guard('customer')->check()) {
$cart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id);
$cart_products = $this->cart->getProducts($cart['id']);
if(isset($cart)) {
$cart_items = $this->cart->items($cart['id']);
// dd($cart_products);
$cart_products = array();
$view->with('cart', $cart_products);
foreach($cart_items as $cart_item) {
array_push($cart_products, $this->cartItem->getProduct($cart_item->id));
}
$view->with('cart', $cart_products);
}
} else {
if(Cookie::has('cart_session_id')) {
$cart = $this->cart->findOneByField('session_id', Cookie::get('cart_session_id'));
$cart_products = $this->cart->getProducts($cart['id']);
if(isset($cart)) {
$cart_items = $this->cart->items($cart['id']);
$view->with('cart', $cart_products);
$cart_products = array();
foreach($cart_items as $cart_item) {
array_push($cart_products, $this->cartItem->getProduct($cart_item->id));
}
$view->with('cart', $cart_products);
}
}
}
}

View File

@ -16,6 +16,10 @@ class Cart extends Model
public function with_products() {
return $this->belongsToMany(Product::class, 'cart_products')->withPivot('id', 'product_id','quantity', 'cart_id');
return $this->belongsToMany(Product::class, 'cart_items')->withPivot('id', 'product_id','quantity', 'cart_id');
}
public function items() {
return $this->hasMany('Webkul\Cart\Models\CartItem');
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Webkul\Cart\Models;
use Illuminate\Database\Eloquent\Model;
class CartItem extends Model
{
protected $table = 'cart_items';
protected $fillable = ['product_id','quantity','cart_id','tax_category_id','coupon_code'];
public function product() {
return $this->hasOne('Webkul\Product\Models\Product', 'id', 'product_id');
}
}

View File

@ -1,11 +0,0 @@
<?php
namespace Webkul\Cart\Models;
use Illuminate\Database\Eloquent\Model;
class CartProduct extends Model
{
protected $table = 'cart_products';
protected $fillable = ['product_id','quantity','cart_id','tax_category_id','coupon_code'];
}

View File

@ -46,10 +46,13 @@ class CartServiceProvider extends ServiceProvider
//to make the cart facade and bind the
//alias to the class needed to be called.
$loader = AliasLoader::getInstance();
$loader->alias('cart', CartFacade::class);
$loader->alias('cart', Cart::class);
$this->app->singleton('cart', function () {
return new Cart();
return new cart();
});
$this->app->bind('cart', 'Webkul\Cart\Cart');
}
}
}

View File

@ -11,7 +11,7 @@ use Webkul\Core\Eloquent\Repository;
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CartProductRepository extends Repository
class CartItemRepository extends Repository
{
/**
* Specify Model class name
@ -21,7 +21,7 @@ class CartProductRepository extends Repository
function model()
{
return 'Webkul\Cart\Models\CartProduct';
return 'Webkul\Cart\Models\CartItem';
}
/**
@ -51,4 +51,8 @@ class CartProductRepository extends Repository
return $cartitems;
}
public function getProduct($cartItemId) {
return $this->model->find($cartItemId)->product->id;
}
}

View File

@ -77,12 +77,25 @@ class CartRepository extends Repository
*
* @return Mixed
*/
public function updateRelatedForMerge($pivot, $column, $value) {
$cart_product = $this->model->findOrFail($pivot['cart_id']);
public function updateRelatedForMerge($cart_id, $product_id, $column, $value) {
$cart_product = $this->model->findOrFail($cart_id);
return $cart_product->with_products()->updateExistingPivot($pivot['product_id'], array($column => $value));
return $cart_product->with_products()->updateExistingPivot($product_id, array($column => $value));
}
/**
* Update the quantity of
* previously added item
* in the cart.
*
* @return Mixed
*/
// public function updateRelatedInItems($cartId, $cartItemId, $column, $value) {
// return $this->updateItem($cartId)->syncWithoutDetaching($cartItemId, [$column => $value]);
// }
/**
* Method to detach
* associations.
@ -92,12 +105,13 @@ class CartRepository extends Repository
*
* @return Mixed
*/
public function detachAndDeleteParent($cart_id) {
public function deleteParent($cart_id) {
$cart = $this->model->find($cart_id);
//apply strict check for verifying guest ownership on this record.
$cart->with_products()->detach();
return $this->model->destroy($cart_id);
}
public function items($cartId) {
return $this->model->find($cartId)->items;
}
}

View File

@ -140,8 +140,8 @@ class Core
$channel = $this->getCurrentChannel();
$currencyCode = $channel->base_currency->code;
// $currencyCode = $channel->base_currency;
// $currencyCode = $channel->base_currency->code;
$currencyCode = $channel->base_currency;
return currency($price, $currencyCode);
}

View File

@ -59,13 +59,13 @@ class AttributeToSelectCriteria extends AbstractProduct implements CriteriaInter
public function apply($model, RepositoryInterface $repository)
{
$model = $model->select('products.*');
foreach ($this->attributeToSelect as $code) {
$attribute = $this->attribute->findOneByField('code', $code);
if(!$attribute)
continue;
$productValueAlias = 'pav_' . $attribute->code;
$model = $model->leftJoin('product_attribute_values as ' . $productValueAlias, function($qb) use($attribute, $productValueAlias) {

View File

@ -23,7 +23,7 @@ class ProductAttributeValue extends Model
'boolean' => 'boolean_value',
'select' => 'integer_value',
'multiselect' => 'text_value',
'datetime' => 'datetime_time',
'datetime' => 'datetime_value',
'date' => 'date_value',
];

View File

@ -14,8 +14,7 @@ Route::group(['middleware' => ['web']], function () {
'view' => 'shop::checkout.onepage'
])->name('shop.checkout');
/* dummy routes ends here */
Route::get('test', 'Webkul\Cart\Http\Controllers\CartController@test');
Route::get('/products/{slug}', 'Webkul\Shop\Http\Controllers\ProductController@index')->defaults('_config', [
'view' => 'shop::products.view'

View File

@ -75,6 +75,8 @@ export default {
initializeDropdown: function() {
this.totalitems = this.items.length;
this.cart_items = this.items;
console.log("The cart items here are = ",cart_items);
}
}
}

View File

@ -91,7 +91,19 @@
</ul>
<cart-dropdown @if(isset($cart)) :items='@json($cart)' @endif></cart-dropdown>
@if(isset($cart))
<cart-dropdown :items='@json($cart)'></cart-dropdown>
@else
<ul class="cart-dropdown">
<li class="cart-summary">
<span class="icon cart-icon"></span>
<span class="cart"><span class="cart-count">0</span>Products</span>
<span class="icon arrow-down-icon"></span>
</li>
</ul>
@endif
{{-- Meant for responsive views only --}}
<ul class="ham-dropdown-container">

View File

@ -8,15 +8,13 @@
@section('content-wrapper')
<section class="product-detail">
<div class="category-breadcrumbs">
<span class="breadcrumb">Home</span> > <span class="breadcrumb">Men</span> > <span class="breadcrumb">Slit Open Jeans</span>
</div>
<div class="layouter">
<form method="POST" action="{{ route('cart.add', $product->id) }}">
@csrf()
<input type="hidden" name="product">
<input type="hidden" name="product" value="{{ $product->id }}">
@include ('shop::products.view.gallery')
@ -37,6 +35,17 @@
{{ $product->short_description }}
</div>
<div class="quantity control-group">
<label class="reqiured">Quantity</label>
<input name="quantity" class="control" value="1" v-validate="'numeric'" required style="width: 60px;">
</div>
@if ($product->type == 'configurable')
<input type="hidden" value="true" name="is_configurable">
@else
<input type="hidden" value="false" name="is_configurable">
@endif
@include ('shop::products.view.configurable-options')
<accordian :title="'{{ __('shop::app.products.description') }}'" :active="true">