onepage cart api implemented
This commit is contained in:
parent
be26ddf986
commit
4e6c462317
|
|
@ -7,6 +7,8 @@ use Illuminate\Http\Request;
|
|||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Webkul\Checkout\Repositories\CartRepository;
|
||||
use Webkul\Checkout\Repositories\CartItemRepository as CartItem;
|
||||
use Webkul\API\Http\Controllers\Shop\OnePagePresenter as PresenterOne;
|
||||
use Auth;
|
||||
use Cart;
|
||||
|
||||
|
|
@ -19,12 +21,14 @@ use Cart;
|
|||
class CartController extends Controller
|
||||
{
|
||||
protected $customer;
|
||||
|
||||
protected $cart;
|
||||
protected $cartItem;
|
||||
|
||||
public function __construct(CartRepository $cart)
|
||||
public function __construct(CartRepository $cart, CartItem $cartItem)
|
||||
{
|
||||
$this->cart = $cart;
|
||||
|
||||
$this->cartItem = $cartItem;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -35,11 +39,11 @@ class CartController extends Controller
|
|||
public function get() {
|
||||
$cart = Cart::getCart();
|
||||
|
||||
if($cart->count() > 0) {
|
||||
return response()->json(['message' => 'success', 'items' => $cart]);
|
||||
} else {
|
||||
if($cart == null || $cart == 'null') {
|
||||
return response()->json(['message' => 'empty', 'items' => null]);
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'success', 'items' => $cart]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -47,8 +51,7 @@ class CartController extends Controller
|
|||
*
|
||||
* @return Mixed
|
||||
*/
|
||||
public function add($id)
|
||||
{
|
||||
public function add($id) {
|
||||
$result = Cart::add($id, request()->all());
|
||||
|
||||
if($result) {
|
||||
|
|
@ -73,12 +76,30 @@ class CartController extends Controller
|
|||
return response()->json(['message' => $result, 'items' => Cart::getCart()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Before checkout starts or full details on the cart
|
||||
*
|
||||
* @return response json
|
||||
*/
|
||||
public function onePage() {
|
||||
$cart = Cart::getCart();
|
||||
|
||||
if($cart == null || $cart == 'null') {
|
||||
return response()->json(['message' => 'empty', 'items' => null]);
|
||||
}
|
||||
|
||||
$presenter = new PresenterOne();
|
||||
$summary = $presenter->presenter($cart);
|
||||
|
||||
return response()->json(['message' => 'success', 'items' => $cart->items, 'summary' => $summary]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the quantity of the items present in the cart.
|
||||
*
|
||||
* @return response JSON
|
||||
*/
|
||||
public function updateBeforeCheckout() {
|
||||
public function updateOnePage() {
|
||||
$request = request()->except('_token');
|
||||
|
||||
foreach($request['qty'] as $id => $quantity) {
|
||||
|
|
@ -92,7 +113,7 @@ class CartController extends Controller
|
|||
|
||||
$data['quantity'] = $value;
|
||||
|
||||
Cart::updateItem($item->product_id, $data, $key);
|
||||
$result = Cart::updateItem($item->product_id, $data, $key);
|
||||
|
||||
unset($item);
|
||||
unset($data);
|
||||
|
|
@ -100,6 +121,6 @@ class CartController extends Controller
|
|||
|
||||
Cart::collectTotals();
|
||||
|
||||
return response()->json(['message' => 'success']);
|
||||
return response()->json(['message' => 'success', 'items' => Cart::getCart()]);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\API\Http\Controllers\Shop;
|
||||
|
||||
/**
|
||||
* Presenter Class for cart and mini cart(if implemented)
|
||||
*/
|
||||
class OnePagePresenter {
|
||||
|
||||
/**
|
||||
* presenter method will handle the cart and collect all the necessary data to be displayed on the onepage cart
|
||||
*/
|
||||
public function presenter($cart) {
|
||||
$cartSummary['grand_total'] = $cart->grand_total;
|
||||
$cartSummary['tax_total'] = $cart->tax_total;
|
||||
$cartSummary['total_items'] = $cart->items_count;
|
||||
$cartSummary['total_items_qty'] = $cart->items_qty;
|
||||
$cartSummary['total_items_qty'] = $cart->items_qty;
|
||||
|
||||
return $cartSummary;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,5 @@
|
|||
<?php
|
||||
Route::get('login', function() {
|
||||
return response()->json(['message' => 'unauthorized'])->name('login');
|
||||
});
|
||||
|
||||
Route::group(['middleware' => 'api','namespace' => 'Webkul\API\Http\Controllers\Customer', 'prefix' => 'api/customer'], function ($router) {
|
||||
Route::post('/register', 'RegistrationController@create');
|
||||
});
|
||||
|
|
@ -48,6 +46,10 @@ Route::group(['namespace' => 'Webkul\API\Http\Controllers\Shop', 'prefix' => 'ap
|
|||
Route::post('add/{id}', 'CartController@add');
|
||||
//remove item to the cart
|
||||
Route::get('remove/{id}', 'CartController@remove');
|
||||
//get cart and all item for cart checkout
|
||||
Route::get('/onepage', 'CartController@onePage');
|
||||
//update OnePage
|
||||
Route::put('/update/onepage', 'CartController@updateOnePage');
|
||||
});
|
||||
|
||||
Route::group(['namespace' => 'Webkul\API\Http\Controllers\Product', 'prefix' => 'api/product'], function ($router) {
|
||||
|
|
|
|||
|
|
@ -325,7 +325,9 @@ class Cart {
|
|||
|
||||
/**
|
||||
* Update the cartItem on cart checkout page and if already added item is added again
|
||||
*
|
||||
* @param $id product_id of cartItem instance
|
||||
* @param $data new requested quantities by customer
|
||||
* @param $itemId is id from cartItem instance
|
||||
* @return boolean
|
||||
*/
|
||||
public function updateItem($id, $data, $itemId)
|
||||
|
|
@ -371,7 +373,6 @@ class Cart {
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -19,10 +19,16 @@ class Cart extends Model
|
|||
|
||||
protected $with = ['items', 'items.child', 'shipping_address', 'billing_address', 'selected_shipping_rate', 'payment'];
|
||||
|
||||
/**
|
||||
* To get relevant associated items with the cart instance
|
||||
*/
|
||||
public function items() {
|
||||
return $this->hasMany(CartItem::class)->whereNull('parent_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* To get all the associated items with the cart instance even the parent and child items of configurable products
|
||||
*/
|
||||
public function all_items() {
|
||||
return $this->hasMany(CartItem::class);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,8 +86,6 @@ class CartController extends Controller
|
|||
* @return Mixed
|
||||
*/
|
||||
public function add($id) {
|
||||
dd($id, request()->all());
|
||||
|
||||
$result = Cart::add($id, request()->except('_token'));
|
||||
|
||||
if($result) {
|
||||
|
|
|
|||
|
|
@ -65,13 +65,15 @@
|
|||
<span class="remove">
|
||||
<a href="{{ route('shop.checkout.cart.remove', $item->id) }}" onclick="removeLink('Do you really want to do this?')">{{ __('shop::app.checkout.cart.remove-link') }}</a></span>
|
||||
|
||||
<span class="towishlist">
|
||||
@if($item->parent_id != 'null' ||$item->parent_id != null)
|
||||
<a href="{{ route('shop.movetowishlist', $item->id) }}" onclick="removeLink('Do you really want to do this?')">{{ __('shop::app.checkout.cart.move-to-wishlist') }}</a>
|
||||
@else
|
||||
<a href="{{ route('shop.movetowishlist', $item->child->id) }}" onclick="removeLink('{{ __('shop::app.checkout.cart.cart-remove-action') }}')">{{ __('shop::app.checkout.cart.move-to-wishlist') }}</a>
|
||||
@endif
|
||||
</span>
|
||||
@auth('customer')
|
||||
<span class="towishlist">
|
||||
@if($item->parent_id != 'null' ||$item->parent_id != null)
|
||||
<a href="{{ route('shop.movetowishlist', $item->id) }}" onclick="removeLink('Do you really want to do this?')">{{ __('shop::app.checkout.cart.move-to-wishlist') }}</a>
|
||||
@else
|
||||
<a href="{{ route('shop.movetowishlist', $item->child->id) }}" onclick="removeLink('{{ __('shop::app.checkout.cart.cart-remove-action') }}')">{{ __('shop::app.checkout.cart.move-to-wishlist') }}</a>
|
||||
@endif
|
||||
</span>
|
||||
@endauth
|
||||
</div>
|
||||
|
||||
@if (!cart()->isItemHaveQuantity($item))
|
||||
|
|
|
|||
Loading…
Reference in New Issue