Buy Now and Wishlist Complete

This commit is contained in:
prashant-webkul 2018-10-30 16:52:36 +05:30
parent e42feb0fa9
commit a792f4e06b
6 changed files with 117 additions and 31 deletions

View File

@ -181,6 +181,7 @@ class Cart {
* @return void
*/
public function add($id, $data, $prepared = false, $preparedData = []) {
// dd($id, $data, $prepared, $preparedData);
if($prepared == false) {
$itemData = $this->prepareItemData($id, $data);
} else {
@ -192,15 +193,19 @@ class Cart {
}
if($cart = $this->getCart()) {
$product = $this->product->find($id);
if($prepared == true) {
$product = $this->product->find($preparedData['parent']['product_id']);
} else {
$product = $this->product->find($id);
}
$cartItems = $cart->items;
//check the isset conditions as collection empty object will mislead the condition and error handling case.
if(isset($cartItems) && $cartItems->count()) {
//for previously added items
foreach($cartItems as $cartItem) {
if($product->type == "simple") {
if($cartItem->product_id == $id) {
$prevQty = $cartItem->quantity;
$newQty = $data['quantity'];
@ -224,14 +229,9 @@ class Cart {
return true;
}
} else if($product->type == "configurable") {
if($cartItem->type == "configurable") {
if ($cartItem->type == "configurable") {
$temp = $this->cartItem->findOneByField('parent_id', $cartItem->id);
if($prepared == true) {
$data['selected_configurable_option'] = $preparedData['parent']['product_id'];
}
if($temp->product_id == $data['selected_configurable_option']) {
$child = $temp->child;
@ -263,13 +263,29 @@ class Cart {
}
}
//for new items
if($product->type == "configurable") {
$canAdd = $this->canAdd($itemData['child']['product_id'], 1);
if($canAdd == false) {
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
return false;
}
$parent = $cart->items()->create($itemData['parent']);
$itemData['child']['parent_id'] = $parent->id;
$cart->items()->create($itemData['child']);
} else if($product->type != "configurable"){
} else if($product->type != "configurable") {
$canAdd = $this->canAdd($itemData['parent']['product_id'], 1);
if($canAdd == false) {
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
return false;
}
$parent = $cart->items()->create($itemData['parent']);
}
@ -277,6 +293,7 @@ class Cart {
return $cart;
} else {
//rare case of accidents
if(isset($cart)) {
$this->cart->delete($cart->id);
} else {
@ -290,7 +307,6 @@ class Cart {
}
}
} else {
// return $this->createNewCart($id, $data);
if($prepared == false) {
return $this->createNewCart($id, $data);
}
@ -1146,7 +1162,7 @@ class Cart {
$moved = $this->add($product->parent_id, $data, true, $result);
if($moved) {
if(isset($moved)) {
return true;
} else {
return false;
@ -1222,14 +1238,43 @@ class Cart {
* @param instance cartItem $id
*/
public function moveToWishlist($itemId) {
$item = $this->cartItem->findOneByField('id', $itemId);
dd($item->cart);
if(!$item)
return false;
$cart = $this->getCart();
$items = $cart->items;
$wishlist = [];
$wishlist = [
'channel_id' => $cart->channel_id,
'customer_id' => auth()->guard('customer')->user()->id,
];
// $wishlist[
// 'channel_id' =>
// ];
foreach($items as $item) {
if($item->id == $itemId) {
if(is_null($item['parent_id']) && $item['type'] == 'simple') {
$wishlist['product_id'] = $item->product_id;
} else {
$wishlist['product_id'] = $item->child->product_id;
$wishtlist['options'] = $item->addtional;
}
$shouldBe = $this->wishlist->findWhere(['customer_id' => auth()->guard('customer')->user()->id, 'product_id' => $wishlist['product_id']]);
if($shouldBe->isEmpty()) {
$wishlist = $this->wishlist->create($wishlist);
}
$result = $this->cartItem->delete($itemId);
if($result) {
if($cart->items()->count() == 0)
$this->cart->delete($cart->id);
session()->flash('success', 'Item Move To Wishlist Successfully');
return $result;
} else {
return $result;
}
}
}
}
/**
@ -1241,11 +1286,13 @@ class Cart {
$product = $this->product->findOneByField('id', $id);
if($product->type == 'configurable') {
session()->flash('warning', 'Please Select Options Before Buying This Product');
session()->flash('warning', trans('shop::app.buynow.no-options'));
return false;
} else {
$this->moveToCart($id);
$result = $this->moveToCart($id);
return $result;
}
}
}

View File

@ -54,7 +54,7 @@ class CartController extends Controller
ProductView $productView
) {
// $this->middleware('customer')->except(['add', 'remove', 'test']);
$this->middleware('customer')->only(['moveToWishlist']);
$this->customer = $customer;
@ -138,16 +138,20 @@ class CartController extends Controller
*
* @return response
*/
public function addconfigurable($slug) {
public function addConfigurable($slug) {
session()->flash('warning', trans('shop::app.checkout.cart.add-config-warning'));
return redirect()->route('shop.products.index', $slug);
}
public function test($id) {
public function buyNow($id) {
$result = Cart::proceedForBuyNow($id);
Cart::collectTotals();
if(!$result) {
return redirect()->back();
} else {
return redirect()->route('shop.checkout.onepage.index');
}
}
@ -160,6 +164,18 @@ class CartController extends Controller
public function moveToWishlist($id) {
$result = Cart::moveToWishlist($id);
dd($result);
if($result) {
Cart::collectTotals();
session()->flash('success', 'Item Successfully Moved To Wishlist');
return redirect()->back();
} else {
session()->flash('warning', 'Cannot move item to wishlist');
return redirect()->back();
}
}
}

View File

@ -27,7 +27,7 @@ Route::group(['middleware' => ['web', 'theme', 'locale', 'currency']], function
Route::post('checkout/cart/add/{id}', 'Webkul\Shop\Http\Controllers\CartController@add')->name('cart.add');
//Cart Items Add Configurable for more
Route::get('checkout/cart/addconfigurable/{slug}', 'Webkul\Shop\Http\Controllers\CartController@addconfigurable')->name('cart.add.configurable');
Route::get('checkout/cart/addconfigurable/{slug}', 'Webkul\Shop\Http\Controllers\CartController@addConfigurable')->name('cart.add.configurable');
//Cart Items Remove
Route::get('checkout/cart/remove/{id}', 'Webkul\Shop\Http\Controllers\CartController@remove')->name('cart.remove');
@ -65,10 +65,10 @@ Route::group(['middleware' => ['web', 'theme', 'locale', 'currency']], function
])->name('shop.checkout.success');
//Shop buynow button action
Route::get('buynow/{id}', 'Webkul\Shop\Http\Controllers\CartController@test')->name('shop.product.buynow');
Route::get('buynow/{id}', 'Webkul\Shop\Http\Controllers\CartController@buyNow')->name('shop.product.buynow');
//Shop buynow button action
Route::get('move/cart/{id}', 'Webkul\Shop\Http\Controllers\CartController@moveToWishlist')->name('shop.movetowishlist');
Route::get('move/wishlist/{id}', 'Webkul\Shop\Http\Controllers\CartController@moveToWishlist')->name('shop.movetowishlist');
//Show Product Details Page(For individually Viewable Product)
Route::get('/products/{slug}', 'Webkul\Shop\Http\Controllers\ProductController@index')->defaults('_config', [

View File

@ -257,6 +257,11 @@ return [
'remove' => 'Item Successfully Removed From Wishlist'
],
'buynow' => [
'no-options' => 'Please Select Options Before Buying This Product'
],
'checkout' => [
'cart' => [
'integrity' => [

View File

@ -1,3 +1,3 @@
{{-- <a href="{{ route('shop.product.buynow', $product->id)}}" class="btn btn-lg btn-primary buynow" style="text-align: center;" id="buynow-changer">
<a href="{{ route('shop.product.buynow', $product->id)}}" class="btn btn-lg btn-primary buynow" style="text-align: center;" id="buynow-changer">
{{ __('shop::app.products.buy-now') }}
</a> --}}
</a>

View File

@ -110,11 +110,12 @@
this.selectedProductId = attribute.options[attribute.selectedIndex].allowedProducts[0];
}
//wishlist anchor href changer with options
@auth('customer')
var prevLink = $('#wishlist-changer').attr('href');
var wishlistLink = $('#wishlist-changer').attr('href');
if(this.selectedProductId != '') {
var splitted = prevLink.split("/");
var splitted = wishlistLink.split("/");
var lastItem = splitted.pop();
@ -127,6 +128,23 @@
$('#wishlist-changer').attr('href', newWishlistUrl);
}
@endauth
//buy now anchor href changer with options
var buyNowLink = $('#buynow-changer').attr('href');
if(this.selectedProductId != '') {
var splitted = buyNowLink.split("/");
var lastItem = splitted.pop();
lastItem = this.selectedProductId;
var joined = splitted.join('/');
var newBuyNowUrl = joined+'/'+lastItem;
$('#buynow-changer').attr('href', newBuyNowUrl);
}
} else {
attribute.selectedIndex = 0;