Refactored code and remove duplicate code

This commit is contained in:
jitendra 2022-09-21 18:14:00 +05:30
parent 627140d0a4
commit aa61de17d8
17 changed files with 155 additions and 186 deletions

View File

@ -2,7 +2,6 @@
namespace Webkul\Product\Http\Controllers; namespace Webkul\Product\Http\Controllers;
use Exception;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Webkul\Admin\DataGrids\ProductDataGrid; use Webkul\Admin\DataGrids\ProductDataGrid;
@ -104,8 +103,10 @@ class ProductController extends Controller
if ( if (
ProductType::hasVariants(request()->input('type')) ProductType::hasVariants(request()->input('type'))
&& (! request()->has('super_attributes') && (
|| ! count(request()->get('super_attributes'))) ! request()->has('super_attributes')
|| ! count(request()->get('super_attributes'))
)
) { ) {
session()->flash('error', trans('admin::app.catalog.products.configurable-error')); session()->flash('error', trans('admin::app.catalog.products.configurable-error'));
@ -274,7 +275,7 @@ class ProductController extends Controller
return response()->json([ return response()->json([
'message' => trans('admin::app.response.delete-success', ['name' => 'Product']), 'message' => trans('admin::app.response.delete-success', ['name' => 'Product']),
]); ]);
} catch (Exception $e) { } catch (\Exception $e) {
report($e); report($e);
} }

View File

