Fixed cart issues
This commit is contained in:
parent
503f0e0a5a
commit
6f57a51152
|
|
@ -31,10 +31,10 @@ class AttributeFamilyTableSeeder extends Seeder
|
|||
'code' => 'url_key',
|
||||
'position' => 3
|
||||
], [
|
||||
'code' => 'new_from',
|
||||
'code' => 'new',
|
||||
'position' => 4
|
||||
], [
|
||||
'code' => 'new_to',
|
||||
'code' => 'featured',
|
||||
'position' => 5
|
||||
], [
|
||||
'code' => 'visible_individually',
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@ class AttributeTableSeeder extends Seeder
|
|||
[
|
||||
'code' => 'sku',
|
||||
'admin_name' => 'SKU',
|
||||
// 'en' => [
|
||||
// 'name' => 'SKU'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'SKU'
|
||||
],
|
||||
'type' => 'text',
|
||||
'position' => 1,
|
||||
'is_unique' => 1,
|
||||
|
|
@ -29,9 +29,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'name',
|
||||
'admin_name' => 'Name',
|
||||
// 'en' => [
|
||||
// 'name' => 'Name'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Name'
|
||||
],
|
||||
'type' => 'text',
|
||||
'position' => 2,
|
||||
'is_required' => 1,
|
||||
|
|
@ -43,9 +43,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'url_key',
|
||||
'admin_name' => 'URL Key',
|
||||
// 'en' => [
|
||||
// 'name' => 'URL Key'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'URL Key'
|
||||
],
|
||||
'type' => 'text',
|
||||
'position' => 3,
|
||||
'is_unique' => 1,
|
||||
|
|
@ -56,12 +56,12 @@ class AttributeTableSeeder extends Seeder
|
|||
'is_configurable' => 0,
|
||||
'is_user_defined' => 0
|
||||
], [
|
||||
'code' => 'new_from',
|
||||
'admin_name' => 'New From',
|
||||
// 'en' => [
|
||||
// 'name' => 'New From'
|
||||
// ],
|
||||
'type' => 'datetime',
|
||||
'code' => 'new',
|
||||
'admin_name' => 'New',
|
||||
'en' => [
|
||||
'name' => 'New'
|
||||
],
|
||||
'type' => 'boolean',
|
||||
'position' => 4,
|
||||
'is_required' => 0,
|
||||
'value_per_locale' => 0,
|
||||
|
|
@ -70,12 +70,12 @@ class AttributeTableSeeder extends Seeder
|
|||
'is_configurable' => 0,
|
||||
'is_user_defined' => 0
|
||||
], [
|
||||
'code' => 'new_to',
|
||||
'admin_name' => 'New To',
|
||||
// 'en' => [
|
||||
// 'name' => 'New To'
|
||||
// ],
|
||||
'type' => 'datetime',
|
||||
'code' => 'featured',
|
||||
'admin_name' => 'Featured',
|
||||
'en' => [
|
||||
'name' => 'Featured'
|
||||
],
|
||||
'type' => 'boolean',
|
||||
'position' => 5,
|
||||
'is_required' => 0,
|
||||
'value_per_locale' => 0,
|
||||
|
|
@ -86,9 +86,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'visible_individually',
|
||||
'admin_name' => 'Visible Individually',
|
||||
// 'en' => [
|
||||
// 'name' => 'Visible Individually'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Visible Individually'
|
||||
],
|
||||
'type' => 'boolean',
|
||||
'position' => 6,
|
||||
'is_required' => 1,
|
||||
|
|
@ -100,9 +100,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'status',
|
||||
'admin_name' => 'Status',
|
||||
// 'en' => [
|
||||
// 'name' => 'Status'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Status'
|
||||
],
|
||||
'type' => 'boolean',
|
||||
'position' => 7,
|
||||
'is_required' => 1,
|
||||
|
|
@ -114,9 +114,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'short_description',
|
||||
'admin_name' => 'Short Description',
|
||||
// 'en' => [
|
||||
// 'name' => 'Short Description'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Short Description'
|
||||
],
|
||||
'type' => 'textarea',
|
||||
'position' => 8,
|
||||
'is_required' => 1,
|
||||
|
|
@ -128,9 +128,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'description',
|
||||
'admin_name' => 'Description',
|
||||
// 'en' => [
|
||||
// 'name' => 'Description'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Description'
|
||||
],
|
||||
'type' => 'textarea',
|
||||
'position' => 9,
|
||||
'is_required' => 1,
|
||||
|
|
@ -142,9 +142,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'price',
|
||||
'admin_name' => 'Price',
|
||||
// 'en' => [
|
||||
// 'name' => 'Price'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Price'
|
||||
],
|
||||
'type' => 'price',
|
||||
'position' => 10,
|
||||
'is_required' => 1,
|
||||
|
|
@ -156,9 +156,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'cost',
|
||||
'admin_name' => 'Cost',
|
||||
// 'en' => [
|
||||
// 'name' => 'Cost'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Cost'
|
||||
],
|
||||
'type' => 'price',
|
||||
'position' => 11,
|
||||
'is_required' => 0,
|
||||
|
|
@ -170,9 +170,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'special_price',
|
||||
'admin_name' => 'Special Price',
|
||||
// 'en' => [
|
||||
// 'name' => 'Special Price'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Special Price'
|
||||
],
|
||||
'type' => 'price',
|
||||
'position' => 12,
|
||||
'is_required' => 0,
|
||||
|
|
@ -184,9 +184,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'special_price_from',
|
||||
'admin_name' => 'Special Price From',
|
||||
// 'en' => [
|
||||
// 'name' => 'Special Price From'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Special Price From'
|
||||
],
|
||||
'type' => 'date',
|
||||
'position' => 13,
|
||||
'is_required' => 0,
|
||||
|
|
@ -198,9 +198,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'special_price_to',
|
||||
'admin_name' => 'Special Price To',
|
||||
// 'en' => [
|
||||
// 'name' => 'Special Price To'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Special Price To'
|
||||
],
|
||||
'type' => 'date',
|
||||
'position' => 14,
|
||||
'is_required' => 0,
|
||||
|
|
@ -212,9 +212,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'meta_title',
|
||||
'admin_name' => 'Meta Title',
|
||||
// 'en' => [
|
||||
// 'name' => 'Meta Description'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Meta Description'
|
||||
],
|
||||
'type' => 'textarea',
|
||||
'position' => 15,
|
||||
'is_required' => 0,
|
||||
|
|
@ -226,9 +226,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'meta_keywords',
|
||||
'admin_name' => 'Meta Keywords',
|
||||
// 'en' => [
|
||||
// 'name' => 'Meta Keywords'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Meta Keywords'
|
||||
],
|
||||
'type' => 'textarea',
|
||||
'position' => 16,
|
||||
'is_required' => 0,
|
||||
|
|
@ -240,9 +240,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'meta_description',
|
||||
'admin_name' => 'Meta Description',
|
||||
// 'en' => [
|
||||
// 'name' => 'Meta Description'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Meta Description'
|
||||
],
|
||||
'type' => 'textarea',
|
||||
'position' => 17,
|
||||
'is_required' => 0,
|
||||
|
|
@ -254,9 +254,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'width',
|
||||
'admin_name' => 'Width',
|
||||
// 'en' => [
|
||||
// 'name' => 'Width'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Width'
|
||||
],
|
||||
'type' => 'text',
|
||||
'validation' => 'numeric',
|
||||
'position' => 18,
|
||||
|
|
@ -269,9 +269,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'height',
|
||||
'admin_name' => 'Height',
|
||||
// 'en' => [
|
||||
// 'name' => 'Height'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Height'
|
||||
],
|
||||
'type' => 'text',
|
||||
'validation' => 'numeric',
|
||||
'position' => 19,
|
||||
|
|
@ -284,9 +284,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'depth',
|
||||
'admin_name' => 'Depth',
|
||||
// 'en' => [
|
||||
// 'name' => 'Depth'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Depth'
|
||||
],
|
||||
'type' => 'text',
|
||||
'validation' => 'numeric',
|
||||
'position' => 20,
|
||||
|
|
@ -299,9 +299,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'weight',
|
||||
'admin_name' => 'Weight',
|
||||
// 'en' => [
|
||||
// 'name' => 'Weight'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Weight'
|
||||
],
|
||||
'type' => 'text',
|
||||
'validation' => 'numeric',
|
||||
'position' => 21,
|
||||
|
|
@ -314,9 +314,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'color',
|
||||
'admin_name' => 'Color',
|
||||
// 'en' => [
|
||||
// 'name' => 'Color'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Color'
|
||||
],
|
||||
'type' => 'select',
|
||||
'position' => 22,
|
||||
'is_required' => 0,
|
||||
|
|
@ -356,9 +356,9 @@ class AttributeTableSeeder extends Seeder
|
|||
], [
|
||||
'code' => 'size',
|
||||
'admin_name' => 'Size',
|
||||
// 'en' => [
|
||||
// 'name' => 'Size'
|
||||
// ],
|
||||
'en' => [
|
||||
'name' => 'Size'
|
||||
],
|
||||
'type' => 'select',
|
||||
'position' => 23,
|
||||
'is_required' => 0,
|
||||
|
|
|
|||
|
|
@ -109,25 +109,11 @@ class Cart {
|
|||
}
|
||||
}
|
||||
|
||||
$child = $childData = null;
|
||||
if($product->type == 'configurable') {
|
||||
//Check if the product is salable
|
||||
$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
|
||||
$childData = [
|
||||
'product_id' => $data['selected_configurable_option'],
|
||||
|
|
@ -136,26 +122,24 @@ class Cart {
|
|||
'type' => $child->type,
|
||||
'name' => $child->name
|
||||
];
|
||||
}
|
||||
|
||||
return ['parent' => $parentData, 'child' => $childData];
|
||||
} else {
|
||||
$parentData = [
|
||||
'sku' => $product->sku,
|
||||
'product_id' => $productId,
|
||||
'quantity' => $data['quantity'],
|
||||
'type' => 'simple',
|
||||
'type' => $product->type,
|
||||
'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'],
|
||||
'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' => null];
|
||||
}
|
||||
return ['parent' => $parentData, 'child' => $childData];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -175,63 +159,74 @@ class Cart {
|
|||
//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;
|
||||
|
||||
$cartData['is_guest'] = 0;
|
||||
|
||||
$cartData['customer_first_name'] = auth()->guard('customer')->user()->first_name;
|
||||
|
||||
$cartData['customer_last_name'] = auth()->guard('customer')->user()->last_name;
|
||||
$cartData['customer_email'] = auth()->guard('customer')->user()->email;
|
||||
} else {
|
||||
$cartData['is_guest'] = 1;
|
||||
}
|
||||
|
||||
$cartData['items_count'] = 1;
|
||||
|
||||
$cartData['items_qty'] = $data['quantity'];
|
||||
|
||||
if($cart = $this->cart->create($cartData)) {
|
||||
$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
|
||||
$itemData['parent']['additional'] = json_encode($data);
|
||||
if($parent = $this->cartItem->create($itemData['parent'])) {
|
||||
|
||||
if($parent = $this->cartItem->create($itemData['parent'])) {
|
||||
//child item entry
|
||||
$itemData['child']['parent_id'] = $parent->id;
|
||||
$itemData['child']['cart_id'] = $cart->id;
|
||||
|
||||
if($child = $this->cartItem->create($itemData['child'])) {
|
||||
session()->put('cart', $cart);
|
||||
$this->putCart($cart);
|
||||
|
||||
session()->flash('success', trans('shop::app.checkout.cart.item.success'));
|
||||
|
||||
$this->collectTotals();
|
||||
|
||||
return redirect()->back();
|
||||
return $cart;
|
||||
}
|
||||
}
|
||||
} else if($this->product->find($id)->type != "configurable") {
|
||||
if($result = $this->cartItem->create($itemData['parent'])) {
|
||||
session()->put('cart', $cart);
|
||||
} else if($product->type != "configurable") {
|
||||
if($this->cartItem->create($itemData['parent'])) {
|
||||
$this->putCart($cart);
|
||||
|
||||
session()->flash('success', trans('shop::app.checkout.cart.item.success'));
|
||||
|
||||
$this->collectTotals();
|
||||
|
||||
return redirect()->back();
|
||||
return $cart;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
*
|
||||
* @return Mixed
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCart()
|
||||
{
|
||||
|
|
@ -276,38 +271,26 @@ class Cart {
|
|||
*/
|
||||
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) {
|
||||
session()->flash('warning', trans('shop::app.checkout.cart.quantity.warning'));
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
if($quantity <= $totalQty) {
|
||||
$item = $this->cartItem->findOneByField('id', $itemId);
|
||||
|
||||
if($item->product->haveSufficientQuantity($quantity)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Items in a cart with some cart and item details.
|
||||
*
|
||||
* @param @id
|
||||
* @param $data
|
||||
* @param integer $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
|
|
@ -315,14 +298,14 @@ class Cart {
|
|||
{
|
||||
$itemData = $this->prepareItemData($id, $data);
|
||||
|
||||
if(session()->has('cart')) {
|
||||
$cart = $this->getCart();
|
||||
if($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) {
|
||||
if($this->product->find($id)->type == "simple") {
|
||||
if($product->type == "simple") {
|
||||
|
||||
if($cartItem->product_id == $id) {
|
||||
$prevQty = $cartItem->quantity;
|
||||
|
|
@ -348,7 +331,7 @@ class Cart {
|
|||
|
||||
return redirect()->back();
|
||||
}
|
||||
} else if($this->product->find($id)->type == "configurable") {
|
||||
} else if($product->type == "configurable") {
|
||||
if($cartItem->type == "configurable") {
|
||||
$temp = $this->cartItem->findOneByField('parent_id', $cartItem->id);
|
||||
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']);
|
||||
|
||||
$itemData['child']['parent_id'] = $parent->id;
|
||||
|
||||
// $this->canAddOrUpdate($parent->child->id, $parent->quantity);
|
||||
|
||||
$cart->items()->create($itemData['child']);
|
||||
} else if($this->product->find($id)->type != "configurable"){
|
||||
// $this->canAddOrUpdate($parent->id, $parent->quantity);
|
||||
|
||||
} else if($product->type != "configurable"){
|
||||
$parent = $cart->items()->create($itemData['parent']);
|
||||
}
|
||||
|
||||
|
|
@ -402,16 +381,16 @@ class Cart {
|
|||
|
||||
session()->flash('success', trans('shop::app.checkout.cart.item.success'));
|
||||
|
||||
return redirect()->back();
|
||||
return $cart;
|
||||
} else {
|
||||
if(isset($cart)) {
|
||||
$this->cart->delete($cart->id);
|
||||
} else {
|
||||
$this->createNewCart($id, $data);
|
||||
return $this->createNewCart($id, $data);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->createNewCart($id, $data);
|
||||
return $this->createNewCart($id, $data);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -421,9 +400,7 @@ class Cart {
|
|||
*/
|
||||
public function update($itemIds)
|
||||
{
|
||||
if(session()->has('cart')) {
|
||||
$cart = $this->getCart();
|
||||
|
||||
if($cart = $this->getCart()) {
|
||||
$items = $cart->items;
|
||||
|
||||
foreach($items as $item) {
|
||||
|
|
@ -438,7 +415,7 @@ class Cart {
|
|||
if($canBe == false) {
|
||||
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
||||
|
||||
return redirect()->back();
|
||||
return $cart;
|
||||
}
|
||||
|
||||
$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)
|
||||
{
|
||||
if($cart = $this->getCart()) {
|
||||
$items = $cart->items;
|
||||
|
||||
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();
|
||||
$this->cartItem->delete($itemId);
|
||||
|
||||
//delete the cart instance if no items are there
|
||||
if($countItems == 0) {
|
||||
$result = $this->cart->delete($cart->id);
|
||||
if($cart->items()->get()->count() == 0) {
|
||||
$this->cart->delete($cart->id);
|
||||
|
||||
session()->forget('cart');
|
||||
} else {
|
||||
session()->forget('cart');
|
||||
|
||||
session()->put('cart', $this->cart->findOneByField('id', $cart->id));
|
||||
}
|
||||
|
||||
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'));
|
||||
session()->flash('success', trans('shop::app.checkout.cart.quantity.success_remove'));
|
||||
}
|
||||
}
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -676,7 +625,7 @@ class Cart {
|
|||
if(session()->has('cart')) {
|
||||
$cart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id);
|
||||
|
||||
$guestCart = $this->getCart();
|
||||
$guestCart = session()->get('cart');
|
||||
|
||||
if(!isset($cart)) {
|
||||
$guestCart->update(['customer_id' => auth()->guard('customer')->user()->id, 'is_guest' => 0]);
|
||||
|
|
@ -777,9 +726,6 @@ class Cart {
|
|||
//forget the guest cart instance
|
||||
session()->forget('cart');
|
||||
|
||||
//put the customer cart instance
|
||||
session()->put('cart', $cart);
|
||||
|
||||
$this->collectTotals();
|
||||
|
||||
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
|
||||
*
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ class CreateCartItemsTable extends Migration
|
|||
});
|
||||
|
||||
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');
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,30 +12,14 @@ class CustomerEventsHandler {
|
|||
public function onCustomerLogin($event)
|
||||
{
|
||||
/**
|
||||
* handle the user login
|
||||
* event to manage the
|
||||
* 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.
|
||||
* handle the user login event to manage the 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
|
||||
* present or not and then
|
||||
* check emptiness and then
|
||||
* do the appropriate actions.
|
||||
* Check whether cookie is present or not and then check emptiness and then do the appropriate actions.
|
||||
*/
|
||||
Cart::mergeCart();
|
||||
}
|
||||
|
||||
//Customer Logout Event Handler.
|
||||
public function onCustomerLogout($event)
|
||||
{
|
||||
Cart::destroyCart();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the listeners for the subscriber.
|
||||
*
|
||||
|
|
@ -45,7 +29,5 @@ class CustomerEventsHandler {
|
|||
public function subscribe($events)
|
||||
{
|
||||
$events->listen('customer.after.login', 'Webkul\Customer\Http\Listeners\CustomerEventsHandler@onCustomerLogin');
|
||||
|
||||
$events->listen('customer.after.logout', 'Webkul\Customer\Http\Listeners\CustomerEventsHandler@onCustomerLogout');
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -185,27 +185,7 @@ class Product extends Model
|
|||
|
||||
$attributeModel = $this->attribute_family->custom_attributes()->where('attributes.code', $key)->first();
|
||||
|
||||
if($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]];
|
||||
}
|
||||
$this->attributes[$key] = $this->getCustomAttributeValue($attributeModel);
|
||||
|
||||
return $this->getAttributeValue($key);
|
||||
}
|
||||
|
|
@ -225,15 +205,32 @@ class Product extends Model
|
|||
$hiddenAttributes = $this->getHidden();
|
||||
|
||||
if(isset($this->id)) {
|
||||
$channel = request()->get('channel') ?: core()->getDefaultChannelCode();
|
||||
|
||||
$locale = request()->get('locale') ?: app()->getLocale();
|
||||
|
||||
foreach ($this->attribute_family->custom_attributes as $attribute) {
|
||||
if (in_array($attribute->code, $hiddenAttributes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attributes[$attribute->code] = $this->getCustomAttributeValue($attribute);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
|
@ -248,11 +245,7 @@ class Product extends Model
|
|||
}
|
||||
}
|
||||
|
||||
$attributes[$attribute->code] = $attributeValue[ProductAttributeValue::$attributeTypeFields[$attribute->type]];
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
return $attributeValue[ProductAttributeValue::$attributeTypeFields[$attribute->type]];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ use Webkul\Product\Contracts\Criteria\SortCriteria;
|
|||
use Webkul\Product\Contracts\Criteria\AttributeToSelectCriteria;
|
||||
use Webkul\Product\Contracts\Criteria\FilterByAttributesCriteria;
|
||||
use Webkul\Product\Contracts\Criteria\FilterByCategoryCriteria;
|
||||
use Webkul\Product\Contracts\Criteria\NewProductsCriteria;
|
||||
use Webkul\Product\Contracts\Criteria\FeaturedProductsCriteria;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
/**
|
||||
|
|
@ -425,12 +427,13 @@ class ProductRepository extends Repository
|
|||
}
|
||||
|
||||
/**
|
||||
* Return newly added product
|
||||
* Returns newly added product
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getNewProducts()
|
||||
{
|
||||
$this->pushCriteria(app(NewProductsCriteria::class));
|
||||
$this->pushCriteria(app(AttributeToSelectCriteria::class)->addAttribueToSelect([
|
||||
'name',
|
||||
'description',
|
||||
|
|
@ -443,7 +446,32 @@ class ProductRepository extends Repository
|
|||
|
||||
$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');
|
||||
})->paginate(4, ['products.id']);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,10 +13,8 @@ use Webkul\Product\Product\View as ProductView;
|
|||
use Cart;
|
||||
|
||||
/**
|
||||
* Cart controller for the customer
|
||||
* and guest users for adding and
|
||||
* removing the products in the
|
||||
* cart.
|
||||
* Cart controller for the customer and guest users for adding and
|
||||
* removing the products in the cart.
|
||||
*
|
||||
* @author Prashant Singh <prashant.singh852@webkul.com>
|
||||
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
||||
|
|
@ -25,9 +23,7 @@ class CartController extends Controller
|
|||
{
|
||||
|
||||
/**
|
||||
* Protected Variables that
|
||||
* holds instances of the
|
||||
* repository classes.
|
||||
* Protected Variables that holds instances of the repository classes.
|
||||
*
|
||||
* @param Array $_config
|
||||
* @param $cart
|
||||
|
|
@ -85,9 +81,7 @@ class CartController extends Controller
|
|||
}
|
||||
|
||||
/**
|
||||
* Function for guests
|
||||
* user to add the product
|
||||
* in the cart.
|
||||
* Function for guests user to add the product in the cart.
|
||||
*
|
||||
* @return Mixed
|
||||
*/
|
||||
|
|
@ -101,8 +95,7 @@ class CartController extends Controller
|
|||
}
|
||||
|
||||
/**
|
||||
* Removes the item from
|
||||
* the cart if it exists
|
||||
* Removes the item from the cart if it exists
|
||||
*
|
||||
* @param integer $itemId
|
||||
*/
|
||||
|
|
@ -113,8 +106,7 @@ class CartController extends Controller
|
|||
}
|
||||
|
||||
/**
|
||||
* Updates the quantity of the
|
||||
* items present in the cart.
|
||||
* Updates the quantity of the items present in the cart.
|
||||
*
|
||||
* @return response
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -40,7 +40,6 @@ class NewProductListComposer
|
|||
*/
|
||||
public function compose(View $view)
|
||||
{
|
||||
// dd($mytime = \Carbon\Carbon::now());
|
||||
$products = $this->product->getNewProducts();
|
||||
|
||||
$view->with('products', $products);
|
||||
|
|
|
|||
|
|
@ -26,5 +26,10 @@ class ComposerServiceProvider extends ServiceProvider
|
|||
['shop::home.new-products'],
|
||||
'Webkul\Shop\Http\ViewComposers\NewProductListComposer'
|
||||
);
|
||||
|
||||
View::composer(
|
||||
['shop::home.featured-products'],
|
||||
'Webkul\Shop\Http\ViewComposers\FeaturedProductListComposer'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,10 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'home' => [
|
||||
'featured-products' => 'Featured Products',
|
||||
'new-products' => 'New Products'
|
||||
],
|
||||
'customer' => [
|
||||
'signup-text' => [
|
||||
'account_exists' => 'Already have an account',
|
||||
|
|
|
|||
|
|
@ -1,31 +1,21 @@
|
|||
<section class="featured-products">
|
||||
@if($products->count())
|
||||
<section class="featured-products">
|
||||
|
||||
<div class="featured-heading">
|
||||
Featured Products<br/>
|
||||
{{ __('shop::app.home.featured-products') }}<br/>
|
||||
|
||||
<span class="featured-seperator" style="color:lightgrey;">_____</span>
|
||||
</div>
|
||||
|
||||
<div class="featured-grid product-grid-4">
|
||||
@for($i=0; $i<4; $i++)
|
||||
<div class="product-card">
|
||||
<div class="product-image">
|
||||
<img src="vendor/webkul/shop/assets/images/grid.png" />
|
||||
|
||||
@foreach ($products as $product)
|
||||
|
||||
@include ('shop::products.list.card', ['product' => $product])
|
||||
|
||||
@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>
|
||||
@endfor
|
||||
</div>
|
||||
</section>
|
||||
|
||||
</section>
|
||||
@endif
|
||||
|
|
@ -1,93 +1,21 @@
|
|||
<section class="featured-products">
|
||||
@if($products->count())
|
||||
<section class="featured-products">
|
||||
|
||||
<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">
|
||||
<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>
|
||||
|
||||
@foreach ($products as $product)
|
||||
|
||||
@include ('shop::products.list.card', ['product' => $product])
|
||||
|
||||
@endforeach
|
||||
|
||||
</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 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 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>
|
||||
</section>
|
||||
</section>
|
||||
@endif
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue