Cart fixes

This commit is contained in:
jitendra 2018-09-26 09:51:14 +05:30
parent 4d0bc20820
commit e2bac8a34e
22 changed files with 546 additions and 154 deletions

View File

@ -9,6 +9,14 @@ return [
'default_rate' => '10',
'type' => 'per_unit',
'class' => 'Webkul\Shipping\Carriers\FlatRate',
],
'free' => [
'code' => 'free',
'title' => 'Free Shipping',
'description' => 'This is a free shipping',
'active' => true,
'class' => 'Webkul\Shipping\Carriers\Free',
]
];

View File

@ -3,59 +3,94 @@
namespace Webkul\Cart;
use Carbon\Carbon;
use Webkul\Cart\Repositories\CartRepository;
use Webkul\Cart\Repositories\CartItemRepository;
use Webkul\Cart\Repositories\CartAddressRepository;
use Webkul\Customer\Repositories\CustomerRepository;
use Webkul\Product\Repositories\ProductRepository;
use Cookie;
/**
* Facade for all
* the methods to be
* implemented in Cart.
* Facade for all the methods to be implemented in Cart.
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class Cart {
protected $cart; //cart repository instance
/**
* CartRepository model
*
* @var mixed
*/
protected $cart;
protected $cartItem; //cart_item repository instance
/**
* CartItemRepository model
*
* @var mixed
*/
protected $cartItem;
protected $customer; //customer repository instance
/**
* CustomerRepository model
*
* @var mixed
*/
protected $customer;
protected $product; //product repository instance
/**
* CartAddressRepository model
*
* @var mixed
*/
protected $cartAddress;
public function __construct(CartRepository $cart,
CartItemRepository $cartItem,
CustomerRepository $customer,
ProductRepository $product) {
/**
* ProductRepository model
*
* @var mixed
*/
protected $product;
/**
* Create a new controller instance.
*
* @param Webkul\Cart\Repositories\CartRepository $cart
* @param Webkul\Cart\Repositories\CartItemRepository $cartItem
* @param Webkul\Cart\Repositories\CartAddressRepository $cartAddress
* @param Webkul\Customer\Repositories\CustomerRepository $customer
* @param Webkul\Product\Repositories\ProductRepository $product
* @return void
*/
public function __construct(
CartRepository $cart,
CartItemRepository $cartItem,
CartAddressRepository $cartAddress,
CustomerRepository $customer,
ProductRepository $product)
{
$this->customer = $customer;
$this->cart = $cart;
$this->cartItem = $cartItem;
$this->cartAddress = $cartAddress;
$this->product = $product;
}
/**
* Create new cart
* instance with the
* current item added.
* Create new cart instance with the current item added.
*
* @param Integer $id
* @param Mixed $data
* @param integer $id
* @param array $data
*
* @return Mixed
* @return Response
*/
public function createNewCart($id, $data) {
public function createNewCart($id, $data)
{
$cartData['channel_id'] = core()->getCurrentChannel()->id;
if(auth()->guard('customer')->check()) {
@ -84,23 +119,22 @@ class Cart {
return redirect()->back();
}
}
session()->flash('error', 'Some error occured');
return redirect()->back();
}
/**
* 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 $data
*
* @return Mixed
* @return void
*/
public function add($id, $data) {
public function add($id, $data)
{
// session()->forget('cart');
// return redirect()->back();
@ -153,26 +187,24 @@ class Cart {
}
/**
* use detach to remove the
* current product from cart tables
* Use detach to remove the current product from cart tables
*
* @param Integer $id
* @return Mixed
*/
public function remove($id) {
public function remove($id)
{
dd("Removing Item from Cart");
}
/**
* This function handles
* when guest has some of
* cart products and then
* logs in.
* This function handles when guest has some of cart products and then logs in.
*
* @return Redirect
* @return Response
*/
public function mergeCart() {
public function mergeCart()
{
if(session()->has('cart')) {
$cart = session()->get('cart');
@ -222,4 +254,80 @@ class Cart {
return redirect()->back();
}
}
/**
* Returns cart
*
* @return Mixed
*/
public function getCart()
{
if(!$cart = session()->get('cart'))
return false;
return $cart;
}
/**
* Save customer address
*
* @return Mixed
*/
public function saveCustomerAddress($data)
{
if(!$cart = $this->getCart())
return false;
$billingAddress = $data['billing'];
$shippingAddress = $data['shipping'];
$billingAddress['cart_id'] = $shippingAddress['cart_id'] = $cart->id;
if($billingAddressModel = $cart->biling_address) {
$this->cartAddress->update($billingAddress, $billingAddressModel->id);
if($shippingAddress = $cart->shipping_address) {
if(isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
$this->cartAddress->update($billingAddress, $shippingAddress->id);
} else {
$this->cartAddress->update($shippingAddress, $shippingAddress->id);
}
} else {
if(isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
$this->cartAddress->create(array_merge($billingAddress, ['address_type' => 'shipping']));
} else {
$this->cartAddress->create(array_merge($shippingAddress, ['address_type' => 'shipping']));
}
}
} else {
$this->cartAddress->create(array_merge($billingAddress, ['address_type' => 'billing']));
if(isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
$this->cartAddress->create(array_merge($billingAddress, ['address_type' => 'shipping']));
} else {
$this->cartAddress->create(array_merge($shippingAddress, ['address_type' => 'shipping']));
}
}
return true;
}
/**
* Save shipping method for cart
*
* @param string $shippingMethodCode
* @return Mixed
*/
public function saveShippingMethod($shippingMethodCode)
{
if(!$cart = $this->getCart())
return false;
foreach($cart->shipping_rates as $rate) {
if($rate->method != $shippingMethodCode) {
$rate->delete();
}
}
return true;
}
}

View File

@ -15,6 +15,9 @@ class CreateCartAddress extends Migration
{
Schema::create('cart_address', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email');
$table->string('address1');
$table->string('address2')->nullable();
$table->string('country');

View File

@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCartShipping extends Migration
class CreateCartShippingRatesTable extends Migration
{
/**
* Run the migrations.
@ -13,7 +13,7 @@ class CreateCartShipping extends Migration
*/
public function up()
{
Schema::create('cart_shipping', function (Blueprint $table) {
Schema::create('cart_shipping_rates', function (Blueprint $table) {
$table->increments('id');
$table->string('carrier');
$table->string('carrier_title');
@ -21,8 +21,6 @@ class CreateCartShipping extends Migration
$table->string('method_title');
$table->string('method_description')->nullable();
$table->double('price')->nullable();
$table->integer('cart_id')->nullable()->unsigned();
$table->foreign('cart_id')->references('id')->on('cart');
$table->integer('cart_address_id')->nullable()->unsigned();
$table->foreign('cart_address_id')->references('id')->on('cart_address');
$table->timestamps();
@ -36,6 +34,6 @@ class CreateCartShipping extends Migration
*/
public function down()
{
Schema::dropIfExists('cart_shipping');
Schema::dropIfExists('cart_shipping_rates');
}
}

View File

@ -4,17 +4,12 @@ namespace Webkul\Cart\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Webkul\Cart\Repositories\CartRepository;
use Webkul\Cart\Repositories\CartItemRepository;
use Webkul\Product\Repositories\ProductRepository;
use Webkul\Customer\Repositories\CustomerRepository;
use Webkul\Product\Product\ProductImage;
use Webkul\Product\Product\View as ProductView;
use Cart;
use Cookie;
@ -47,9 +42,16 @@ class CartController extends Controller
protected $productView;
public function __construct(CartRepository $cart, CartItemRepository $cartItem, CustomerRepository $customer, ProductRepository $product, ProductImage $productImage, ProductView $productView) {
public function __construct(
CartRepository $cart,
CartItemRepository $cartItem,
CustomerRepository $customer,
ProductRepository $product,
ProductImage $productImage,
ProductView $productView
) {
$this->middleware('customer')->except(['add', 'remove', 'test']);
// $this->middleware('customer')->except(['add', 'remove', 'test']);
$this->customer = $customer;
@ -66,6 +68,19 @@ class CartController extends Controller
$this->_config = request('_config');
}
/**
* Method to populate
* the cart page which
* will be populated
* before the checkout
* process.
*
* @return Mixed
*/
public function index() {
return view($this->_config['view'])->with('cart', Cart::getCart());
}
/**
* Function for guests
* user to add the product
@ -107,67 +122,6 @@ class CartController extends Controller
return redirect()->back();
}
/**
* Method to populate
* the cart page which
* will be populated
* before the checkout
* process.
*
* @return Mixed
*/
public function beforeCheckout() {
if(auth()->guard('customer')->check()) {
$cart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id);
if(isset($cart)) {
$cart = $this->cart->findOneByField('id', 144);
$cartItems = $this->cart->items($cart['id']);
$products = array();
foreach($cartItems as $cartItem) {
$image = $this->productImage->getGalleryImages($cartItem->product);
if(isset($image[0]['small_image_url'])) {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, $image[0]['small_image_url'], $cartItem->quantity];
}
else {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, 'null', $cartItem->quantity];
}
}
}
} else {
if(session()->has('cart')) {
$cart = session()->get('cart');
if(isset($cart)) {
$cart = $this->cart->findOneByField('id', 144);
$cartItems = $this->cart->items($cart['id']);
$products = array();
foreach($cartItems as $cartItem) {
$image = $this->productImage->getGalleryImages($cartItem->product);
if(isset($image[0]['small_image_url'])) {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, $image[0]['small_image_url'], $cartItem->quantity];
}
else {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, 'null', $cartItem->quantity];
}
}
}
}
}
return view($this->_config['view'])->with('products', $products);
}
/**
* This method will return
* the quantities from

View File

@ -43,7 +43,10 @@ class CheckoutController extends Controller
*/
public function index()
{
return view($this->_config['view']);
if(!$cart = Cart::getCart())
return redirect()->route('shop.checkout.cart.index');
return view($this->_config['view'])->with('cart', $cart);
}
/**
@ -54,11 +57,10 @@ class CheckoutController extends Controller
*/
public function saveAddress(CustomerAddressForm $request)
{
if(!Cart::saveCustomerAddress(request()->all())) {
// return response()->json(['redirect_url' => route('store.home')], 403)
}
if(!Cart::saveCustomerAddress(request()->all()) || !$rates = Shipping::collectRates())
return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403);
return response()->json(Shipping::collectRates());
return response()->json($rates);
}
/**

View File

@ -70,13 +70,9 @@ class CartComposer
$view->with('cart', $products);
}
} else {
if(session()->has('cart')) {
$cart = session()->get('cart');
if($cart = session()->get('cart')) {
if(isset($cart)) {
$cart = $this->cart->findOneByField('id', 144);
$cartItems = $this->cart->items($cart['id']);
$cartItems = $cart->items($cart['id']);
$products = array();

View File

@ -5,7 +5,7 @@ namespace Webkul\Cart\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Product\Models\Product;
use Webkul\Cart\Models\CartAddress;
use Webkul\Cart\Models\CartShipping;
use Webkul\Cart\Models\CartShippingRate;
class Cart extends Model
{
@ -28,10 +28,42 @@ class Cart extends Model
}
/**
* Get the shipping for the cart.
* Get the biling address for the cart.
*/
public function shipping()
public function biling_address()
{
return $this->hasMany(CartShipping::class);
return $this->addresses()->where('address_type', 'billing');
}
}
/**
* Get all of the attributes for the attribute groups.
*/
public function getBilingAddressAttribute()
{
return $this->biling_address()->first();
}
/**
* Get the shipping address for the cart.
*/
public function shipping_address()
{
return $this->addresses()->where('address_type', 'shipping');
}
/**
* Get all of the attributes for the attribute groups.
*/
public function getShippingAddressAttribute()
{
return $this->shipping_address()->first();
}
/**
* Get the shipping rates for the cart.
*/
public function shipping_rates()
{
return $this->hasManyThrough(CartShippingRate::class, CartAddress::class, 'cart_id', 'cart_address_id');
}
}

View File

@ -3,8 +3,19 @@
namespace Webkul\Cart\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Cart\Models\CartShippingRate;
class CartAddress extends Model
{
}
protected $table = 'cart_address';
protected $fillable = ['first_name', 'last_name', 'email', 'address1', 'address2', 'city', 'state', 'postcode', 'country', 'email', 'phone', 'address_type', 'cart_id'];
/**
* Get the shipping rates for the cart address.
*/
public function shipping_rates()
{
return $this->hasMany(CartShippingRate::class);
}
}

View File

@ -1,10 +0,0 @@
<?php
namespace Webkul\Cart\Models;
use Illuminate\Database\Eloquent\Model;
class CartShipping extends Model
{
}

View File

@ -0,0 +1,17 @@
<?php
namespace Webkul\Cart\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Cart\Models\CartAddress;
class CartShippingRate extends Model
{
/**
* Get the post that owns the comment.
*/
public function shipping_address()
{
return $this->belongsTo(CartAddress::class);
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace Webkul\Cart\Repositories;
use Webkul\Core\Eloquent\Repository;
/**
* Cart Address Reposotory
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CartAddressRepository extends Repository
{
/**
* Specify Model class name
*
* @return Mixed
*/
function model()
{
return 'Webkul\Cart\Models\CartAddress';
}
}

View File

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

View File

@ -3,7 +3,7 @@
namespace Webkul\Shipping\Carriers;
use Config;
use Webkul\Cart\Models\CartShipping;
use Webkul\Cart\Models\CartShippingRate;
use Webkul\Shipping\Facades\Shipping;
/**
@ -19,12 +19,17 @@ class FlatRate extends AbstractShipping
*/
protected $code = 'flatrate';
/**
* Returns rate for flatrate
*
* @return array
*/
public function calculate()
{
if(!$this->isAvailable())
return false;
$object = new CartShipping;
$object = new CartShippingRate;
$object->carrier = 'flatrate';
$object->carrier_title = $this->getConfigData('title');

View File

@ -0,0 +1,43 @@
<?php
namespace Webkul\Shipping\Carriers;
use Config;
use Webkul\Cart\Models\CartShippingRate;
use Webkul\Shipping\Facades\Shipping;
/**
* Class Rate.
*
*/
class Free extends AbstractShipping
{
/**
* Payment method code
*
* @var string
*/
protected $code = 'free';
/**
* Returns rate for flatrate
*
* @return array
*/
public function calculate()
{
if(!$this->isAvailable())
return false;
$object = new CartShippingRate;
$object->carrier = 'free';
$object->carrier_title = $this->getConfigData('title');
$object->method = 'free_free';
$object->method_title = $this->getConfigData('title');
$object->method_description = $this->getConfigData('description');
$object->price = 0;
return $object;
}
}

View File

@ -3,6 +3,7 @@
namespace Webkul\Shipping;
use Illuminate\Support\Facades\Config;
use Webkul\Cart\Facades\Cart;
/**
* Class Shipping.
@ -10,10 +11,25 @@ use Illuminate\Support\Facades\Config;
*/
class Shipping
{
/**
* Rates
*
* @var array
*/
protected $rates = [];
/**
* Collects rate from available shipping methods
*
* @return array
*/
public function collectRates()
{
if(!$cart = Cart::getCart())
return false;
$this->removeAllShippingRates();
foreach(Config::get('carriers') as $shippingMethod) {
$object = new $shippingMethod['class'];
@ -26,12 +42,53 @@ class Shipping
}
}
$this->saveAllShippingRates();
return [
'jump_to_section' => 'shipping',
'html' => view('shop::checkout.onepage.shipping', ['shippingRateGroups' => $this->getGroupedAllShippingRates()])->render()
];
}
/**
* Persist shipping rate to database
*
* @return void
*/
public function removeAllShippingRates()
{
if(!$cart = Cart::getCart())
return;
foreach($cart->shipping_rates()->get() as $rate) {
$rate->delete();
}
}
/**
* Persist shipping rate to database
*
* @return void
*/
public function saveAllShippingRates()
{
if(!$cart = Cart::getCart())
return;
$shippingAddress = $cart->shipping_address;
foreach($this->rates as $rate) {
$rate->cart_address_id = $shippingAddress->id;
$rate->save();
}
}
/**
* Returns shipping rates, grouped by shipping method
*
* @return void
*/
public function getGroupedAllShippingRates()
{
$rates = [];

View File

@ -10,16 +10,16 @@ Route::group(['middleware' => ['web']], function () {
'view' => 'shop::products.index'
]);
Route::get('/checkout', 'Webkul\Cart\Http\Controllers\CheckoutController@index')->defaults('_config', [
Route::get('/checkout/cart', 'Webkul\Cart\Http\Controllers\CartController@index')->defaults('_config', [
'view' => 'shop::checkout.cart.index'
])->name('shop.checkout.cart.index');
Route::get('/checkout/onepage', 'Webkul\Cart\Http\Controllers\CheckoutController@index')->defaults('_config', [
'view' => 'shop::checkout.onepage'
])->name('shop.checkout');
])->name('shop.checkout.onepage.index');
Route::get('test', 'Webkul\Cart\Http\Controllers\CartController@test');
Route::get('cart', 'Webkul\Cart\Http\Controllers\CartController@beforeCheckout')->defaults('_config', [
'view' => 'shop::store.cart.index'
]);
Route::post('/checkout/save-address', 'Webkul\Cart\Http\Controllers\CheckoutController@saveAddress')->name('shop.checkout.save-address');
Route::post('/checkout/save-shipping', 'Webkul\Cart\Http\Controllers\CheckoutController@saveShipping')->name('shop.checkout.save-shipping');

View File

@ -60,7 +60,8 @@ return [
'checkout' => [
'cart' => [
'title' => 'Shopping Cart',
'empty' => 'Shopping Cart Is Empty',
],
'onepage' => [

View File

@ -0,0 +1,141 @@
@extends('shop::layouts.master')
@section('page_title')
{{ __('shop::app.checkout.cart.title') }}
@stop
@section('content-wrapper')
@inject ('productImageHelper', 'Webkul\Product\Product\ProductImage')
<section class="cart">
@if ($cart)
<div class="title">
{{ __('shop::app.checkout.cart.title') }}
</div>
<div class="cart-content">
<div class="left-side">
@foreach($cart->items as $item)
<?php
$product = $item->product;
$productBaseImage = $productImageHelper->getProductBaseImage($product);
?>
<div class="item">
<div style="margin-right: 15px;">
<img class="item-image" src="{{ $productBaseImage['medium_image_url'] }}" />
</div>
<div class="item-details">
<div class="item-title">
{{ $product->name }}
</div>
<div class="price">
<span class="main-price">
{{ $item->price }}
</span>
<span class="real-price">
$25.00
</span>
<span class="discount">
10% Off
</span>
</div>
<div class="summary" >
Color : Gray, Size : S
</div>
<div class="misc">
<div class="qty-text">Quantity</div>
<div class="box">{{ $item->quantity }}</div>
<span class="remove">Remove</span>
<span class="towishlist">Move to Wishlist</span>
</div>
</div>
</div>
@endforeach
<div class="misc-controls">
<span>Continue Shopping</span>
<button class="btn btn-lg btn-primary">PROCEED TO CHECKOUT</button>
</div>
</div>
<div class="right-side">
<div class="price-section">
<div class="title">
Price Detail
</div>
<div class="all-item-details">
@foreach($cart->items as $item)
<div class="item-details">
<span class="name">{{ $item->product->name }}</span>
<span class="price">$ {{ $item->price }}</span>
</div>
@endforeach
</div>
<div class="horizontal-rule"></div>
<div class="total-details">
<span class="name">Amount Payable</span>
<span class="amount">$75.00</span>
</div>
</div>
<div class="coupon-section">
<span class="title">Apply Coupon</span>
<div class="control-group">
<input type="text" class="control coupon-input" placeholder="Coupon Code" />
</div>
<button class="btn btn-md btn-primary">Apply</button>
<div class="coupon-details">
<div class="title">Coupon Used</div>
<div class="coupon">
<span class="name">Coupon 1</span>
<span class="discount">$15</span>
</div>
<div class="coupon">
<span class="name">Coupon 2</span>
<span class="discount">$5</span>
</div>
</div>
<div class="horizontal-rule"></div>
<div class="after-coupon-amount">
<span class="name">Amount Payable</span>
<span class="amount">$75.00</span>
</div>
</div>
</div>
</div>
@else
<div class="title">
{{ __('shop::app.checkout.cart.empty') }}
</div>
@endif
</section>
@endsection

View File

@ -124,7 +124,7 @@
@foreach (country()->all() as $code => $country)
<option value="{{ $country}}">{{ $country }}</option>
<option value="{{ $code }}">{{ $country }}</option>
@endforeach
</select>
@ -264,7 +264,7 @@
@foreach (country()->all() as $code => $country)
<option value="{{ $country}}">{{ $country }}</option>
<option value="{{ $code }}">{{ $country }}</option>
@endforeach
</select>

View File

@ -16,7 +16,7 @@
<input v-validate="'required'" type="radio" id="{{ $rate->method }}" name="shipping_method" value="{{ $rate->method }}" v-model="selected_shipping_method">
<label class="radio-view" for="{{ $rate->method }}"></label>
{{ $rate->method_title }}
<b>{{ $rate->price }}</b>
<b>{{ core()->currency($rate->price) }}</b>
</span>
@endforeach

File diff suppressed because one or more lines are too long