@ -53,7 +53,9 @@ class ProductForm extends FormRequest
{ {
$product = $this->productRepository->find($this->id); $product = $this->productRepository->find($this->id);
$maxVideoFileSize = (core()->getConfigData('catalog.products.attribute.file_attribute_upload_size')) ? core()->getConfigData('catalog.products.attribute.file_attribute_upload_size') : '2048'; $maxVideoFileSize = core()->getConfigData('catalog.products.attribute.file_attribute_upload_size')
? core()->getConfigData('catalog.products.attribute.file_attribute_upload_size')
: '2048';
$this->rules = array_merge($product->getTypeInstance()->getTypeValidationRules(), [ $this->rules = array_merge($product->getTypeInstance()->getTypeValidationRules(), [
'sku' => ['required', 'unique:products,sku,' . $this->id, new Slug], 'sku' => ['required', 'unique:products,sku,' . $this->id, new Slug],

View File

@ -587,7 +587,7 @@ class Product extends Model implements ProductContract
* *
* @return void * @return void
*/ */
public function refreshloadedAttributeValues(): void public function refreshLoadedAttributeValues(): void
{ {
self::$loadedAttributeValues = []; self::$loadedAttributeValues = [];
} }

View File

@ -345,11 +345,14 @@ class ProductRepository extends Repository
/** /**
* Returns newly added product. * Returns newly added product.
* *
* @param int $count
* @return \Illuminate\Support\Collection * @return \Illuminate\Support\Collection
*/ */
public function getNewProducts() public function getNewProducts($count = null)
{ {
$count = core()->getConfigData('catalog.products.homepage.no_of_new_product_homepage'); if (! $count) {
$count = core()->getConfigData('catalog.products.homepage.no_of_new_product_homepage');
}
$results = app(ProductFlatRepository::class)->scopeQuery(function ($query) { $results = app(ProductFlatRepository::class)->scopeQuery(function ($query) {
$channel = core()->getRequestedChannelCode(); $channel = core()->getRequestedChannelCode();
@ -372,11 +375,14 @@ class ProductRepository extends Repository
/** /**
* Returns featured product. * Returns featured product.
* *
* @param int $count
* @return \Illuminate\Support\Collection * @return \Illuminate\Support\Collection
*/ */
public function getFeaturedProducts() public function getFeaturedProducts($count = null)
{ {
$count = core()->getConfigData('catalog.products.homepage.no_of_featured_product_homepage'); if (! $count) {
$count = core()->getConfigData('catalog.products.homepage.no_of_featured_product_homepage');
}
$results = app(ProductFlatRepository::class)->scopeQuery(function ($query) { $results = app(ProductFlatRepository::class)->scopeQuery(function ($query) {
$channel = core()->getRequestedChannelCode(); $channel = core()->getRequestedChannelCode();

View File

@ -10,7 +10,7 @@ namespace Webkul\Tax\Helpers;
class Tax class Tax
{ {
/** /**
* Tax rate precission. * Tax rate precision.
* *
* @var int * @var int
*/ */
@ -93,7 +93,9 @@ class Tax
return new class() return new class()
{ {
public $country; public $country;
public $state; public $state;
public $postcode; public $postcode;
function __construct() function __construct()
@ -101,7 +103,9 @@ class Tax
$this->country = core()->getConfigData('taxes.catalogue.default-location-calculation.country') != '' $this->country = core()->getConfigData('taxes.catalogue.default-location-calculation.country') != ''
? core()->getConfigData('taxes.catalogue.default-location-calculation.country') ? core()->getConfigData('taxes.catalogue.default-location-calculation.country')
: strtoupper(config('app.default_country')); : strtoupper(config('app.default_country'));
$this->state = core()->getConfigData('taxes.catalogue.default-location-calculation.state'); $this->state = core()->getConfigData('taxes.catalogue.default-location-calculation.state');
$this->postcode = core()->getConfigData('taxes.catalogue.default-location-calculation.post_code'); $this->postcode = core()->getConfigData('taxes.catalogue.default-location-calculation.post_code');
} }
}; };

View File

@ -25,8 +25,6 @@ class TaxCategoryRepository extends Repository
*/ */
public function attachOrDetach($taxCategory, $data) public function attachOrDetach($taxCategory, $data)
{ {
$taxCategory->tax_rates;
$this->model->findOrFail($taxCategory->id)->tax_rates()->sync($data); $this->model->findOrFail($taxCategory->id)->tax_rates()->sync($data);
return true; return true;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,9 +1,9 @@
{ {
"/js/jquery-ez-plus.js": "/js/jquery-ez-plus.js?id=ba3c7cada62de152fd8fce211d0b1b70", "/js/jquery-ez-plus.js": "/js/jquery-ez-plus.js?id=ba3c7cada62de152fd8fce211d0b1b70",
"/js/velocity-core.js": "/js/velocity-core.js?id=73cc7c3501570ebe9151c72d954bd97d", "/js/velocity-core.js": "/js/velocity-core.js?id=73cc7c3501570ebe9151c72d954bd97d",
"/js/velocity.js": "/js/velocity.js?id=0d596f41977fd0fae50ccb5efadd13f0", "/js/velocity.js": "/js/velocity.js?id=83a0618bf07b69ad4369c0bae1910296",
"/js/manifest.js": "/js/manifest.js?id=e069a8f952a02ea0f290bcca8fab930e", "/js/manifest.js": "/js/manifest.js?id=e069a8f952a02ea0f290bcca8fab930e",
"/js/components.js": "/js/components.js?id=31a698db1035bfcb62e3819412bcf2a1", "/js/components.js": "/js/components.js?id=4d84c5cd2cc1a94703fd04627b62bd26",
"/css/velocity.css": "/css/velocity.css?id=99812a83c44eb8390c6e1d34d67259b8", "/css/velocity.css": "/css/velocity.css?id=99812a83c44eb8390c6e1d34d67259b8",
"/css/velocity-admin.css": "/css/velocity-admin.css?id=b67a82956e53163b5e3ff45a44f9778f", "/css/velocity-admin.css": "/css/velocity-admin.css?id=b67a82956e53163b5e3ff45a44f9778f",
"/images/icon-calendar.svg": "/images/icon-calendar.svg?id=870d0f733a58377422766f3152e15486", "/images/icon-calendar.svg": "/images/icon-calendar.svg?id=870d0f733a58377422766f3152e15486",

View File

@ -2,18 +2,28 @@
namespace Webkul\Velocity\Http\Controllers\Shop; namespace Webkul\Velocity\Http\Controllers\Shop;
use Cart;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Webkul\Velocity\Helpers\Helper; use Webkul\Checkout\Facades\Cart;
use Webkul\Checkout\Contracts\Cart as CartModel; use Webkul\Checkout\Contracts\Cart as CartModel;
use Webkul\Product\Repositories\ProductRepository; use Webkul\Product\Repositories\ProductRepository;
class CartController extends Controller class CartController extends Controller
{ {
/** /**
* Retrives the mini cart details * Create a new controller instance.
* *
* @return \Illuminate\Http\Response * @param \Webkul\Product\Repositories\ProductRepository $productRepository
*
* @return void
*/
public function __construct(protected ProductRepository $productRepository)
{
}
/**
* Retrieves the mini cart details
*
* @return \Illuminate\Http\JsonResponse
*/ */
public function getMiniCartDetails() public function getMiniCartDetails()
{ {

View File

@ -2,8 +2,35 @@
namespace Webkul\Velocity\Http\Controllers\Shop; namespace Webkul\Velocity\Http\Controllers\Shop;
use Webkul\Velocity\Helpers\Helper;
use Webkul\Product\Models\ProductFlat;
use Webkul\Velocity\Repositories\VelocityCustomerCompareProductRepository as CustomerCompareProductRepository;
class ComparisonController extends Controller class ComparisonController extends Controller
{ {
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
/**
* Create a new controller instance.
*
* @param \Webkul\Velocity\Helpers\Helper $velocityHelper
* @param \Webkul\Velocity\Repositories\VelocityCustomerCompareProductRepository $compareProductsRepository
*
* @return void
*/
public function __construct(
protected Helper $velocityHelper,
protected CustomerCompareProductRepository $compareProductsRepository
)
{
$this->_config = request('_config');
}
/** /**
* Method for customers to get products in comparison. * Method for customers to get products in comparison.
* *
@ -69,47 +96,39 @@ class ComparisonController extends Controller
'product_flat_id' => $productId, 'product_flat_id' => $productId,
]); ]);
if (! $compareProduct) { if ($compareProduct) {
// insert new row
$productFlatRepository = app('\Webkul\Product\Models\ProductFlat');
$productFlat = $productFlatRepository
->where('id', $productId)
->orWhere('parent_id', $productId)
->orWhere('id', $productId)
->get()
->first();
if ($productFlat == null) {
return response()->json([
'status' => 'warning',
'message' => trans('customer::app.product-removed'),
'label' => trans('velocity::app.shop.general.alert.warning'),
]);
}
if ($productFlat) {
$productId = $productFlat->id;
$this->compareProductsRepository->create([
'customer_id' => $customerId,
'product_flat_id' => $productId,
]);
}
return response()->json([ return response()->json([
'status' => 'success', 'status' => 'warning',
'message' => trans('velocity::app.customer.compare.added'), 'label' => trans('velocity::app.shop.general.alert.warning'),
'label' => trans('velocity::app.shop.general.alert.success'),
]);
} else {
return response()->json([
'status' => 'success',
'label' => trans('velocity::app.shop.general.alert.success'),
'message' => trans('velocity::app.customer.compare.already_added'), 'message' => trans('velocity::app.customer.compare.already_added'),
]); ]);
} }
$productFlat = app(ProductFlat::class)
->where('id', $productId)
->orWhere('parent_id', $productId)
->orWhere('id', $productId)
->get()
->first();
if (! $productFlat) {
return response()->json([
'status' => 'warning',
'message' => trans('customer::app.product-removed'),
'label' => trans('velocity::app.shop.general.alert.warning'),
]);
}
$this->compareProductsRepository->create([
'customer_id' => $customerId,
'product_flat_id' => $productFlat->id,
]);
return response()->json([
'status' => 'success',
'message' => trans('velocity::app.customer.compare.added'),
'label' => trans('velocity::app.shop.general.alert.success'),
]);
} }
/** /**

View File

@ -6,47 +6,7 @@ use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController; use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Validation\ValidatesRequests;
use Webkul\Velocity\Helpers\Helper;
use Webkul\Product\Repositories\SearchRepository;
use Webkul\Product\Repositories\ProductRepository;
use Webkul\Customer\Repositories\WishlistRepository;
use Webkul\Category\Repositories\CategoryRepository;
use Webkul\Velocity\Repositories\Product\ProductRepository as VelocityProductRepository;
use Webkul\Velocity\Repositories\VelocityCustomerCompareProductRepository as CustomerCompareProductRepository;
class Controller extends BaseController class Controller extends BaseController
{ {
use DispatchesJobs, ValidatesRequests; use DispatchesJobs, ValidatesRequests;
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
/**
* Create a new controller instance.
*
* @param \Webkul\Velocity\Helpers\Helper $velocityHelper
* @param \Webkul\Product\Repositories\SearchRepository $searchRepository
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @param \Webkul\Velocity\Repositories\Product\ProductRepository $velocityProductRepository
* @param \Webkul\Velocity\Repositories\VelocityCustomerCompareProductRepository $compareProductsRepository
*
* @return void
*/
public function __construct(
protected Helper $velocityHelper,
protected SearchRepository $searchRepository,
protected ProductRepository $productRepository,
protected WishlistRepository $wishlistRepository,
protected CategoryRepository $categoryRepository,
protected VelocityProductRepository $velocityProductRepository,
protected CustomerCompareProductRepository $compareProductsRepository
)
{
$this->_config = request('_config');
}
} }

View File

@ -2,10 +2,47 @@
namespace Webkul\Velocity\Http\Controllers\Shop; namespace Webkul\Velocity\Http\Controllers\Shop;
use Webkul\Velocity\Helpers\Helper;
use Webkul\Product\Repositories\ProductRepository;
use Webkul\Customer\Repositories\WishlistRepository;
use Webkul\Category\Repositories\CategoryRepository;
use Webkul\Velocity\Repositories\Product\ProductRepository as VelocityProductRepository;
use Webkul\Velocity\Repositories\VelocityCustomerCompareProductRepository as CustomerCompareProductRepository;
use Webkul\Product\Facades\ProductImage; use Webkul\Product\Facades\ProductImage;
class ShopController extends Controller class ShopController extends Controller
{ {
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
/**
* Create a new controller instance.
*
* @param \Webkul\Velocity\Helpers\Helper $velocityHelper
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @param \Webkul\Product\Repositories\WishlistRepository $wishlistRepository
* @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @param \Webkul\Velocity\Repositories\Product\ProductRepository $velocityProductRepository
* @param \Webkul\Velocity\Repositories\VelocityCustomerCompareProductRepository $compareProductsRepository
*
* @return void
*/
public function __construct(
protected Helper $velocityHelper,
protected ProductRepository $productRepository,
protected WishlistRepository $wishlistRepository,
protected CategoryRepository $categoryRepository,
protected VelocityProductRepository $velocityProductRepository,
protected CustomerCompareProductRepository $compareProductsRepository
)
{
$this->_config = request('_config');
}
/** /**
* Index to handle the view loaded with the search results. * Index to handle the view loaded with the search results.
* *

View File

@ -2,17 +2,14 @@
namespace Webkul\Velocity\Repositories\Product; namespace Webkul\Velocity\Repositories\Product;
use Webkul\Core\Eloquent\Repository;
use Illuminate\Container\Container; use Illuminate\Container\Container;
use Webkul\Product\Models\ProductAttributeValue; use Webkul\Product\Repositories\ProductRepository as BaseProductRepository;
use Prettus\Repository\Traits\CacheableRepository;
use Webkul\Product\Repositories\ProductFlatRepository; use Webkul\Product\Repositories\ProductFlatRepository;
use Webkul\Attribute\Repositories\AttributeRepository; use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Product\Models\ProductAttributeValue;
class ProductRepository extends Repository class ProductRepository extends BaseProductRepository
{ {
use CacheableRepository;
/** /**
* Create a new controller instance. * Create a new controller instance.
* *
@ -25,72 +22,12 @@ class ProductRepository extends Repository
Container $container Container $container
) )
{ {
parent::__construct($container); parent::__construct(
$attributeRepository,
$container
);
} }
/**
* Specify Model class name
*
* @return string
*/
function model(): string
{
return 'Webkul\Product\Contracts\Product';
}
/**
* Returns featured product
*
* @param int $count
* @return \Illuminate\Support\Collection
*/
public function getFeaturedProducts($count)
{
$results = app(ProductFlatRepository::class)->scopeQuery(function($query) {
$channel = core()->getRequestedChannelCode();
$locale = core()->getRequestedLocaleCode();
return $query->distinct()
->addSelect('product_flat.*')
->where('product_flat.status', 1)
->where('product_flat.visible_individually', 1)
->where('product_flat.featured', 1)
->where('product_flat.channel', $channel)
->where('product_flat.locale', $locale)
->orderBy('product_id', 'desc');
})->paginate($count);
return $results;
}
/**
* Returns newly added product
*
* @param int $count
* @return \Illuminate\Support\Collection
*/
public function getNewProducts($count)
{
$results = app(ProductFlatRepository::class)->scopeQuery(function($query) {
$channel = core()->getRequestedChannelCode();
$locale = core()->getRequestedLocaleCode();
return $query->distinct()
->addSelect('product_flat.*')
->where('product_flat.status', 1)
->where('product_flat.visible_individually', 1)
->where('product_flat.new', 1)
->where('product_flat.channel', $channel)
->where('product_flat.locale', $locale)
->orderBy('product_id', 'desc');
})->paginate($count);
return $results;
}
/** /**
* Search Product by Attribute * Search Product by Attribute
* *
@ -99,10 +36,7 @@ class ProductRepository extends Repository
*/ */
public function searchProductsFromCategory($params) public function searchProductsFromCategory($params)
{ {
$term = $params['term'] ?? ''; $results = app(ProductFlatRepository::class)->scopeQuery(function($query) use($params) {
$categoryId = $params['category'] ?? '';
$results = app(ProductFlatRepository::class)->scopeQuery(function($query) use($term, $categoryId, $params) {
$channel = core()->getRequestedChannelCode(); $channel = core()->getRequestedChannelCode();
$locale = core()->getRequestedLocaleCode(); $locale = core()->getRequestedLocaleCode();
@ -121,14 +55,12 @@ class ProductRepository extends Repository
->where('product_flat.locale', $locale) ->where('product_flat.locale', $locale)
->whereNotNull('product_flat.url_key'); ->whereNotNull('product_flat.url_key');
if ($term) if (! empty($params['term'])) {
$query->where('product_flat.name', 'like', '%' . urldecode($term) . '%'); $query->where('product_flat.name', 'like', '%' . urldecode($params['term']) . '%');
}
if ( if (! empty($params['category'])) {
$categoryId $query = $query->where('product_categories.category_id', $params['category']);
&& $categoryId !== ""
) {
$query = $query->where('product_categories.category_id', $categoryId);
} }
if (isset($params['sort'])) { if (isset($params['sort'])) {

View File

@ -46,8 +46,8 @@
); );
} else { } else {
window.showAlert( window.showAlert(
`alert-success`, `alert-warning`,
this.__('shop.general.alert.success'), this.__('shop.general.alert.warning'),
`${this.__('customer.compare.already_added')}` `${this.__('customer.compare.already_added')}`
); );
} }

View File

@ -80,7 +80,7 @@ class CartCest
]) ])
->update(['boolean_value' => 0]); ->update(['boolean_value' => 0]);
$this->simpleProduct2->refreshloadedAttributeValues(); $this->simpleProduct2->refreshLoadedAttributeValues();
Event::dispatch('catalog.product.update.after', $this->simpleProduct2->refresh()); Event::dispatch('catalog.product.update.after', $this->simpleProduct2->refresh());
@ -109,7 +109,7 @@ class CartCest
]) ])
->update(['boolean_value' => 0]); ->update(['boolean_value' => 0]);
$this->simpleProduct2->refreshloadedAttributeValues(); $this->simpleProduct2->refreshLoadedAttributeValues();
Event::dispatch('catalog.product.update.after', $this->downloadableProduct2->refresh()); Event::dispatch('catalog.product.update.after', $this->downloadableProduct2->refresh());
@ -132,7 +132,7 @@ class CartCest
]) ])
->update(['boolean_value' => 0]); ->update(['boolean_value' => 0]);
$this->simpleProduct2->refreshloadedAttributeValues(); $this->simpleProduct2->refreshLoadedAttributeValues();
Event::dispatch('catalog.product.update.after', $this->virtualProduct2->refresh()); Event::dispatch('catalog.product.update.after', $this->virtualProduct2->refresh());

View File

@ -36,7 +36,7 @@ class BookingCronCest
['booking_product_id' => $bookingProducts[$i]->id]); ['booking_product_id' => $bookingProducts[$i]->id]);
$products[$i]->refresh(); $products[$i]->refresh();
$products[$i]->refreshloadedAttributeValues(); $products[$i]->refreshLoadedAttributeValues();
$I->assertNotFalse($products[$i]->status); $I->assertNotFalse($products[$i]->status);
} }
@ -44,7 +44,7 @@ class BookingCronCest
for ($i=0; $i<$index; $i++) { for ($i=0; $i<$index; $i++) {
$products[$i]->refresh(); $products[$i]->refresh();
$products[$i]->refreshloadedAttributeValues(); $products[$i]->refreshLoadedAttributeValues();
if ($bookingProducts[$i]->available_to < Carbon::now()) { if ($bookingProducts[$i]->available_to < Carbon::now()) {
$I->assertEquals(0, $products[$i]->status); $I->assertEquals(0, $products[$i]->status);