bulk add to cart
This commit is contained in:
parent
8e37ebe7d5
commit
1a84a016fa
|
|
@ -60,7 +60,9 @@
|
|||
"bagisto/laravel-tax": "v0.1.0",
|
||||
"bagisto/laravel-api": "v0.1.0",
|
||||
"bagisto/laravel-paypal": "v0.1.0",
|
||||
"bagisto/laravel-discount": "v0.1.0"
|
||||
"bagisto/laravel-discount": "v0.1.0",
|
||||
"bagisto/laravel-customer-document": "v0.1.0",
|
||||
"bagisto/laravel-bulk-add-to-cart": "v0.1.0"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
|
|
@ -89,7 +91,8 @@
|
|||
"Webkul\\Tax\\": "packages/Webkul/Tax/src",
|
||||
"Webkul\\API\\": "packages/Webkul/API",
|
||||
"Webkul\\CustomerDocument\\": "packages/Webkul/CustomerDocument/src",
|
||||
"Webkul\\Discount\\": "packages/Webkul/Discount/src"
|
||||
"Webkul\\Discount\\": "packages/Webkul/Discount/src",
|
||||
"Webkul\\BulkAddToCart\\": "packages/Webkul/BulkAddToCart/src"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
|
|
|
|||
|
|
@ -246,7 +246,8 @@ return [
|
|||
Webkul\Tax\Providers\TaxServiceProvider::class,
|
||||
Webkul\API\Providers\APIServiceProvider::class,
|
||||
Webkul\CustomerDocument\Providers\CustomerDocumentServiceProvider::class,
|
||||
Webkul\Discount\Providers\DiscountServiceProvider::class
|
||||
Webkul\Discount\Providers\DiscountServiceProvider::class,
|
||||
Webkul\BulkAddToCart\Providers\BulkAddToCartServiceProvider::class,
|
||||
],
|
||||
|
||||
/*
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"name": "bagisto/laravel-bulk-add-to-cart",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Rahul Shukla",
|
||||
"email": "rahulshukla517@webkul.com"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Webkul\\BulkAddToCart\\": "src/"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"Webkul\\BulkAddToCart\\BulkAddToCartServiceProvider"
|
||||
],
|
||||
"aliases": {}
|
||||
}
|
||||
},
|
||||
"minimum-stability": "dev"
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
[
|
||||
'key' => 'account.bulk-add-to-cart',
|
||||
'name' => 'bulkaddtocart::app.products.bulk-add-to-cart',
|
||||
'route' =>'cart.bulk-add-to-cart.create',
|
||||
'sort' => 7
|
||||
]
|
||||
];
|
||||
|
||||
?>
|
||||
|
|
@ -0,0 +1,163 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\BulkAddToCart\Http\Controllers;
|
||||
|
||||
use Webkul\Admin\Imports\DataGridImport;
|
||||
use Webkul\Product\Repositories\ProductRepository as Product;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Maatwebsite\Excel\Validators\Failure;
|
||||
use Excel;
|
||||
use Cart;
|
||||
|
||||
/**
|
||||
* BulkAddToCart controlller
|
||||
*
|
||||
* @author Rahul Shukla <rahulshukla.symfony517@webkul.com>
|
||||
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
||||
*/
|
||||
class BulkAddToCartController extends Controller
|
||||
{
|
||||
/**
|
||||
* Contains route related configuration
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_config;
|
||||
|
||||
/**
|
||||
* ProductRepository object
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $product;
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @param \Webkul\Product\Repositories\ProductRepository $product
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Product $product)
|
||||
{
|
||||
$this->product = $product;
|
||||
|
||||
$this->_config = request('_config');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view($this->_config['view']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store()
|
||||
{
|
||||
$valid_extension = ['xlsx', 'csv', 'xls', 'ods'];
|
||||
|
||||
if (! in_array(request()->file('file')->getClientOriginalExtension(), $valid_extension)) {
|
||||
session()->flash('error', trans('bulkaddtocart::app.products.upload-error'));
|
||||
|
||||
return redirect()->back();
|
||||
} else {
|
||||
try {
|
||||
$excelData = (new DataGridImport)->toArray(request()->file('file'));
|
||||
$cart = [];
|
||||
|
||||
foreach ($excelData as $data) {
|
||||
foreach ($data as $column => $uploadData) {
|
||||
$validator = Validator::make($uploadData, [
|
||||
'sku' => 'required',
|
||||
'quantity' => 'required|numeric',
|
||||
]);
|
||||
|
||||
$product = $this->product->findOneWhere([
|
||||
'sku' => $uploadData['sku'],
|
||||
]);
|
||||
|
||||
if ($product) {
|
||||
$canAdd = $product->haveSufficientQuantity($uploadData['quantity']);
|
||||
|
||||
if (! $canAdd) {
|
||||
$sufficientQuantity[] = $column + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if ($validator->fails()) {
|
||||
$failedRules[$column+1] = $validator->errors();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($failedRules)) {
|
||||
foreach ($failedRules as $coulmn => $fail) {
|
||||
if ($fail->first('sku')) {
|
||||
$errorMsg[$coulmn] = $fail->first('sku');
|
||||
} else if ($fail->first('quantity')) {
|
||||
$errorMsg[$coulmn] = $fail->first('quantity');
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($errorMsg as $key => $msg) {
|
||||
$msg = str_replace(".", "", $msg);
|
||||
$message[] = $msg. ' at Row ' .$key . '.';
|
||||
}
|
||||
|
||||
$finalMsg = implode(" ", $message);
|
||||
|
||||
session()->flash('error', $finalMsg);
|
||||
|
||||
return redirect()->back();
|
||||
} else if (isset($sufficientQuantity)) {
|
||||
$errorRows = implode(",", $sufficientQuantity);
|
||||
$finalMsg = trans('bulkaddtocart::app.products.quantity-error') . ' ' . $errorRows;
|
||||
|
||||
session()->flash('error', $finalMsg);
|
||||
|
||||
return redirect()->back();
|
||||
} else {
|
||||
foreach ($excelData as $data) {
|
||||
foreach ($data as $column => $uploadData) {
|
||||
$product = $this->product->findOneWhere([
|
||||
'sku' => $uploadData['sku'],
|
||||
]);
|
||||
|
||||
if ($product->type == 'simple') {
|
||||
$cart['product'] = (string)$product->id;
|
||||
$cart['quantity'] = (string)$uploadData['quantity'];
|
||||
$cart['is_configurable'] = 'false';
|
||||
|
||||
Event::fire('checkout.cart.add.before', $cart['product']);
|
||||
|
||||
$result = Cart::add($cart['product'], $cart);
|
||||
|
||||
Event::fire('checkout.cart.add.after', $result);
|
||||
|
||||
Cart::collectTotals();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
session()->flash('success', trans('bulkaddtocart::app.products.upload-sucess'));
|
||||
|
||||
return redirect()->route($this->_config['redirect']);
|
||||
} catch (\Exception $e) {
|
||||
$failure = new Failure(1, 'rows', [0 => trans('bulkaddtocart::app.products.enough-row-error')]);
|
||||
|
||||
session()->flash('error', $failure->errors()[0]);
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\BulkAddToCart\Http\Controllers;
|
||||
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
|
||||
class Controller extends BaseController
|
||||
{
|
||||
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
Route::group(['middleware' => ['web', 'locale', 'theme', 'currency']], function () {
|
||||
|
||||
Route::prefix('customer')->group(function () {
|
||||
|
||||
Route::group(['middleware' => ['customer']], function () {
|
||||
|
||||
Route::get('bulk-add-to-cart', 'Webkul\BulkAddToCart\Http\Controllers\BulkAddToCartController@create')->defaults('_config', [
|
||||
'view' => 'bulkaddtocart::products.bulk-add-to-cart'
|
||||
])->name('cart.bulk-add-to-cart.create');
|
||||
|
||||
Route::post('bulk-add-to-cart', 'Webkul\BulkAddToCart\Http\Controllers\BulkAddToCartController@store')->defaults('_config', [
|
||||
'redirect' => 'shop.checkout.cart.index'
|
||||
])->name('cart.bulk-add-to-cart.store');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\BulkAddToCart\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Routing\Router;
|
||||
|
||||
class BulkAddToCartServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot(Router $router)
|
||||
{
|
||||
$this->loadRoutesFrom(__DIR__ . '/../Http/routes.php');
|
||||
|
||||
$this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'bulkaddtocart');
|
||||
|
||||
$this->loadViewsFrom(__DIR__ . '/../Resources/views', 'bulkaddtocart');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->registerConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register package config.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function registerConfig()
|
||||
{
|
||||
$this->mergeConfigFrom(
|
||||
dirname(__DIR__) . '/Config/menu.php', 'menu.customer'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'products' => [
|
||||
'bulk-add-to-cart' => 'Bulk Add to Cart',
|
||||
'file' => 'File',
|
||||
'submit' => 'Submit',
|
||||
'upload-sucess' => 'Products Sucessfully added to Cart',
|
||||
'quantity-error' => 'Products have not sufficient quantity at row',
|
||||
'upload-error' => 'The file must be a file of type: xls, xlsx, csv, ods.',
|
||||
'enough-row-error' => 'file has not enough rows',
|
||||
],
|
||||
];
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
@extends('shop::layouts.master')
|
||||
|
||||
@section('page_title')
|
||||
{{ __('bulkaddtocart::app.products.bulk-add-to-cart') }}
|
||||
@endsection
|
||||
|
||||
@section('content-wrapper')
|
||||
|
||||
<div class="account-content">
|
||||
@include('shop::customers.account.partials.sidemenu')
|
||||
|
||||
<div class="account-layout">
|
||||
|
||||
<div class="account-head mb-15">
|
||||
<span class="account-heading">{{ __('bulkaddtocart::app.products.bulk-add-to-cart') }}</span>
|
||||
|
||||
<div class="horizontal-rule"></div>
|
||||
</div>
|
||||
|
||||
<div class="account-items-list">
|
||||
<form method="POST" action="{{ route('cart.bulk-add-to-cart.store') }}" enctype="multipart/form-data" @submit.prevent="onSubmit">
|
||||
@csrf()
|
||||
|
||||
<div class="control-group" :class="[errors.has('file') ? 'has-error' : '']">
|
||||
<label for="file" class="required">{{ __('bulkaddtocart::app.products.file') }}</label>
|
||||
<input v-validate="'required'" type="file" class="control" id="file" name="file" data-vv-as=""{{ __('bulkaddtocart::app.products.file') }}"" value="{{ old('file') }}" style="padding-top: 5px">
|
||||
<span class="control-error" v-if="errors.has('file')">@{{ errors.first('file') }}</span>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-lg btn-primary">
|
||||
{{ __('bulkaddtocart::app.products.submit') }}
|
||||
</button>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"name": "bagisto/customer-document",
|
||||
"name": "bagisto/laravel-customer-document",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue