Merge pull request #45 from bagisto/jitendra

Fixed cart issues
This commit is contained in:
JItendra Singh 2018-10-10 15:57:36 +05:30 committed by GitHub
commit 5770736ee7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 411 additions and 415 deletions

View File

@ -31,10 +31,10 @@ class AttributeFamilyTableSeeder extends Seeder
'code' => 'url_key', 'code' => 'url_key',
'position' => 3 'position' => 3
], [ ], [
'code' => 'new_from', 'code' => 'new',
'position' => 4 'position' => 4
], [ ], [
'code' => 'new_to', 'code' => 'featured',
'position' => 5 'position' => 5
], [ ], [
'code' => 'visible_individually', 'code' => 'visible_individually',

View File

@ -14,9 +14,9 @@ class AttributeTableSeeder extends Seeder
[ [
'code' => 'sku', 'code' => 'sku',
'admin_name' => 'SKU', 'admin_name' => 'SKU',
// 'en' => [ 'en' => [
// 'name' => 'SKU' 'name' => 'SKU'
// ], ],
'type' => 'text', 'type' => 'text',
'position' => 1, 'position' => 1,
'is_unique' => 1, 'is_unique' => 1,
@ -29,9 +29,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'name', 'code' => 'name',
'admin_name' => 'Name', 'admin_name' => 'Name',
// 'en' => [ 'en' => [
// 'name' => 'Name' 'name' => 'Name'
// ], ],
'type' => 'text', 'type' => 'text',
'position' => 2, 'position' => 2,
'is_required' => 1, 'is_required' => 1,
@ -43,9 +43,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'url_key', 'code' => 'url_key',
'admin_name' => 'URL Key', 'admin_name' => 'URL Key',
// 'en' => [ 'en' => [
// 'name' => 'URL Key' 'name' => 'URL Key'
// ], ],
'type' => 'text', 'type' => 'text',
'position' => 3, 'position' => 3,
'is_unique' => 1, 'is_unique' => 1,
@ -56,12 +56,12 @@ class AttributeTableSeeder extends Seeder
'is_configurable' => 0, 'is_configurable' => 0,
'is_user_defined' => 0 'is_user_defined' => 0
], [ ], [
'code' => 'new_from', 'code' => 'new',
'admin_name' => 'New From', 'admin_name' => 'New',
// 'en' => [ 'en' => [
// 'name' => 'New From' 'name' => 'New'
// ], ],
'type' => 'datetime', 'type' => 'boolean',
'position' => 4, 'position' => 4,
'is_required' => 0, 'is_required' => 0,
'value_per_locale' => 0, 'value_per_locale' => 0,
@ -70,12 +70,12 @@ class AttributeTableSeeder extends Seeder
'is_configurable' => 0, 'is_configurable' => 0,
'is_user_defined' => 0 'is_user_defined' => 0
], [ ], [
'code' => 'new_to', 'code' => 'featured',
'admin_name' => 'New To', 'admin_name' => 'Featured',
// 'en' => [ 'en' => [
// 'name' => 'New To' 'name' => 'Featured'
// ], ],
'type' => 'datetime', 'type' => 'boolean',
'position' => 5, 'position' => 5,
'is_required' => 0, 'is_required' => 0,
'value_per_locale' => 0, 'value_per_locale' => 0,
@ -86,9 +86,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'visible_individually', 'code' => 'visible_individually',
'admin_name' => 'Visible Individually', 'admin_name' => 'Visible Individually',
// 'en' => [ 'en' => [
// 'name' => 'Visible Individually' 'name' => 'Visible Individually'
// ], ],
'type' => 'boolean', 'type' => 'boolean',
'position' => 6, 'position' => 6,
'is_required' => 1, 'is_required' => 1,
@ -100,9 +100,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'status', 'code' => 'status',
'admin_name' => 'Status', 'admin_name' => 'Status',
// 'en' => [ 'en' => [
// 'name' => 'Status' 'name' => 'Status'
// ], ],
'type' => 'boolean', 'type' => 'boolean',
'position' => 7, 'position' => 7,
'is_required' => 1, 'is_required' => 1,
@ -114,9 +114,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'short_description', 'code' => 'short_description',
'admin_name' => 'Short Description', 'admin_name' => 'Short Description',
// 'en' => [ 'en' => [
// 'name' => 'Short Description' 'name' => 'Short Description'
// ], ],
'type' => 'textarea', 'type' => 'textarea',
'position' => 8, 'position' => 8,
'is_required' => 1, 'is_required' => 1,
@ -128,9 +128,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'description', 'code' => 'description',
'admin_name' => 'Description', 'admin_name' => 'Description',
// 'en' => [ 'en' => [
// 'name' => 'Description' 'name' => 'Description'
// ], ],
'type' => 'textarea', 'type' => 'textarea',
'position' => 9, 'position' => 9,
'is_required' => 1, 'is_required' => 1,
@ -142,9 +142,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'price', 'code' => 'price',
'admin_name' => 'Price', 'admin_name' => 'Price',
// 'en' => [ 'en' => [
// 'name' => 'Price' 'name' => 'Price'
// ], ],
'type' => 'price', 'type' => 'price',
'position' => 10, 'position' => 10,
'is_required' => 1, 'is_required' => 1,
@ -156,9 +156,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'cost', 'code' => 'cost',
'admin_name' => 'Cost', 'admin_name' => 'Cost',
// 'en' => [ 'en' => [
// 'name' => 'Cost' 'name' => 'Cost'
// ], ],
'type' => 'price', 'type' => 'price',
'position' => 11, 'position' => 11,
'is_required' => 0, 'is_required' => 0,
@ -170,9 +170,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'special_price', 'code' => 'special_price',
'admin_name' => 'Special Price', 'admin_name' => 'Special Price',
// 'en' => [ 'en' => [
// 'name' => 'Special Price' 'name' => 'Special Price'
// ], ],
'type' => 'price', 'type' => 'price',
'position' => 12, 'position' => 12,
'is_required' => 0, 'is_required' => 0,
@ -184,9 +184,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'special_price_from', 'code' => 'special_price_from',
'admin_name' => 'Special Price From', 'admin_name' => 'Special Price From',
// 'en' => [ 'en' => [
// 'name' => 'Special Price From' 'name' => 'Special Price From'
// ], ],
'type' => 'date', 'type' => 'date',
'position' => 13, 'position' => 13,
'is_required' => 0, 'is_required' => 0,
@ -198,9 +198,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'special_price_to', 'code' => 'special_price_to',
'admin_name' => 'Special Price To', 'admin_name' => 'Special Price To',
// 'en' => [ 'en' => [
// 'name' => 'Special Price To' 'name' => 'Special Price To'
// ], ],
'type' => 'date', 'type' => 'date',
'position' => 14, 'position' => 14,
'is_required' => 0, 'is_required' => 0,
@ -212,9 +212,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'meta_title', 'code' => 'meta_title',
'admin_name' => 'Meta Title', 'admin_name' => 'Meta Title',
// 'en' => [ 'en' => [
// 'name' => 'Meta Description' 'name' => 'Meta Description'
// ], ],
'type' => 'textarea', 'type' => 'textarea',
'position' => 15, 'position' => 15,
'is_required' => 0, 'is_required' => 0,
@ -226,9 +226,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'meta_keywords', 'code' => 'meta_keywords',
'admin_name' => 'Meta Keywords', 'admin_name' => 'Meta Keywords',
// 'en' => [ 'en' => [
// 'name' => 'Meta Keywords' 'name' => 'Meta Keywords'
// ], ],
'type' => 'textarea', 'type' => 'textarea',
'position' => 16, 'position' => 16,
'is_required' => 0, 'is_required' => 0,
@ -240,9 +240,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'meta_description', 'code' => 'meta_description',
'admin_name' => 'Meta Description', 'admin_name' => 'Meta Description',
// 'en' => [ 'en' => [
// 'name' => 'Meta Description' 'name' => 'Meta Description'
// ], ],
'type' => 'textarea', 'type' => 'textarea',
'position' => 17, 'position' => 17,
'is_required' => 0, 'is_required' => 0,
@ -254,9 +254,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'width', 'code' => 'width',
'admin_name' => 'Width', 'admin_name' => 'Width',
// 'en' => [ 'en' => [
// 'name' => 'Width' 'name' => 'Width'
// ], ],
'type' => 'text', 'type' => 'text',
'validation' => 'numeric', 'validation' => 'numeric',
'position' => 18, 'position' => 18,
@ -269,9 +269,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'height', 'code' => 'height',
'admin_name' => 'Height', 'admin_name' => 'Height',
// 'en' => [ 'en' => [
// 'name' => 'Height' 'name' => 'Height'
// ], ],
'type' => 'text', 'type' => 'text',
'validation' => 'numeric', 'validation' => 'numeric',
'position' => 19, 'position' => 19,
@ -284,9 +284,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'depth', 'code' => 'depth',
'admin_name' => 'Depth', 'admin_name' => 'Depth',
// 'en' => [ 'en' => [
// 'name' => 'Depth' 'name' => 'Depth'
// ], ],
'type' => 'text', 'type' => 'text',
'validation' => 'numeric', 'validation' => 'numeric',
'position' => 20, 'position' => 20,
@ -299,9 +299,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'weight', 'code' => 'weight',
'admin_name' => 'Weight', 'admin_name' => 'Weight',
// 'en' => [ 'en' => [
// 'name' => 'Weight' 'name' => 'Weight'
// ], ],
'type' => 'text', 'type' => 'text',
'validation' => 'numeric', 'validation' => 'numeric',
'position' => 21, 'position' => 21,
@ -314,9 +314,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'color', 'code' => 'color',
'admin_name' => 'Color', 'admin_name' => 'Color',
// 'en' => [ 'en' => [
// 'name' => 'Color' 'name' => 'Color'
// ], ],
'type' => 'select', 'type' => 'select',
'position' => 22, 'position' => 22,
'is_required' => 0, 'is_required' => 0,
@ -356,9 +356,9 @@ class AttributeTableSeeder extends Seeder
], [ ], [
'code' => 'size', 'code' => 'size',
'admin_name' => 'Size', 'admin_name' => 'Size',
// 'en' => [ 'en' => [
// 'name' => 'Size' 'name' => 'Size'
// ], ],
'type' => 'select', 'type' => 'select',
'position' => 23, 'position' => 23,
'is_required' => 0, 'is_required' => 0,

View File

@ -109,25 +109,11 @@ class Cart {
} }
} }
$child = $childData = null;
if($product->type == 'configurable') { if($product->type == 'configurable') {
//Check if the product is salable //Check if the product is salable
$child = $this->product->findOneByField('id', $data['selected_configurable_option']); $child = $this->product->findOneByField('id', $data['selected_configurable_option']);
$parentData = [
'sku' => $product->sku,
'product_id' => $productId,
'quantity' => $data['quantity'],
'type' => 'configurable',
'name' => $product->name,
'price' => ($price = $child->price), //This should be price from the price helper
'base_price' => $price,
'total' => $price * $data['quantity'],
'base_total' => $price * $data['quantity'],
'weight' => ($weight = $child->weight),
'total_weight' => $weight * $data['quantity'],
'base_total_weight' => $weight * $data['quantity'],
];
//child row data //child row data
$childData = [ $childData = [
'product_id' => $data['selected_configurable_option'], 'product_id' => $data['selected_configurable_option'],
@ -136,33 +122,31 @@ class Cart {
'type' => $child->type, 'type' => $child->type,
'name' => $child->name 'name' => $child->name
]; ];
return ['parent' => $parentData, 'child' => $childData];
} else {
$parentData = [
'sku' => $product->sku,
'product_id' => $productId,
'quantity' => $data['quantity'],
'type' => 'simple',
'name' => $product->name,
'price' => $product->price,
'base_price' => $product->price,
'total' => $product->price * $data['quantity'],
'base_total' => $product->price * $data['quantity'],
'weight' => $product->weight,
'total_weight' => $product->weight * $data['quantity'],
'base_total_weight' => $product->weight * $data['quantity'],
];
return ['parent' => $parentData, 'child' => null];
} }
$parentData = [
'sku' => $product->sku,
'product_id' => $productId,
'quantity' => $data['quantity'],
'type' => $product->type,
'name' => $product->name,
'price' => $price = ($product->type == 'configurable' ? $child->price : $product->price),
'base_price' => $price,
'total' => $price * $data['quantity'],
'base_total' => $price * $data['quantity'],
'weight' => $weight = ($product->type == 'configurable' ? $child->weight : $product->weight),
'total_weight' => $weight * $data['quantity'],
'base_total_weight' => $weight * $data['quantity']
];
return ['parent' => $parentData, 'child' => $childData];
} }
/** /**
* Create new cart instance with the current item success. * Create new cart instance with the current item success.
* *
* @param integer $id * @param integer $id
* @param array $data * @param array $data
* *
* @return Response * @return Response
*/ */
@ -175,63 +159,74 @@ class Cart {
//auth user details else they will be set when the customer is guest //auth user details else they will be set when the customer is guest
if(auth()->guard('customer')->check()) { if(auth()->guard('customer')->check()) {
$cartData['customer_id'] = auth()->guard('customer')->user()->id; $cartData['customer_id'] = auth()->guard('customer')->user()->id;
$cartData['is_guest'] = 0; $cartData['is_guest'] = 0;
$cartData['customer_first_name'] = auth()->guard('customer')->user()->first_name; $cartData['customer_first_name'] = auth()->guard('customer')->user()->first_name;
$cartData['customer_last_name'] = auth()->guard('customer')->user()->last_name; $cartData['customer_last_name'] = auth()->guard('customer')->user()->last_name;
$cartData['customer_email'] = auth()->guard('customer')->user()->email;
} else { } else {
$cartData['is_guest'] = 1; $cartData['is_guest'] = 1;
} }
$cartData['items_count'] = 1; $cartData['items_count'] = 1;
$cartData['items_qty'] = $data['quantity']; $cartData['items_qty'] = $data['quantity'];
if($cart = $this->cart->create($cartData)) { if($cart = $this->cart->create($cartData)) {
$itemData['parent']['cart_id'] = $cart->id; $itemData['parent']['cart_id'] = $cart->id;
$product = $this->product->find($id);
if ($this->product->find($id)->type == "configurable") { if ($product->type == "configurable") {
//parent item entry //parent item entry
$itemData['parent']['additional'] = json_encode($data); $itemData['parent']['additional'] = json_encode($data);
if($parent = $this->cartItem->create($itemData['parent'])) {
if($parent = $this->cartItem->create($itemData['parent'])) {
//child item entry //child item entry
$itemData['child']['parent_id'] = $parent->id; $itemData['child']['parent_id'] = $parent->id;
$itemData['child']['cart_id'] = $cart->id; $itemData['child']['cart_id'] = $cart->id;
if($child = $this->cartItem->create($itemData['child'])) { if($child = $this->cartItem->create($itemData['child'])) {
session()->put('cart', $cart); $this->putCart($cart);
session()->flash('success', trans('shop::app.checkout.cart.item.success')); session()->flash('success', trans('shop::app.checkout.cart.item.success'));
$this->collectTotals(); $this->collectTotals();
return redirect()->back(); return $cart;
} }
} }
} else if($this->product->find($id)->type != "configurable") { } else if($product->type != "configurable") {
if($result = $this->cartItem->create($itemData['parent'])) { if($this->cartItem->create($itemData['parent'])) {
session()->put('cart', $cart); $this->putCart($cart);
session()->flash('success', trans('shop::app.checkout.cart.item.success')); session()->flash('success', trans('shop::app.checkout.cart.item.success'));
$this->collectTotals(); $this->collectTotals();
return redirect()->back(); return $cart;
} }
} }
} }
session()->flash('error', trans('shop::app.checkout.cart.item.error_add')); session()->flash('error', trans('shop::app.checkout.cart.item.error_add'));
return redirect()->back(); return false;
}
/**
* Save cart
*
* @return mixed
*/
public function putCart($cart)
{
if(!auth()->guard('customer')->check()) {
session()->put('cart', $cart);
}
} }
/** /**
* Returns cart * Returns cart
* *
* @return Mixed * @return mixed
*/ */
public function getCart() public function getCart()
{ {
@ -276,38 +271,26 @@ class Cart {
*/ */
public function canAddOrUpdate($itemId, $quantity) public function canAddOrUpdate($itemId, $quantity)
{ {
$item = $this->cartItem->findOneByField('id', $itemId);
$inventories = $item->product->inventories;
$inventory_sources = $item->product->inventory_sources;
$totalQty = 0;
foreach($inventory_sources as $inventory_source) {
if($inventory_source->status && $inventory_source->toArray()['pivot']['qty']) {
$totalQty = $totalQty + $inventory_source->toArray()['pivot']['qty'];
}
}
if ($quantity < 1) { if ($quantity < 1) {
session()->flash('warning', trans('shop::app.checkout.cart.quantity.warning')); session()->flash('warning', trans('shop::app.checkout.cart.quantity.warning'));
return redirect()->back(); return redirect()->back();
} }
if($quantity <= $totalQty) { $item = $this->cartItem->findOneByField('id', $itemId);
if($item->product->haveSufficientQuantity($quantity)) {
return true; return true;
} else {
return false;
} }
return false;
} }
/** /**
* Add Items in a cart with some cart and item details. * Add Items in a cart with some cart and item details.
* *
* @param @id * @param integer $id
* @param $data * @param array $data
* *
* @return void * @return void
*/ */
@ -315,14 +298,14 @@ class Cart {
{ {
$itemData = $this->prepareItemData($id, $data); $itemData = $this->prepareItemData($id, $data);
if(session()->has('cart')) { if($cart = $this->getCart()) {
$cart = $this->getCart(); $product = $this->product->find($id);
$cartItems = $cart->items()->get(); $cartItems = $cart->items;
if(isset($cartItems)) { if($cartItems->count()) {
foreach($cartItems as $cartItem) { foreach($cartItems as $cartItem) {
if($this->product->find($id)->type == "simple") { if($product->type == "simple") {
if($cartItem->product_id == $id) { if($cartItem->product_id == $id) {
$prevQty = $cartItem->quantity; $prevQty = $cartItem->quantity;
@ -348,7 +331,7 @@ class Cart {
return redirect()->back(); return redirect()->back();
} }
} else if($this->product->find($id)->type == "configurable") { } else if($product->type == "configurable") {
if($cartItem->type == "configurable") { if($cartItem->type == "configurable") {
$temp = $this->cartItem->findOneByField('parent_id', $cartItem->id); $temp = $this->cartItem->findOneByField('parent_id', $cartItem->id);
if($temp->product_id == $data['selected_configurable_option']) { if($temp->product_id == $data['selected_configurable_option']) {
@ -384,17 +367,13 @@ class Cart {
} }
} }
if($this->product->find($id)->type == "configurable") { if($product->type == "configurable") {
$parent = $cart->items()->create($itemData['parent']); $parent = $cart->items()->create($itemData['parent']);
$itemData['child']['parent_id'] = $parent->id; $itemData['child']['parent_id'] = $parent->id;
// $this->canAddOrUpdate($parent->child->id, $parent->quantity);
$cart->items()->create($itemData['child']); $cart->items()->create($itemData['child']);
} else if($this->product->find($id)->type != "configurable"){ } else if($product->type != "configurable"){
// $this->canAddOrUpdate($parent->id, $parent->quantity);
$parent = $cart->items()->create($itemData['parent']); $parent = $cart->items()->create($itemData['parent']);
} }
@ -402,16 +381,16 @@ class Cart {
session()->flash('success', trans('shop::app.checkout.cart.item.success')); session()->flash('success', trans('shop::app.checkout.cart.item.success'));
return redirect()->back(); return $cart;
} else { } else {
if(isset($cart)) { if(isset($cart)) {
$this->cart->delete($cart->id); $this->cart->delete($cart->id);
} else { } else {
$this->createNewCart($id, $data); return $this->createNewCart($id, $data);
} }
} }
} else { } else {
$this->createNewCart($id, $data); return $this->createNewCart($id, $data);
} }
} }
@ -421,9 +400,7 @@ class Cart {
*/ */
public function update($itemIds) public function update($itemIds)
{ {
if(session()->has('cart')) { if($cart = $this->getCart()) {
$cart = $this->getCart();
$items = $cart->items; $items = $cart->items;
foreach($items as $item) { foreach($items as $item) {
@ -438,7 +415,7 @@ class Cart {
if($canBe == false) { if($canBe == false) {
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning')); session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
return redirect()->back(); return $cart;
} }
$item->update(['quantity' => $quantity]); $item->update(['quantity' => $quantity]);
@ -447,10 +424,11 @@ class Cart {
} }
} }
} }
session()->flash('success', trans('shop::app.checkout.cart.quantity.success'));
return redirect()->back(); session()->flash('success', trans('shop::app.checkout.cart.quantity.success'));
} }
return $cart;
} }
/** /**
@ -461,46 +439,17 @@ class Cart {
public function removeItem($itemId) public function removeItem($itemId)
{ {
if($cart = $this->getCart()) { if($cart = $this->getCart()) {
$items = $cart->items; $this->cartItem->delete($itemId);
foreach($items as $item) {
if($item->id == $itemId) {
if($item->type == "configurable") {
$child = $item->child;
//delete the child first
$result = $this->cartItem->delete($child->id);
if($result)
$result = $this->cartItem->delete($item->id);
$this->collectTotals();
} else if($item->type == "simple" && $item->parent_id == null){
$result = $this->cartItem->delete($item->id);
$this->collectTotals();
}
}
}
$countItems = $this->cart->findOneByField('id', $cart->id)->items->count();
//delete the cart instance if no items are there //delete the cart instance if no items are there
if($countItems == 0) { if($cart->items()->get()->count() == 0) {
$result = $this->cart->delete($cart->id); $this->cart->delete($cart->id);
session()->forget('cart'); session()->forget('cart');
} else {
session()->forget('cart');
session()->put('cart', $this->cart->findOneByField('id', $cart->id)); session()->flash('success', trans('shop::app.checkout.cart.quantity.success_remove'));
}
if ($result) {
session()->flash('sucess', trans('shop::app.checkout.cart.quantity.success_remove'));
} else {
session()->flash('error', trans('shop::app.checkout.cart.quantity.error_remove'));
} }
} }
return redirect()->back();
} }
/** /**
@ -676,7 +625,7 @@ class Cart {
if(session()->has('cart')) { if(session()->has('cart')) {
$cart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id); $cart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id);
$guestCart = $this->getCart(); $guestCart = session()->get('cart');
if(!isset($cart)) { if(!isset($cart)) {
$guestCart->update(['customer_id' => auth()->guard('customer')->user()->id, 'is_guest' => 0]); $guestCart->update(['customer_id' => auth()->guard('customer')->user()->id, 'is_guest' => 0]);
@ -777,9 +726,6 @@ class Cart {
//forget the guest cart instance //forget the guest cart instance
session()->forget('cart'); session()->forget('cart');
//put the customer cart instance
session()->put('cart', $cart);
$this->collectTotals(); $this->collectTotals();
return redirect()->back(); return redirect()->back();
@ -788,22 +734,6 @@ class Cart {
} }
} }
/**
* Destroys the session maintained for cart on customer logout.
*
* @return response
*/
public function destroyCart()
{
if(session()->has('cart')) {
session()->forget('cart');
return redirect()->back();
} else {
return redirect()->back();
}
}
/** /**
* Checks if cart has any error * Checks if cart has any error
* *

View File

@ -57,7 +57,7 @@ class CreateCartItemsTable extends Migration
}); });
Schema::table('cart_items', function (Blueprint $table) { Schema::table('cart_items', function (Blueprint $table) {
$table->foreign('parent_id')->references('id')->on('cart_items'); $table->foreign('parent_id')->references('id')->on('cart_items')->onDelete('cascade');
}); });
} }

View File

@ -12,30 +12,14 @@ class CustomerEventsHandler {
public function onCustomerLogin($event) public function onCustomerLogin($event)
{ {
/** /**
* handle the user login * handle the user login event to manage the after login, if the user has added any products as guest then
* event to manage the * the cart items from session will be transferred from cookie to the cart table in the database.
* after login, if
* the user has added any
* products as guest then
* the cart items from session
* will be transferred from
* cookie to the cart table
* in the database.
* *
* Check whether cookie is * Check whether cookie is present or not and then check emptiness and then do the appropriate actions.
* present or not and then
* check emptiness and then
* do the appropriate actions.
*/ */
Cart::mergeCart(); Cart::mergeCart();
} }
//Customer Logout Event Handler.
public function onCustomerLogout($event)
{
Cart::destroyCart();
}
/** /**
* Register the listeners for the subscriber. * Register the listeners for the subscriber.
* *
@ -45,7 +29,5 @@ class CustomerEventsHandler {
public function subscribe($events) public function subscribe($events)
{ {
$events->listen('customer.after.login', 'Webkul\Customer\Http\Listeners\CustomerEventsHandler@onCustomerLogin'); $events->listen('customer.after.login', 'Webkul\Customer\Http\Listeners\CustomerEventsHandler@onCustomerLogin');
$events->listen('customer.after.logout', 'Webkul\Customer\Http\Listeners\CustomerEventsHandler@onCustomerLogout');
} }
} }

View File

@ -0,0 +1,49 @@
<?php
namespace Webkul\Product\Contracts\Criteria;
use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Contracts\RepositoryInterface;
use Webkul\Attribute\Repositories\AttributeRepository;
/**
* Class MyCriteria.
*
* @package namespace App\Criteria;
*/
class FeaturedProductsCriteria implements CriteriaInterface
{
/**
* @var AttributeRepository
*/
protected $attribute;
/**
* @param Webkul\Attribute\Repositories\AttributeRepository $attribute
* @return void
*/
public function __construct(AttributeRepository $attribute)
{
$this->attribute = $attribute;
}
/**
* Apply criteria in query repository
*
* @param string $model
* @param RepositoryInterface $repository
*
* @return mixed
*/
public function apply($model, RepositoryInterface $repository)
{
$attribute = $this->attribute->findOneByField('code', 'featured');
$model = $model->leftJoin('product_attribute_values as filter_featured', 'products.id', '=', 'filter_featured.product_id');
$model->where('filter_featured.boolean_value', 1)
->where('filter_featured.attribute_id', $attribute->id);
return $model;
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace Webkul\Product\Contracts\Criteria;
use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Contracts\RepositoryInterface;
use Webkul\Attribute\Repositories\AttributeRepository;
/**
* Class MyCriteria.
*
* @package namespace App\Criteria;
*/
class NewProductsCriteria implements CriteriaInterface
{
/**
* @var AttributeRepository
*/
protected $attribute;
/**
* @param Webkul\Attribute\Repositories\AttributeRepository $attribute
* @return void
*/
public function __construct(AttributeRepository $attribute)
{
$this->attribute = $attribute;
}
/**
* Apply criteria in query repository
*
* @param string $model
* @param RepositoryInterface $repository
*
* @return mixed
*/
public function apply($model, RepositoryInterface $repository)
{
$attribute = $this->attribute->findOneByField('code', 'new');
$model = $model->leftJoin('product_attribute_values as filter_new', 'products.id', '=', 'filter_new.product_id');
$model->where('filter_new.boolean_value', 1)
->where('filter_new.attribute_id', $attribute->id);
return $model;
}
}

View File

@ -185,27 +185,7 @@ class Product extends Model
$attributeModel = $this->attribute_family->custom_attributes()->where('attributes.code', $key)->first(); $attributeModel = $this->attribute_family->custom_attributes()->where('attributes.code', $key)->first();
if($attributeModel) { $this->attributes[$key] = $this->getCustomAttributeValue($attributeModel);
$channel = request()->get('channel') ?: core()->getDefaultChannelCode();
$locale = request()->get('locale') ?: app()->getLocale();
if($attributeModel->value_per_channel) {
if($attributeModel->value_per_locale) {
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('locale', $locale)->where('attribute_id', $attributeModel->id)->first();
} else {
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('attribute_id', $attributeModel->id)->first();
}
} else {
if($attributeModel->value_per_locale) {
$attributeValue = $this->attribute_values()->where('locale', $locale)->where('attribute_id', $attributeModel->id)->first();
} else {
$attributeValue = $this->attribute_values()->where('attribute_id', $attributeModel->id)->first();
}
}
$this->attributes[$key] = $attributeValue[ProductAttributeValue::$attributeTypeFields[$attributeModel->type]];
}
return $this->getAttributeValue($key); return $this->getAttributeValue($key);
} }
@ -225,36 +205,49 @@ class Product extends Model
$hiddenAttributes = $this->getHidden(); $hiddenAttributes = $this->getHidden();
if(isset($this->id)) { if(isset($this->id)) {
$channel = request()->get('channel') ?: core()->getDefaultChannelCode();
$locale = request()->get('locale') ?: app()->getLocale();
foreach ($this->attribute_family->custom_attributes as $attribute) { foreach ($this->attribute_family->custom_attributes as $attribute) {
if (in_array($attribute->code, $hiddenAttributes)) { if (in_array($attribute->code, $hiddenAttributes)) {
continue; continue;
} }
if($attribute->value_per_channel) { $attributes[$attribute->code] = $this->getCustomAttributeValue($attribute);
if($attribute->value_per_locale) {
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('locale', $locale)->where('attribute_id', $attribute->id)->first();
} else {
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('attribute_id', $attribute->id)->first();
}
} else {
if($attribute->value_per_locale) {
$attributeValue = $this->attribute_values()->where('locale', $locale)->where('attribute_id', $attribute->id)->first();
} else {
$attributeValue = $this->attribute_values()->where('attribute_id', $attribute->id)->first();
}
}
$attributes[$attribute->code] = $attributeValue[ProductAttributeValue::$attributeTypeFields[$attribute->type]];
} }
} }
return $attributes; return $attributes;
} }
/**
* Get an product attribute value.
*
* @return mixed
*/
public function getCustomAttributeValue($attribute)
{
if(!$attribute)
return;
$channel = request()->get('channel') ?: core()->getDefaultChannelCode();
$locale = request()->get('locale') ?: app()->getLocale();
if($attribute->value_per_channel) {
if($attribute->value_per_locale) {
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('locale', $locale)->where('attribute_id', $attribute->id)->first();
} else {
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('attribute_id', $attribute->id)->first();
}
} else {
if($attribute->value_per_locale) {
$attributeValue = $this->attribute_values()->where('locale', $locale)->where('attribute_id', $attribute->id)->first();
} else {
$attributeValue = $this->attribute_values()->where('attribute_id', $attribute->id)->first();
}
}
return $attributeValue[ProductAttributeValue::$attributeTypeFields[$attribute->type]];
}
/** /**
* Overrides the default Eloquent query builder * Overrides the default Eloquent query builder
* *

View File

@ -14,6 +14,8 @@ use Webkul\Product\Contracts\Criteria\SortCriteria;
use Webkul\Product\Contracts\Criteria\AttributeToSelectCriteria; use Webkul\Product\Contracts\Criteria\AttributeToSelectCriteria;
use Webkul\Product\Contracts\Criteria\FilterByAttributesCriteria; use Webkul\Product\Contracts\Criteria\FilterByAttributesCriteria;
use Webkul\Product\Contracts\Criteria\FilterByCategoryCriteria; use Webkul\Product\Contracts\Criteria\FilterByCategoryCriteria;
use Webkul\Product\Contracts\Criteria\NewProductsCriteria;
use Webkul\Product\Contracts\Criteria\FeaturedProductsCriteria;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
/** /**
@ -425,12 +427,13 @@ class ProductRepository extends Repository
} }
/** /**
* Return newly added product * Returns newly added product
* *
* @return Collection * @return Collection
*/ */
public function getNewProducts() public function getNewProducts()
{ {
$this->pushCriteria(app(NewProductsCriteria::class));
$this->pushCriteria(app(AttributeToSelectCriteria::class)->addAttribueToSelect([ $this->pushCriteria(app(AttributeToSelectCriteria::class)->addAttribueToSelect([
'name', 'name',
'description', 'description',
@ -443,7 +446,32 @@ class ProductRepository extends Repository
$params = request()->input(); $params = request()->input();
return $this->scopeQuery(function($query){ return $this->scopeQuery(function($query) {
return $query->distinct()->addSelect('products.*')->orderBy('id', 'desc');
})->paginate(4, ['products.id']);
}
/**
* Returns featured product
*
* @return Collection
*/
public function getFeaturedProducts()
{
$this->pushCriteria(app(FeaturedProductsCriteria::class));
$this->pushCriteria(app(AttributeToSelectCriteria::class)->addAttribueToSelect([
'name',
'description',
'short_description',
'price',
'special_price',
'special_price_from',
'special_price_to'
]));
$params = request()->input();
return $this->scopeQuery(function($query) {
return $query->distinct()->addSelect('products.*')->orderBy('id', 'desc'); return $query->distinct()->addSelect('products.*')->orderBy('id', 'desc');
})->paginate(4, ['products.id']); })->paginate(4, ['products.id']);
} }

View File

@ -13,10 +13,8 @@ use Webkul\Product\Product\View as ProductView;
use Cart; use Cart;
/** /**
* Cart controller for the customer * Cart controller for the customer and guest users for adding and
* and guest users for adding and * removing the products in the cart.
* removing the products in the
* cart.
* *
* @author Prashant Singh <prashant.singh852@webkul.com> * @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
@ -25,9 +23,7 @@ class CartController extends Controller
{ {
/** /**
* Protected Variables that * Protected Variables that holds instances of the repository classes.
* holds instances of the
* repository classes.
* *
* @param Array $_config * @param Array $_config
* @param $cart * @param $cart
@ -85,9 +81,7 @@ class CartController extends Controller
} }
/** /**
* Function for guests * Function for guests user to add the product in the cart.
* user to add the product
* in the cart.
* *
* @return Mixed * @return Mixed
*/ */
@ -101,8 +95,7 @@ class CartController extends Controller
} }
/** /**
* Removes the item from * Removes the item from the cart if it exists
* the cart if it exists
* *
* @param integer $itemId * @param integer $itemId
*/ */
@ -113,8 +106,7 @@ class CartController extends Controller
} }
/** /**
* Updates the quantity of the * Updates the quantity of the items present in the cart.
* items present in the cart.
* *
* @return response * @return response
*/ */

View File

@ -0,0 +1,47 @@
<?php
namespace Webkul\Shop\Http\ViewComposers;
use Illuminate\View\View;
use Illuminate\Support\Collection;
use Webkul\Product\Repositories\ProductRepository as Product;
/**
* Featured Products page
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class FeaturedProductListComposer
{
/**
* ProductRepository object
*
* @var array
*/
protected $product;
/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function __construct(Product $product)
{
$this->product = $product;
}
/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$products = $this->product->getFeaturedProducts();
$view->with('products', $products);
}
}

View File

@ -40,7 +40,6 @@ class NewProductListComposer
*/ */
public function compose(View $view) public function compose(View $view)
{ {
// dd($mytime = \Carbon\Carbon::now());
$products = $this->product->getNewProducts(); $products = $this->product->getNewProducts();
$view->with('products', $products); $view->with('products', $products);

View File

@ -26,5 +26,10 @@ class ComposerServiceProvider extends ServiceProvider
['shop::home.new-products'], ['shop::home.new-products'],
'Webkul\Shop\Http\ViewComposers\NewProductListComposer' 'Webkul\Shop\Http\ViewComposers\NewProductListComposer'
); );
View::composer(
['shop::home.featured-products'],
'Webkul\Shop\Http\ViewComposers\FeaturedProductListComposer'
);
} }
} }

View File

@ -1,6 +1,10 @@
<?php <?php
return [ return [
'home' => [
'featured-products' => 'Featured Products',
'new-products' => 'New Products'
],
'customer' => [ 'customer' => [
'signup-text' => [ 'signup-text' => [
'account_exists' => 'Already have an account', 'account_exists' => 'Already have an account',

View File

@ -1,31 +1,21 @@
<section class="featured-products"> @if($products->count())
<div class="featured-heading"> <section class="featured-products">
Featured Products<br/>
<span class="featured-seperator" style="color:lightgrey;">_____</span>
</div>
<div class="featured-grid product-grid-4"> <div class="featured-heading">
@for($i=0; $i<4; $i++) {{ __('shop::app.home.featured-products') }}<br/>
<div class="product-card">
<div class="product-image"> <span class="featured-seperator" style="color:lightgrey;">_____</span>
<img src="vendor/webkul/shop/assets/images/grid.png" />
</div>
<div class="product-name">
<span>Red Black Tees</span>
</div>
<div class="product-price">
<span>$65.00</span>
</div>
<div class="product-ratings mb-10">
<span>
<img src="vendor/webkul/shop/assets/images/5star.svg" />
</span>
</div>
<div class="cart-fav-seg">
<button class="btn btn-md btn-primary addtocart">Add to Cart</button>
<span><img src="vendor/webkul/shop/assets/images/wishadd.svg" /></span>
</div>
</div> </div>
@endfor
</div> <div class="featured-grid product-grid-4">
</section>
@foreach ($products as $product)
@include ('shop::products.list.card', ['product' => $product])
@endforeach
</div>
</section>
@endif

View File

@ -1,93 +1,21 @@
<section class="featured-products"> @if($products->count())
<section class="featured-products">
<div class="featured-heading"> <div class="featured-heading">
New Products<br/> {{ __('shop::app.home.new-products') }}<br/>
<span class="featured-seperator" style="color:lightgrey;">_____</span>
</div>
<div class="product-grid-4"> <span class="featured-seperator" style="color:lightgrey;">_____</span>
<div class="product-card">
<div class="product-image">
<img src="vendor/webkul/shop/assets/images/new.png" />
</div>
<div class="product-name">
<span>Red Black Tees</span>
</div>
<div class="product-price">
<span>$65.00</span>
</div>
<div class="product-ratings mb-10">
<span>
<img src="vendor/webkul/shop/assets/images/5star.svg" />
</span>
</div>
<div class="cart-fav-seg">
<button class="btn btn-md btn-primary addtocart">Add to Cart</button>
<span><img src="vendor/webkul/shop/assets/images/wishadd.svg" /></span>
</div>
</div> </div>
<div class="product-card"> <div class="product-grid-4">
<div class="product-image">
<img src="vendor/webkul/shop/assets/images/new.png" /> @foreach ($products as $product)
</div>
<div class="product-name">
<span>Red Black Tees</span>
</div>
<div class="product-price">
<span>$65.00</span>
</div>
<div class="product-ratings mb-10">
<span>
<img src="vendor/webkul/shop/assets/images/5star.svg" />
</span>
</div>
<div class="cart-fav-seg">
<button class="btn btn-md btn-primary addtocart">Add to Cart</button>
<span><img src="vendor/webkul/shop/assets/images/wishadd.svg" /></span>
</div>
</div>
<div class="product-card"> @include ('shop::products.list.card', ['product' => $product])
<div class="product-image">
<img src="vendor/webkul/shop/assets/images/new.png" /> @endforeach
</div>
<div class="product-name">
<span>Red Black Tees</span>
</div>
<div class="product-price">
<span>$65.00</span>
</div>
<div class="product-ratings mb-10">
<span>
<img src="vendor/webkul/shop/assets/images/5star.svg" />
</span>
</div>
<div class="cart-fav-seg">
<button class="btn btn-md btn-primary addtocart">Add to Cart</button>
<span><img src="vendor/webkul/shop/assets/images/wishadd.svg" /></span>
</div>
</div>
<div class="product-card">
<div class="product-image">
<img src="vendor/webkul/shop/assets/images/new.png" />
</div>
<div class="product-name">
<span>Red Black Tees</span>
</div>
<div class="product-price">
<span>$65.00</span>
</div>
<div class="product-ratings mb-10">
<span>
<img src="vendor/webkul/shop/assets/images/5star.svg" />
</span>
</div>
<div class="cart-fav-seg">
<button class="btn btn-md btn-primary addtocart">Add to Cart</button>
<span><img src="vendor/webkul/shop/assets/images/wishadd.svg" /></span>
</div>
</div> </div>
</div>
</section> </section>
@endif

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
{ {
"/js/shop.js": "/js/shop.js", "/js/shop.js": "/js/shop.js",
"/css/shop.css": "/css/shop.css" "/css/shop.css": "/css/shop.css"
} }