diff --git a/composer.json b/composer.json index ada2d1e1c..58323a4e4 100755 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/config/app.php b/config/app.php index b142d15f6..83f330445 100755 --- a/config/app.php +++ b/config/app.php @@ -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, ], /* diff --git a/packages/Webkul/BulkAddToCart/composer.json b/packages/Webkul/BulkAddToCart/composer.json new file mode 100644 index 000000000..36b409ba8 --- /dev/null +++ b/packages/Webkul/BulkAddToCart/composer.json @@ -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" +} \ No newline at end of file diff --git a/packages/Webkul/BulkAddToCart/src/Config/menu.php b/packages/Webkul/BulkAddToCart/src/Config/menu.php new file mode 100644 index 000000000..20ef523ad --- /dev/null +++ b/packages/Webkul/BulkAddToCart/src/Config/menu.php @@ -0,0 +1,12 @@ + 'account.bulk-add-to-cart', + 'name' => 'bulkaddtocart::app.products.bulk-add-to-cart', + 'route' =>'cart.bulk-add-to-cart.create', + 'sort' => 7 + ] +]; + +?> \ No newline at end of file diff --git a/packages/Webkul/BulkAddToCart/src/Http/Controllers/BulkAddToCartController.php b/packages/Webkul/BulkAddToCart/src/Http/Controllers/BulkAddToCartController.php new file mode 100644 index 000000000..f4b02b50c --- /dev/null +++ b/packages/Webkul/BulkAddToCart/src/Http/Controllers/BulkAddToCartController.php @@ -0,0 +1,163 @@ + + * @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(); + } + } + } +} \ No newline at end of file diff --git a/packages/Webkul/BulkAddToCart/src/Http/Controllers/Controller.php b/packages/Webkul/BulkAddToCart/src/Http/Controllers/Controller.php new file mode 100644 index 000000000..47f4fa3ef --- /dev/null +++ b/packages/Webkul/BulkAddToCart/src/Http/Controllers/Controller.php @@ -0,0 +1,13 @@ + ['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'); + }); + }); +}); \ No newline at end of file diff --git a/packages/Webkul/BulkAddToCart/src/Providers/BulkAddToCartServiceProvider.php b/packages/Webkul/BulkAddToCart/src/Providers/BulkAddToCartServiceProvider.php new file mode 100644 index 000000000..f9f096835 --- /dev/null +++ b/packages/Webkul/BulkAddToCart/src/Providers/BulkAddToCartServiceProvider.php @@ -0,0 +1,45 @@ +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' + ); + } +} \ No newline at end of file diff --git a/packages/Webkul/BulkAddToCart/src/Resources/lang/en/app.php b/packages/Webkul/BulkAddToCart/src/Resources/lang/en/app.php new file mode 100644 index 000000000..edef522ce --- /dev/null +++ b/packages/Webkul/BulkAddToCart/src/Resources/lang/en/app.php @@ -0,0 +1,13 @@ + [ + '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', + ], +]; \ No newline at end of file diff --git a/packages/Webkul/BulkAddToCart/src/Resources/views/products/bulk-add-to-cart.blade.php b/packages/Webkul/BulkAddToCart/src/Resources/views/products/bulk-add-to-cart.blade.php new file mode 100644 index 000000000..5f369b688 --- /dev/null +++ b/packages/Webkul/BulkAddToCart/src/Resources/views/products/bulk-add-to-cart.blade.php @@ -0,0 +1,39 @@ +@extends('shop::layouts.master') + +@section('page_title') + {{ __('bulkaddtocart::app.products.bulk-add-to-cart') }} +@endsection + +@section('content-wrapper') + +