diff --git a/packages/Webkul/CartRule/src/Helpers/CartRule.php b/packages/Webkul/CartRule/src/Helpers/CartRule.php index 8ba53e4d4..24f347930 100644 --- a/packages/Webkul/CartRule/src/Helpers/CartRule.php +++ b/packages/Webkul/CartRule/src/Helpers/CartRule.php @@ -3,15 +3,15 @@ namespace Webkul\CartRule\Helpers; use Carbon\Carbon; +use Webkul\Checkout\Facades\Cart; +use Webkul\Rule\Helpers\Validator; +use Webkul\Checkout\Models\CartItem; use Illuminate\Database\Eloquent\Builder; use Webkul\CartRule\Repositories\CartRuleRepository; -use Webkul\CartRule\Repositories\CartRuleCouponRepository; -use Webkul\CartRule\Repositories\CartRuleCouponUsageRepository; -use Webkul\CartRule\Repositories\CartRuleCustomerRepository; use Webkul\Customer\Repositories\CustomerGroupRepository; -use Webkul\Checkout\Models\CartItem; -use Webkul\Rule\Helpers\Validator; -use Webkul\Checkout\Facades\Cart; +use Webkul\CartRule\Repositories\CartRuleCouponRepository; +use Webkul\CartRule\Repositories\CartRuleCustomerRepository; +use Webkul\CartRule\Repositories\CartRuleCouponUsageRepository; class CartRule { @@ -153,7 +153,9 @@ class CartRule if (Cart::getCurrentCustomer()->check()) { $customerGroupId = Cart::getCurrentCustomer()->user()->customer_group_id; } else { - if ($customerGuestGroup = $this->customerGroupRepository->findOneByField('code', 'guest')) { + $customerGuestGroup = $this->customerGroupRepository->getCustomerGuestGroup(); + + if ($customerGuestGroup) { $customerGroupId = $customerGuestGroup->id; } } diff --git a/packages/Webkul/CatalogRule/src/Helpers/CatalogRuleProductPrice.php b/packages/Webkul/CatalogRule/src/Helpers/CatalogRuleProductPrice.php index 2d7413526..8420805e9 100644 --- a/packages/Webkul/CatalogRule/src/Helpers/CatalogRuleProductPrice.php +++ b/packages/Webkul/CatalogRule/src/Helpers/CatalogRuleProductPrice.php @@ -3,8 +3,8 @@ namespace Webkul\CatalogRule\Helpers; use Carbon\Carbon; -use Webkul\CatalogRule\Repositories\CatalogRuleProductPriceRepository; use Webkul\Customer\Repositories\CustomerGroupRepository; +use Webkul\CatalogRule\Repositories\CatalogRuleProductPriceRepository; class CatalogRuleProductPrice { @@ -161,7 +161,7 @@ class CatalogRuleProductPrice case 'by_fixed': $price = max(0, $price - $rule->discount_amount); - + break; case 'by_percent': @@ -191,7 +191,7 @@ class CatalogRuleProductPrice } /** - * Get catalog rules product price for specific date, channel and customer group + * Get catalog rules product price for specific date, channel and customer group. * * @param \Webkul\Product\Contracts\Product $product * @return array|void @@ -201,7 +201,7 @@ class CatalogRuleProductPrice if ($this->getCurrentCustomer()->check()) { $customerGroupId = $this->getCurrentCustomer()->user()->customer_group_id; } else { - $customerGroup = $this->customerGroupRepository->findOneByField('code', 'guest'); + $customerGroup = $this->customerGroupRepository->getCustomerGuestGroup(); if (! $customerGroup) { return; @@ -210,11 +210,6 @@ class CatalogRuleProductPrice $customerGroupId = $customerGroup->id; } - return $this->catalogRuleProductPriceRepository->findOneWhere([ - 'product_id' => $product->id, - 'channel_id' => core()->getCurrentChannel()->id, - 'customer_group_id' => $customerGroupId, - 'rule_date' => Carbon::now()->format('Y-m-d'), - ]); + return $this->catalogRuleProductPriceRepository->checkInLoadedCatalogRulePrice($product, $customerGroupId); } } \ No newline at end of file diff --git a/packages/Webkul/CatalogRule/src/Repositories/CatalogRuleProductPriceRepository.php b/packages/Webkul/CatalogRule/src/Repositories/CatalogRuleProductPriceRepository.php index 711a2026e..87f4b2964 100644 --- a/packages/Webkul/CatalogRule/src/Repositories/CatalogRuleProductPriceRepository.php +++ b/packages/Webkul/CatalogRule/src/Repositories/CatalogRuleProductPriceRepository.php @@ -2,12 +2,13 @@ namespace Webkul\CatalogRule\Repositories; +use Illuminate\Support\Carbon; use Webkul\Core\Eloquent\Repository; class CatalogRuleProductPriceRepository extends Repository { /** - * Specify Model class name + * Specify Model class name. * * @return mixed */ @@ -15,4 +16,25 @@ class CatalogRuleProductPriceRepository extends Repository { return 'Webkul\CatalogRule\Contracts\CatalogRuleProductPrice'; } + + /** + * Check if catalog rule prices already loaded. If already loaded then load from it. + * + * @return object + */ + public function checkInLoadedCatalogRulePrice($product, $customerGroupId) + { + static $catalogRulePrices = []; + + if (array_key_exists($product->id, $catalogRulePrices)) { + return $catalogRulePrices[$product->id]; + } + + return $catalogRulePrices[$product->id] = $this->findOneWhere([ + 'product_id' => $product->id, + 'channel_id' => core()->getCurrentChannel()->id, + 'customer_group_id' => $customerGroupId, + 'rule_date' => Carbon::now()->format('Y-m-d'), + ]); + } } \ No newline at end of file diff --git a/packages/Webkul/Core/src/Core.php b/packages/Webkul/Core/src/Core.php index bb2a03f3e..065e5ee71 100755 --- a/packages/Webkul/Core/src/Core.php +++ b/packages/Webkul/Core/src/Core.php @@ -4,14 +4,14 @@ namespace Webkul\Core; use Carbon\Carbon; use Webkul\Core\Models\Channel; -use Webkul\Core\Repositories\CurrencyRepository; -use Webkul\Core\Repositories\ExchangeRateRepository; -use Webkul\Core\Repositories\CountryRepository; -use Webkul\Core\Repositories\CountryStateRepository; -use Webkul\Core\Repositories\ChannelRepository; -use Webkul\Core\Repositories\LocaleRepository; -use Webkul\Core\Repositories\CoreConfigRepository; use Illuminate\Support\Facades\Config; +use Webkul\Core\Repositories\LocaleRepository; +use Webkul\Core\Repositories\ChannelRepository; +use Webkul\Core\Repositories\CountryRepository; +use Webkul\Core\Repositories\CurrencyRepository; +use Webkul\Core\Repositories\CoreConfigRepository; +use Webkul\Core\Repositories\CountryStateRepository; +use Webkul\Core\Repositories\ExchangeRateRepository; use Webkul\Customer\Repositories\CustomerGroupRepository; class Core @@ -72,9 +72,20 @@ class Core */ protected $coreConfigRepository; - /** @var Channel */ + /** + * @var \Webkul\Core\Models\Channel + */ private static $channel; + /** + * Register your core config keys here which you don't want to + * load in static array. These keys will load from database + * everytime the `getConfigData` method is called. + */ + private $coreConfigExceptions = [ + 'catalog.products.guest-checkout.allow-guest-checkout' + ]; + /** * Create a new instance. * @@ -118,7 +129,7 @@ class Core } /** - * Returns all channels + * Returns all channels. * * @return \Illuminate\Support\Collection */ @@ -134,7 +145,7 @@ class Core } /** - * Returns currenct channel models + * Returns currenct channel models. * * @return \Webkul\Core\Contracts\Channel */ @@ -158,7 +169,7 @@ class Core } /** - * Set the current channel + * Set the current channel. * * @param Channel $channel */ @@ -168,7 +179,7 @@ class Core } /** - * Returns currenct channel code + * Returns currenct channel code. * * @return \Webkul\Core\Contracts\Channel */ @@ -184,7 +195,7 @@ class Core } /** - * Returns default channel models + * Returns default channel models. * * @return \Webkul\Core\Contracts\Channel */ @@ -206,7 +217,7 @@ class Core } /** - * Returns the default channel code configured in config/app.php + * Returns the default channel code configured in `config/app.php`. * * @return string */ @@ -256,7 +267,7 @@ class Core } /** - * Returns current locale + * Returns current locale. * * @return \Webkul\Core\Contracts\Locale */ @@ -278,7 +289,7 @@ class Core } /** - * Returns all Customer Groups + * Returns all customer groups. * * @return \Illuminate\Support\Collection */ @@ -294,7 +305,7 @@ class Core } /** - * Returns all currencies + * Returns all currencies. * * @return \Illuminate\Support\Collection */ @@ -310,7 +321,7 @@ class Core } /** - * Returns base channel's currency model + * Returns base channel's currency model. * * @return \Webkul\Core\Contracts\Currency */ @@ -332,7 +343,7 @@ class Core } /** - * Returns base channel's currency code + * Returns base channel's currency code. * * @return string */ @@ -348,7 +359,7 @@ class Core } /** - * Returns base channel's currency model + * Returns base channel's currency model. * * @return \Webkul\Core\Contracts\Currency */ @@ -366,7 +377,7 @@ class Core } /** - * Returns base channel's currency code + * Returns base channel's currency code. * * @return string */ @@ -382,7 +393,7 @@ class Core } /** - * Returns current channel's currency model + * Returns current channel's currency model. * * @return \Webkul\Core\Contracts\Currency */ @@ -404,7 +415,7 @@ class Core } /** - * Returns current channel's currency code + * Returns current channel's currency code. * * @return string */ @@ -420,7 +431,27 @@ class Core } /** - * Converts price + * Returns exchange rates. + * + * @return object + */ + public function getExchangeRate($targetCurrencyId) + { + static $exchangeRate; + + if ($exchangeRate || $exchangeRate === '') { + return $exchangeRate; + } + + $found = $this->exchangeRateRepository->findOneWhere([ + 'target_currency' => $targetCurrencyId, + ]); + + return $exchangeRate = ($found ? $found : ''); + } + + /** + * Converts price. * * @param float $amount * @param string $targetCurrencyCode @@ -456,11 +487,9 @@ class Core return $amount; } - $exchangeRate = $this->exchangeRateRepository->findOneWhere([ - 'target_currency' => $targetCurrency->id, - ]); + $exchangeRate = $this->getExchangeRate($targetCurrency->id); - if (null === $exchangeRate || ! $exchangeRate->rate) { + if ('' === $exchangeRate || null === $exchangeRate || ! $exchangeRate->rate) { return $amount; } @@ -474,7 +503,7 @@ class Core } /** - * Converts to base price + * Converts to base price. * * @param float $amount * @param string $targetCurrencyCode @@ -503,7 +532,7 @@ class Core } /** - * Format and convert price with currency symbol + * Format and convert price with currency symbol. * * @param float $price * @@ -519,7 +548,7 @@ class Core } /** - * Return currency symbol from currency code + * Return currency symbol from currency code. * * @param float $price * @@ -533,7 +562,7 @@ class Core } /** - * Format and convert price with currency symbol + * Format and convert price with currency symbol. * * @param float $price * @@ -550,7 +579,7 @@ class Core } /** - * Format and convert price with currency symbol + * Format and convert price with currency symbol. * * @return array */ @@ -565,7 +594,7 @@ class Core $pattern = str_replace("#,##0.00", "%v", $pattern); return [ - 'symbol' => core()->currencySymbol(core()->getCurrentCurrencyCode()), + 'symbol' => $this->currencySymbol($this->getCurrentCurrencyCode()), 'decimal' => $formater->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL), 'format' => $pattern, ]; @@ -604,7 +633,7 @@ class Core } /** - * Checks if current date of the given channel (in the channel timezone) is within the range + * Checks if current date of the given channel (in the channel timezone) is within the range. * * @param int|string|\Webkul\Core\Contracts\Channel $channel * @param string|null $dateFrom @@ -638,7 +667,7 @@ class Core } /** - * Get channel timestamp, timstamp will be builded with channel timezone settings + * Get channel timestamp, timstamp will be builded with channel timezone settings. * * @param \Webkul\Core\Contracts\Channel $channel * @@ -660,7 +689,7 @@ class Core } /** - * Check whether sql date is empty + * Check whether sql date is empty. * * @param string $date * @@ -693,7 +722,7 @@ class Core } /** - * Retrieve information from payment configuration + * Retrieve information from payment configuration. * * @param string $field * @param int|string|null $channelId @@ -703,57 +732,20 @@ class Core */ public function getConfigData($field, $channel = null, $locale = null) { - if (null === $channel) { - $channel = request()->get('channel') ?: ($this->getCurrentChannelCode() ?: $this->getDefaultChannelCode()); - } + static $loadedConfigs = []; - if (null === $locale) { - $locale = request()->get('locale') ?: app()->getLocale(); - - $channelLocales = $this->channelRepository->findOneByField('code', $channel)->locales; - - if (! $channelLocales->contains('code', $locale)) { - $locale = config('app.fallback_locale'); - } - } - - $fields = $this->getConfigField($field); - - $channel_based = false; - $locale_based = false; - - if (isset($fields['channel_based']) && $fields['channel_based']) { - $channel_based = true; - } - - if (isset($fields['locale_based']) && $fields['locale_based']) { - $locale_based = true; - } - - if (isset($fields['channel_based']) && $fields['channel_based']) { - if (isset($fields['locale_based']) && $fields['locale_based']) { - $coreConfigValue = $this->coreConfigRepository->findOneWhere([ - 'code' => $field, - 'channel_code' => $channel, - 'locale_code' => $locale, - ]); - } else { - $coreConfigValue = $this->coreConfigRepository->findOneWhere([ - 'code' => $field, - 'channel_code' => $channel, - ]); - } + if (array_key_exists($field, $loadedConfigs) && ! in_array($field, $this->coreConfigExceptions)) { + $coreConfigValue = $loadedConfigs[$field]; } else { - if (isset($fields['locale_based']) && $fields['locale_based']) { - $coreConfigValue = $this->coreConfigRepository->findOneWhere([ - 'code' => $field, - 'locale_code' => $locale, - ]); - } else { - $coreConfigValue = $this->coreConfigRepository->findOneWhere([ - 'code' => $field, - ]); + if (null === $channel) { + $channel = request()->get('channel') ?: ($this->getCurrentChannelCode() ?: $this->getDefaultChannelCode()); } + + if (null === $locale) { + $locale = request()->get('locale') ?: app()->getLocale(); + } + + $loadedConfigs[$field] = $coreConfigValue = $this->getCoreConfigValue($field, $channel, $locale); } if (! $coreConfigValue) { @@ -770,7 +762,7 @@ class Core } /** - * Retrieve a group of information from the core config table + * Retrieve a group of information from the core config table. * * @param mixed $criteria * @@ -782,7 +774,7 @@ class Core } /** - * Retrieve all countries + * Retrieve all countries. * * @return \Illuminate\Support\Collection */ @@ -792,7 +784,7 @@ class Core } /** - * Returns country name by code + * Returns country name by code. * * @param string $code * @@ -806,7 +798,7 @@ class Core } /** - * Retrieve all country states + * Retrieve all country states. * * @param string $countryCode * @@ -818,7 +810,7 @@ class Core } /** - * Retrieve all grouped states by country code + * Retrieve all grouped states by country code. * * @return \Illuminate\Support\Collection */ @@ -834,7 +826,7 @@ class Core } /** - * Retrieve all grouped states by country code + * Retrieve all grouped states by country code. * * @return \Illuminate\Support\Collection */ @@ -852,7 +844,7 @@ class Core } /** - * Returns time intervals + * Returns time intervals. * * @param \Illuminate\Support\Carbon $startDate * @param \Illuminate\Support\Carbon $endDate @@ -912,7 +904,6 @@ class Core } /** - * * @param string $date * @param int $day * @@ -934,7 +925,7 @@ class Core } /** - * Method to sort through the acl items and put them in order + * Method to sort through the acl items and put them in order. * * @param array $items * @@ -1050,6 +1041,86 @@ class Core return $array; } + /** + * @param array $array1 + * + * @return array + */ + public function convertEmptyStringsToNull($array) + { + foreach ($array as $key => $value) { + if ($value == "" || $value == "null") { + $array[$key] = null; + } + } + + return $array; + } + + /** + * Create singleton object through single facade. + * + * @param string $className + * + * @return object + */ + public function getSingletonInstance($className) + { + static $instance = []; + + if (array_key_exists($className, $instance)) { + return $instance[$className]; + } + + return $instance[$className] = app($className); + } + + /** + * Returns a string as selector part for identifying elements in views. + * + * @param float $taxRate + * + * @return string + */ + public static function taxRateAsIdentifier(float $taxRate): string + { + return str_replace('.', '_', (string)$taxRate); + } + + /** + * Get Shop email sender details. + * + * @return array + */ + public function getSenderEmailDetails() + { + $sender_name = $this->getConfigData('general.general.email_settings.sender_name') ? $this->getConfigData('general.general.email_settings.sender_name') : config('mail.from.name'); + + $sender_email = $this->getConfigData('general.general.email_settings.shop_email_from') ? $this->getConfigData('general.general.email_settings.shop_email_from') : config('mail.from.address'); + + return [ + 'name' => $sender_name, + 'email' => $sender_email, + ]; + } + + /** + * Get Admin email details. + * + * @return array + */ + public function getAdminEmailDetails() + { + $admin_name = $this->getConfigData('general.general.email_settings.admin_name') ? $this->getConfigData('general.general.email_settings.admin_name') : config('mail.admin.name'); + + $admin_email = $this->getConfigData('general.general.email_settings.admin_email') ? $this->getConfigData('general.general.email_settings.admin_email') : config('mail.admin.address'); + + return [ + 'name' => $admin_name, + 'email' => $admin_email, + ]; + } + /** * @param array $array1 * @param array $array2 @@ -1072,82 +1143,38 @@ class Core } /** - * @param array $array1 - * - * @return array + * Get core config values. */ - public function convertEmptyStringsToNull($array) + protected function getCoreConfigValue($field, $channel, $locale) { - foreach ($array as $key => $value) { - if ($value == "" || $value == "null") { - $array[$key] = null; + $fields = $this->getConfigField($field); + + if (isset($fields['channel_based']) && $fields['channel_based']) { + if (isset($fields['locale_based']) && $fields['locale_based']) { + $coreConfigValue = $this->coreConfigRepository->findOneWhere([ + 'code' => $field, + 'channel_code' => $channel, + 'locale_code' => $locale, + ]); + } else { + $coreConfigValue = $this->coreConfigRepository->findOneWhere([ + 'code' => $field, + 'channel_code' => $channel, + ]); + } + } else { + if (isset($fields['locale_based']) && $fields['locale_based']) { + $coreConfigValue = $this->coreConfigRepository->findOneWhere([ + 'code' => $field, + 'locale_code' => $locale, + ]); + } else { + $coreConfigValue = $this->coreConfigRepository->findOneWhere([ + 'code' => $field, + ]); } } - return $array; - } - - /** - * Create singletom object through single facade - * - * @param string $className - * - * @return object - */ - public function getSingletonInstance($className) - { - static $instance = []; - - if (array_key_exists($className, $instance)) { - return $instance[$className]; - } - - return $instance[$className] = app($className); - } - - /** - * Returns a string as selector part for identifying elements in views - * - * @param float $taxRate - * - * @return string - */ - public static function taxRateAsIdentifier(float $taxRate): string - { - return str_replace('.', '_', (string)$taxRate); - } - - /** - * Get Shop email sender details - * - * @return array - */ - public function getSenderEmailDetails() - { - $sender_name = core()->getConfigData('general.general.email_settings.sender_name') ? core()->getConfigData('general.general.email_settings.sender_name') : config('mail.from.name'); - - $sender_email = core()->getConfigData('general.general.email_settings.shop_email_from') ? core()->getConfigData('general.general.email_settings.shop_email_from') : config('mail.from.address'); - - return [ - 'name' => $sender_name, - 'email' => $sender_email, - ]; - } - - /** - * Get Admin email details - * - * @return array - */ - public function getAdminEmailDetails() - { - $admin_name = core()->getConfigData('general.general.email_settings.admin_name') ? core()->getConfigData('general.general.email_settings.admin_name') : config('mail.admin.name'); - - $admin_email = core()->getConfigData('general.general.email_settings.admin_email') ? core()->getConfigData('general.general.email_settings.admin_email') : config('mail.admin.address'); - - return [ - 'name' => $admin_name, - 'email' => $admin_email, - ]; + return $coreConfigValue; } } \ No newline at end of file diff --git a/packages/Webkul/Customer/src/Helpers/Wishlist.php b/packages/Webkul/Customer/src/Helpers/Wishlist.php index 405417a1c..fa6321abb 100644 --- a/packages/Webkul/Customer/src/Helpers/Wishlist.php +++ b/packages/Webkul/Customer/src/Helpers/Wishlist.php @@ -2,28 +2,8 @@ namespace Webkul\Customer\Helpers; -use Webkul\Customer\Repositories\WishlistRepository; - class Wishlist { - /** - * WishlistRepository object - * - * @var \Webkul\Customer\Repositories\WishlistRepository - */ - protected $wishlistRepository; - - /** - * Create a new controller instance. - * - * @param \Webkul\Customer\Repositories\WishlistRepository - * @return void - */ - public function __construct(WishlistRepository $wishlistRepository) - { - $this->wishlistRepository = $wishlistRepository; - } - /** * Returns wishlist products for current customer. * @@ -34,12 +14,10 @@ class Wishlist { $wishlist = false; - if (auth()->guard('customer')->user()) { - $wishlist = $this->wishlistRepository->findOneWhere([ - 'channel_id' => core()->getCurrentChannel()->id, - 'product_id' => $product->product_id, - 'customer_id' => auth()->guard('customer')->user()->id, - ]); + if ($customer = auth()->guard('customer')->user()) { + $wishlist = $customer->wishlist_items->filter(function ($item) use ($product) { + return $item->channel_id == core()->getCurrentChannel()->id && $item->product_id == $product->product_id; + })->first(); } if ($wishlist) { diff --git a/packages/Webkul/Customer/src/Repositories/CustomerGroupRepository.php b/packages/Webkul/Customer/src/Repositories/CustomerGroupRepository.php index 8cd2c01b6..db1fdc175 100755 --- a/packages/Webkul/Customer/src/Repositories/CustomerGroupRepository.php +++ b/packages/Webkul/Customer/src/Repositories/CustomerGroupRepository.php @@ -42,4 +42,20 @@ class CustomerGroupRepository extends Repository return $customer; } + + /** + * Returns guest group. + * + * @return object + */ + public function getCustomerGuestGroup() + { + static $customerGuestGroup; + + if ($customerGuestGroup) { + return $customerGuestGroup; + } + + return $customerGuestGroup = $this->findOneByField('code', 'guest'); + } } \ No newline at end of file diff --git a/packages/Webkul/Inventory/src/Repositories/InventorySourceRepository.php b/packages/Webkul/Inventory/src/Repositories/InventorySourceRepository.php index 3cf0e5c63..4dfd98ac6 100755 --- a/packages/Webkul/Inventory/src/Repositories/InventorySourceRepository.php +++ b/packages/Webkul/Inventory/src/Repositories/InventorySourceRepository.php @@ -15,4 +15,24 @@ class InventorySourceRepository extends Repository { return 'Webkul\Inventory\Contracts\InventorySource'; } + + /** + * Returns channel inventory source ids. + * + * @return collection + */ + public function getChannelInventorySourceIds() + { + static $channelInventorySourceIds; + + if ($channelInventorySourceIds) { + return $channelInventorySourceIds; + } + + $found = core()->getCurrentChannel()->inventory_sources() + ->where('status', 1) + ->pluck('id'); + + return $channelInventorySourceIds = ($found ? $found : collect([])); + } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Helpers/Review.php b/packages/Webkul/Product/src/Helpers/Review.php index 993e90b14..6582888b6 100755 --- a/packages/Webkul/Product/src/Helpers/Review.php +++ b/packages/Webkul/Product/src/Helpers/Review.php @@ -74,6 +74,28 @@ class Review extends AbstractProduct return $totalRating[$product->id] = $product->reviews()->where('status', 'approved')->sum('rating'); } + /** + * Returns reviews with ratings. + * + * @param \Webkul\Product\Contracts\Product|\Webkul\Product\Contracts\ProductFlat $product + * + * @return collection + */ + public function getReviewsWithRatings($product) + { + static $reviews = []; + + if (array_key_exists($product->id, $reviews)) { + return $reviews[$product->id]; + } + + return $reviews[$product->id] = $product->reviews()->where('status', 'approved') + ->select('rating', DB::raw('count(*) as total')) + ->groupBy('rating') + ->orderBy('rating', 'desc') + ->get(); + } + /** * Returns the Percentage rating of the product * @@ -82,11 +104,7 @@ class Review extends AbstractProduct */ public function getPercentageRating($product) { - $reviews = $product->reviews()->where('status', 'approved') - ->select('rating', DB::raw('count(*) as total')) - ->groupBy('rating') - ->orderBy('rating', 'desc') - ->get(); + $reviews = $this->getReviewsWithRatings($product); $totalReviews = $this->getTotalReviews($product); diff --git a/packages/Webkul/Product/src/Models/Product.php b/packages/Webkul/Product/src/Models/Product.php index 269ebb6b2..84f6bf6a5 100755 --- a/packages/Webkul/Product/src/Models/Product.php +++ b/packages/Webkul/Product/src/Models/Product.php @@ -12,6 +12,7 @@ use Webkul\Attribute\Models\AttributeFamilyProxy; use Webkul\Inventory\Models\InventorySourceProxy; use Webkul\Attribute\Repositories\AttributeRepository; use Webkul\Product\Contracts\Product as ProductContract; +use Webkul\CatalogRule\Models\CatalogRuleProductPriceProxy; class Product extends Model implements ProductContract { @@ -338,6 +339,23 @@ class Product extends Model implements ProductContract return parent::getAttribute($key); } + /** + * Check in loaded family attributes. + * + * @return object + */ + public function checkInLoadedFamilyAttributes() + { + static $loadedFamilyAttributes = []; + + if (array_key_exists($this->attribute_family_id, $loadedFamilyAttributes)) { + return $loadedFamilyAttributes[$this->attribute_family_id]; + } + + return $loadedFamilyAttributes[$this->attribute_family_id] = core()->getSingletonInstance(AttributeRepository::class) + ->getFamilyAttributes($this->attribute_family); + } + /** * @return array */ @@ -348,9 +366,7 @@ class Product extends Model implements ProductContract $hiddenAttributes = $this->getHidden(); if (isset($this->id)) { - $familyAttributes = core() - ->getSingletonInstance(AttributeRepository::class) - ->getFamilyAttributes($this->attribute_family); + $familyAttributes = $this->checkInLoadedFamilyAttributes(); foreach ($familyAttributes as $attribute) { if (in_array($attribute->code, $hiddenAttributes)) { diff --git a/packages/Webkul/Product/src/Models/ProductFlat.php b/packages/Webkul/Product/src/Models/ProductFlat.php index 92a199407..c9bba615f 100644 --- a/packages/Webkul/Product/src/Models/ProductFlat.php +++ b/packages/Webkul/Product/src/Models/ProductFlat.php @@ -121,16 +121,14 @@ class ProductFlat extends Model implements ProductFlatContract */ public function images() { - return (ProductImageProxy::modelClass()) - ::where('product_images.product_id', $this->product_id) - ->select('product_images.*'); + return $this->hasMany(ProductImageProxy::modelClass(), 'product_id', 'product_id'); } /** * The videos that belong to the product. */ public function videos() - { + { return $this->product->videos(); } diff --git a/packages/Webkul/Product/src/ProductImage.php b/packages/Webkul/Product/src/ProductImage.php index 2ac256ac4..df2fe6699 100644 --- a/packages/Webkul/Product/src/ProductImage.php +++ b/packages/Webkul/Product/src/ProductImage.php @@ -36,10 +36,16 @@ class ProductImage extends AbstractProduct */ public function getGalleryImages($product) { + static $loadedGalleryImages = []; + if (! $product) { return []; } + if (array_key_exists($product->id, $loadedGalleryImages)) { + return $loadedGalleryImages[$product->id]; + } + $images = []; foreach ($product->images as $image) { @@ -64,16 +70,60 @@ class ProductImage extends AbstractProduct ]; } - return $images; + return $loadedGalleryImages[$product->id] = $images; } /** - * Get product's base image + * This method will first check whether the gallery images are already + * present or not. If not then it will load from the product. + * + * @param \Webkul\Product\Contracts\Product|\Webkul\Product\Contracts\ProductFlat $product + * @param array + * @return array + */ + public function getProductBaseImage($product, array $galleryImages = null) + { + static $loadedBaseImages = []; + + if ($product) { + if (array_key_exists($product->id, $loadedBaseImages)) { + return $loadedBaseImages[$product->id]; + } + + return $loadedBaseImages[$product->id] = $galleryImages + ? $galleryImages[0] + : $this->otherwiseLoadFromProduct($product); + } + } + + /** + * Get product varient image if available otherwise product base image. + * + * @param \Webkul\Customer\Contracts\Wishlist $item + * @return array + */ + public function getProductImage($item) + { + if ($item instanceof \Webkul\Customer\Contracts\Wishlist) { + if (isset($item->additional['selected_configurable_option'])) { + $product = $this->productRepository->find($item->additional['selected_configurable_option']); + } else { + $product = $item->product; + } + } else { + $product = $item->product; + } + + return $this->getProductBaseImage($product); + } + + /** + * Load product's base image. * * @param \Webkul\Product\Contracts\Product|\Webkul\Product\Contracts\ProductFlat $product * @return array */ - public function getProductBaseImage($product) + protected function otherwiseLoadFromProduct($product) { $images = $product ? $product->images : null; @@ -95,25 +145,4 @@ class ProductImage extends AbstractProduct return $image; } - - /** - * Get product varient image if available otherwise product base image - * - * @param \Webkul\Customer\Contracts\Wishlist $item - * @return array - */ - public function getProductImage($item) - { - if ($item instanceof \Webkul\Customer\Contracts\Wishlist) { - if (isset($item->additional['selected_configurable_option'])) { - $product = $this->productRepository->find($item->additional['selected_configurable_option']); - } else { - $product = $item->product; - } - } else { - $product = $item->product; - } - - return $this->getProductBaseImage($product); - } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Repositories/ProductCustomerGroupPriceRepository.php b/packages/Webkul/Product/src/Repositories/ProductCustomerGroupPriceRepository.php index 5a42c9421..ea773ee19 100755 --- a/packages/Webkul/Product/src/Repositories/ProductCustomerGroupPriceRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductCustomerGroupPriceRepository.php @@ -2,15 +2,13 @@ namespace Webkul\Product\Repositories; -use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; -use Illuminate\Http\UploadedFile; use Webkul\Core\Eloquent\Repository; class ProductCustomerGroupPriceRepository extends Repository { /** - * Specify Model class name + * Specify Model class name. * * @return mixed */ @@ -50,4 +48,22 @@ class ProductCustomerGroupPriceRepository extends Repository $this->delete($customerGroupPriceId); } } + + /** + * Check if product customer group prices already loaded then load from it. + * + * @return object + */ + public function checkInLoadedCustomerGroupPrice($product, $customerGroupId) + { + static $customerGroupPrices = []; + + if (array_key_exists($product->id, $customerGroupPrices)) { + return $customerGroupPrices[$product->id]; + } + + return $customerGroupPrices[$product->id] = $product->customer_group_prices->filter(function ($customerGroupPrice) use ($customerGroupId) { + return $customerGroupPrice->customer_group_id == $customerGroupId || is_null($customerGroupPrice->customer_group_id); + }); + } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php b/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php index 796b68f6a..5c3347945 100644 --- a/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php @@ -19,11 +19,17 @@ class ProductFlatRepository extends Repository */ public function getCategoryProductMaximumPrice($category = null) { + static $loadedCategoryMaxPrice = []; + if (! $category) { return $this->model->max('max_price'); } - return $this->model + if (array_key_exists($category->id, $loadedCategoryMaxPrice)) { + return $loadedCategoryMaxPrice[$category->id]; + } + + return $loadedCategoryMaxPrice[$category->id] = $this->model ->leftJoin('product_categories', 'product_flat.product_id', 'product_categories.product_id') ->where('product_categories.category_id', $category->id) ->max('max_price'); @@ -108,6 +114,12 @@ class ProductFlatRepository extends Repository */ public function getProductsRelatedFilterableAttributes($category) { + static $loadedCategoryAttributes = []; + + if (array_key_exists($category->id, $loadedCategoryAttributes)) { + return $loadedCategoryAttributes[$category->id]; + } + $productsCount = $this->categoryProductQuerybuilder($category->id)->count(); if ($productsCount > 0) { @@ -122,10 +134,10 @@ class ProductFlatRepository extends Repository } ])->whereIn('id', $allFilterableAttributes)->get(); - return $attributes; + return $loadedCategoryAttributes[$category->id] = $attributes; } else { - return $category->filterableAttributes; + return $loadedCategoryAttributes[$category->id] = $category->filterableAttributes; } } diff --git a/packages/Webkul/Product/src/Repositories/ProductInventoryRepository.php b/packages/Webkul/Product/src/Repositories/ProductInventoryRepository.php index d80731963..12422d80c 100755 --- a/packages/Webkul/Product/src/Repositories/ProductInventoryRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductInventoryRepository.php @@ -2,13 +2,12 @@ namespace Webkul\Product\Repositories; -use Illuminate\Container\Container as App; use Webkul\Core\Eloquent\Repository; class ProductInventoryRepository extends Repository { /** - * Specify Model class name + * Specify Model class name. * * @return mixed */ @@ -49,4 +48,20 @@ class ProductInventoryRepository extends Repository } } } + + /** + * Check if product inventories are already loaded. If already loaded then load from it. + * + * @return object + */ + public function checkInLoadedProductInventories($product) + { + static $productInventories = []; + + if (array_key_exists($product->id, $productInventories)) { + return $productInventories[$product->id]; + } + + return $productInventories[$product->id] = $product->inventories; + } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Type/AbstractType.php b/packages/Webkul/Product/src/Type/AbstractType.php index 5b2d2bfdf..460dda2dd 100644 --- a/packages/Webkul/Product/src/Type/AbstractType.php +++ b/packages/Webkul/Product/src/Type/AbstractType.php @@ -12,8 +12,11 @@ use Webkul\Attribute\Repositories\AttributeRepository; use Webkul\Product\Datatypes\CartItemValidationResult; use Webkul\Product\Repositories\ProductImageRepository; use Webkul\Product\Repositories\ProductVideoRepository; +use Webkul\Customer\Repositories\CustomerGroupRepository; +use Webkul\Inventory\Repositories\InventorySourceRepository; use Webkul\Product\Repositories\ProductInventoryRepository; use Webkul\Product\Repositories\ProductAttributeValueRepository; +use Webkul\Product\Repositories\ProductCustomerGroupPriceRepository; abstract class AbstractType { @@ -288,7 +291,7 @@ abstract class AbstractType $this->productVideoRepository->uploadVideos($data, $product); - app('Webkul\Product\Repositories\ProductCustomerGroupPriceRepository')->saveCustomerGroupPrices($data, + app(ProductCustomerGroupPriceRepository::class)->saveCustomerGroupPrices($data, $product); } @@ -435,20 +438,18 @@ abstract class AbstractType { $total = 0; - $channelInventorySourceIds = core()->getCurrentChannel() - ->inventory_sources() - ->where('status', 1) - ->pluck('id'); + $channelInventorySourceIds = app(InventorySourceRepository::class)->getChannelInventorySourceIds(); - foreach ($this->product->inventories as $inventory) { - if (is_numeric($index = $channelInventorySourceIds->search($inventory->inventory_source_id))) { + $productInventories = $this->productInventoryRepository->checkInLoadedProductInventories($this->product); + + foreach ($productInventories as $inventory) { + if (is_numeric($channelInventorySourceIds->search($inventory->inventory_source_id))) { $total += $inventory->qty; } } $orderedInventory = $this->product->ordered_inventories() - ->where('channel_id', core()->getCurrentChannel()->id) - ->first(); + ->where('channel_id', core()->getCurrentChannel()->id)->first(); if ($orderedInventory) { $total -= $orderedInventory->qty; @@ -573,7 +574,9 @@ abstract class AbstractType $rulePrice = app('Webkul\CatalogRule\Helpers\CatalogRuleProductPrice')->getRulePrice($this->product); - if ((is_null($this->product->special_price) || ! (float)$this->product->special_price) + $specialPrice = $this->product->special_price; + + if ((is_null($specialPrice) || ! (float) $specialPrice) && ! $rulePrice && $customerGroupPrice == $this->product->price ) { @@ -582,7 +585,7 @@ abstract class AbstractType $haveSpecialPrice = false; - if (! (float)$this->product->special_price) { + if (! (float) $specialPrice) { if ($rulePrice && $rulePrice->price < $this->product->price) { $this->product->special_price = $rulePrice->price; @@ -633,18 +636,14 @@ abstract class AbstractType if (Cart::getCurrentCustomer()->check()) { $customerGroupId = Cart::getCurrentCustomer()->user()->customer_group_id; } else { - $customerGroupRepository = app('Webkul\Customer\Repositories\CustomerGroupRepository'); + $customerGuestGroup = app(CustomerGroupRepository::class)->getCustomerGuestGroup(); - if ($customerGuestGroup = $customerGroupRepository->findOneByField('code', 'guest')) { + if ($customerGuestGroup) { $customerGroupId = $customerGuestGroup->id; } } - $customerGroupPrices = $product->customer_group_prices()->where(function ($query) use ($customerGroupId) { - $query->where('customer_group_id', $customerGroupId) - ->orWhereNull('customer_group_id'); - } - )->get(); + $customerGroupPrices = app(ProductCustomerGroupPriceRepository::class)->checkInLoadedCustomerGroupPrice($product, $customerGroupId); if (!$customerGroupPrices->count()) { return $product->price; @@ -995,4 +994,20 @@ abstract class AbstractType return $offerLines; } + + /** + * Check in loaded saleable. + * + * @return object + */ + public function checkInLoadedSaleableChecks($product, $callback) + { + static $loadedSaleableChecks = []; + + if (array_key_exists($product->id, $loadedSaleableChecks)) { + return $loadedSaleableChecks[$product->id]; + } + + return $loadedSaleableChecks[$product->id] = $callback($product); + } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Type/Grouped.php b/packages/Webkul/Product/src/Type/Grouped.php index 17899afd2..97d928c04 100644 --- a/packages/Webkul/Product/src/Type/Grouped.php +++ b/packages/Webkul/Product/src/Type/Grouped.php @@ -2,15 +2,14 @@ namespace Webkul\Product\Type; -use Webkul\Attribute\Repositories\AttributeRepository; +use Webkul\Product\Models\ProductFlat; use Webkul\Product\Repositories\ProductRepository; -use Webkul\Product\Repositories\ProductAttributeValueRepository; -use Webkul\Product\Repositories\ProductInventoryRepository; +use Webkul\Attribute\Repositories\AttributeRepository; use Webkul\Product\Repositories\ProductImageRepository; use Webkul\Product\Repositories\ProductVideoRepository; +use Webkul\Product\Repositories\ProductInventoryRepository; +use Webkul\Product\Repositories\ProductAttributeValueRepository; use Webkul\Product\Repositories\ProductGroupedProductRepository; -use Webkul\Product\Models\ProductAttributeValue; -use Webkul\Product\Models\ProductFlat; class Grouped extends AbstractType { diff --git a/packages/Webkul/Product/src/Type/Simple.php b/packages/Webkul/Product/src/Type/Simple.php index 8dced5ecb..03eb5ccf8 100644 --- a/packages/Webkul/Product/src/Type/Simple.php +++ b/packages/Webkul/Product/src/Type/Simple.php @@ -19,33 +19,36 @@ class Simple extends AbstractType ]; /** - * Show quantity box + * Show quantity box. * * @var bool */ protected $showQuantityBox = true; /** - * Return true if this product type is saleable + * Return true if this product type is saleable. Saleable check added because + * this is the point where all parent product will recall this. * * @return bool */ public function isSaleable() { - if (! $this->product->status) { + return $this->checkInLoadedSaleableChecks($this->product, function ($product) { + if (! $product->status) { + return false; + } + + if (is_callable(config('products.isSaleable')) && + call_user_func(config('products.isSaleable'), $product) === false) { + return false; + } + + if ($this->haveSufficientQuantity(1)) { + return true; + } + return false; - } - - if (is_callable(config('products.isSaleable')) && - call_user_func(config('products.isSaleable'), $this->product) === false) { - return false; - } - - if ($this->haveSufficientQuantity(1)) { - return true; - } - - return false; + }); } /** diff --git a/packages/Webkul/Shop/src/Http/Middleware/Currency.php b/packages/Webkul/Shop/src/Http/Middleware/Currency.php index 52a3d9265..5655724ae 100755 --- a/packages/Webkul/Shop/src/Http/Middleware/Currency.php +++ b/packages/Webkul/Shop/src/Http/Middleware/Currency.php @@ -2,8 +2,8 @@ namespace Webkul\Shop\Http\Middleware; -use Webkul\Core\Repositories\CurrencyRepository; use Closure; +use Webkul\Core\Repositories\CurrencyRepository; class Currency { diff --git a/packages/Webkul/Shop/src/Resources/views/guest/compare/compare-products.blade.php b/packages/Webkul/Shop/src/Resources/views/guest/compare/compare-products.blade.php index b6f65a053..85fe7368a 100644 --- a/packages/Webkul/Shop/src/Resources/views/guest/compare/compare-products.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/guest/compare/compare-products.blade.php @@ -68,7 +68,7 @@ @case('addToCartHtml')
-
+
diff --git a/packages/Webkul/Shop/src/Resources/views/layouts/header/index.blade.php b/packages/Webkul/Shop/src/Resources/views/layouts/header/index.blade.php index 771d88d22..333b2899e 100755 --- a/packages/Webkul/Shop/src/Resources/views/layouts/header/index.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/layouts/header/index.blade.php @@ -262,7 +262,7 @@ if (imageInput.files[0].type.includes('image/')) { var self = this; - if (imageInput.files[0].size <= 2000000) { + if (imageInput.files[0].size <= 2000000) { self.$root.showLoader(); var formData = new FormData(); @@ -344,8 +344,8 @@ ]; self.$root.addFlashMessages(); - - } + + } } else { imageInput.value = ''; diff --git a/packages/Webkul/Velocity/src/Helpers/Helper.php b/packages/Webkul/Velocity/src/Helpers/Helper.php index c2cb28969..51129aa4c 100644 --- a/packages/Webkul/Velocity/src/Helpers/Helper.php +++ b/packages/Webkul/Velocity/src/Helpers/Helper.php @@ -9,8 +9,8 @@ use Webkul\Product\Models\Product as ProductModel; use Webkul\Product\Repositories\ProductRepository; use Webkul\Product\Repositories\ProductFlatRepository; use Webkul\Velocity\Repositories\OrderBrandsRepository; -use Webkul\Attribute\Repositories\AttributeOptionRepository; use Webkul\Product\Repositories\ProductReviewRepository; +use Webkul\Attribute\Repositories\AttributeOptionRepository; use Webkul\Velocity\Repositories\VelocityMetadataRepository; class Helper extends Review @@ -174,49 +174,18 @@ class Helper extends Review } /** - * Returns the count rating of the product - * - * @param \Webkul\Product\Contracts\Product $product - * - * @return int - */ - public function getCountRating($product) - { - $reviews = $product->reviews() - ->where('status', 'approved') - ->select('rating', DB::raw('count(*) as total')) - ->groupBy('rating') - ->orderBy('rating','desc') - ->get(); - - $totalReviews = $this->getTotalReviews($product); - - for ($i = 5; $i >= 1; $i--) { - if (! $reviews->isEmpty()) { - foreach ($reviews as $review) { - if ($review->rating == $i) { - $percentage[$i] = $review->total; - - break; - } else { - $percentage[$i]=0; - } - } - } else { - $percentage[$i]=0; - } - } - - return $percentage; - } - - /** - * Returns the count rating of the product + * Returns the count rating of the product. * * @return array */ public function getVelocityMetaData($locale = null, $channel = null, $default = true) { + static $metaData; + + if ($metaData) { + return $metaData; + } + if (! $locale) { $locale = request()->get('locale') ?: app()->getLocale(); } @@ -309,12 +278,8 @@ class Helper extends Review { $reviewHelper = app('Webkul\Product\Helpers\Review'); - $totalReviews = $reviewHelper->getTotalReviews($product); - - $avgRatings = ceil($reviewHelper->getAverageRating($product)); - $galleryImages = ProductImage::getGalleryImages($product); - $productImage = ProductImage::getProductBaseImage($product)['medium_image_url']; + $productImage = ProductImage::getProductBaseImage($product, $galleryImages)['medium_image_url']; $largeProductImageName = "large-product-placeholder.png"; $mediumProductImageName = "meduim-product-placeholder.png"; @@ -334,8 +299,8 @@ class Helper extends Review return [ 'priceHTML' => $priceHTML, - 'avgRating' => $avgRatings, - 'totalReviews' => $totalReviews, + 'avgRating' => ceil($reviewHelper->getAverageRating($product)), + 'totalReviews' => $reviewHelper->getTotalReviews($product), 'image' => $productImage, 'new' => $isProductNew, 'galleryImages' => $galleryImages, @@ -344,7 +309,6 @@ class Helper extends Review 'description' => $product->description, 'shortDescription' => $product->short_description, 'firstReviewText' => trans('velocity::app.products.be-first-review'), - 'defaultAddToCart' => view('shop::products.add-buttons', ['product' => $product])->render(), 'addToCartHtml' => view('shop::products.add-to-cart', [ 'product' => $product, 'addWishlistClass' => ! (isset($list) && $list) ? '' : '', @@ -373,38 +337,26 @@ class Helper extends Review */ public function fetchProductCollection($items, $moveToCart = false, $separator='&') { - $productCollection = []; - $productIds = explode($separator, $items); + $productIds = collect(explode($separator, $items)); - foreach ($productIds as $productId) { - // @TODO:- query only once insted of 2 + return $productIds->map(function ($productId) use ($moveToCart) { $productFlat = $this->productFlatRepository->findOneWhere(['id' => $productId]); if ($productFlat) { - $product = $this->productRepository->findOneWhere(['id' => $productFlat->product_id]); + $formattedProduct = $this->formatProduct($productFlat, false, [ + 'moveToCart' => $moveToCart, + 'btnText' => $moveToCart ? trans('shop::app.customer.account.wishlist.move-to-cart') : null, + ]); - if ($product) { - $formattedProduct = $this->formatProduct($productFlat, false, [ - 'moveToCart' => $moveToCart, - 'btnText' => $moveToCart ? trans('shop::app.customer.account.wishlist.move-to-cart') : null, - ]); - - $productMetaDetails = []; - $productMetaDetails['slug'] = $product->url_key; - $productMetaDetails['product_image'] = $formattedProduct['image']; - $productMetaDetails['priceHTML'] = $formattedProduct['priceHTML']; - $productMetaDetails['new'] = $formattedProduct['new']; - $productMetaDetails['addToCartHtml'] = $formattedProduct['addToCartHtml']; - $productMetaDetails['galleryImages'] = $formattedProduct['galleryImages']; - $productMetaDetails['defaultAddToCart'] = $formattedProduct['defaultAddToCart']; - - $product = array_merge($productFlat->toArray(), $productMetaDetails); - - array_push($productCollection, $product); - } + return array_merge($productFlat->toArray(), [ + 'slug' => $productFlat->url_key, + 'product_image' => $formattedProduct['image'], + 'priceHTML' => $formattedProduct['priceHTML'], + 'new' => $formattedProduct['new'], + 'addToCartHtml' => $formattedProduct['addToCartHtml'], + 'galleryImages' => $formattedProduct['galleryImages'] + ]); } - } - - return $productCollection; + })->toArray(); } } diff --git a/packages/Webkul/Velocity/src/Http/Controllers/Shop/ComparisonController.php b/packages/Webkul/Velocity/src/Http/Controllers/Shop/ComparisonController.php index fd4fd5966..d1639f303 100644 --- a/packages/Webkul/Velocity/src/Http/Controllers/Shop/ComparisonController.php +++ b/packages/Webkul/Velocity/src/Http/Controllers/Shop/ComparisonController.php @@ -2,26 +2,19 @@ namespace Webkul\Velocity\Http\Controllers\Shop; -use Webkul\Velocity\Helpers\Helper; -use Webkul\Product\Repositories\ProductRepository; -use Webkul\Velocity\Repositories\VelocityCustomerCompareProductRepository as CustomerCompareProductRepository; - class ComparisonController extends Controller { /** - * function for customers to get products in comparison. + * Method for customers to get products in comparison. * * @return \Illuminate\Http\Response|\Illuminate\View\View */ public function getComparisonList() { if (! core()->getConfigData('general.content.shop.compare_option')) { - abort(404); } else { if (request()->get('data')) { - $productSlugs = null; - $productCollection = []; if (auth()->guard('customer')->user()) { @@ -32,20 +25,17 @@ class ComparisonController extends Controller 'product_flat.id' ) ->where('customer_id', auth()->guard('customer')->user()->id) - ->get() - ->toArray(); + ->get(); - $items = []; - - foreach ($productCollection as $index => $customerCompare) { - array_push($items, $customerCompare['id']); - } - - $items = implode('&', $items); - $productCollection = $this->velocityHelper->fetchProductCollection($items); + $items = $productCollection->map(function ($product) { + return $product->id; + })->join('&'); + $productCollection = ! empty($items) + ? $this->velocityHelper->fetchProductCollection($items) + : []; } else { - // for product details + /* for product details */ if ($items = request()->get('items')) { $productCollection = $this->velocityHelper->fetchProductCollection($items); } diff --git a/packages/Webkul/Velocity/src/Http/Controllers/Shop/ShopController.php b/packages/Webkul/Velocity/src/Http/Controllers/Shop/ShopController.php index 3c3eb0b99..ce5f98895 100644 --- a/packages/Webkul/Velocity/src/Http/Controllers/Shop/ShopController.php +++ b/packages/Webkul/Velocity/src/Http/Controllers/Shop/ShopController.php @@ -66,7 +66,6 @@ class ShopController extends Controller switch ($slug) { case 'new-products': case 'featured-products': - $formattedProducts = []; $count = request()->get('count'); if ($slug == "new-products") { @@ -75,19 +74,17 @@ class ShopController extends Controller $products = $this->velocityProductRepository->getFeaturedProducts($count); } - foreach ($products as $product) { - if (core()->getConfigData('catalog.products.homepage.out_of_stock_items')) { - array_push($formattedProducts, $this->velocityHelper->formatProduct($product)); - } else { - if ($product->isSaleable()) { - array_push($formattedProducts, $this->velocityHelper->formatProduct($product)); - } - } - } - $response = [ 'status' => true, - 'products' => $formattedProducts, + 'products' => $products->map(function ($product) { + if (core()->getConfigData('catalog.products.homepage.out_of_stock_items')) { + return $this->velocityHelper->formatProduct($product); + } else { + if ($product->isSaleable()) { + return $this->velocityHelper->formatProduct($product); + } + } + }), ]; break; @@ -259,26 +256,36 @@ class ShopController extends Controller ]); } + /** + * This method will fetch products from category. + * + * @param int $categoryId + * + * @return \Illuminate\Http\Response + */ public function getCategoryProducts($categoryId) { - $products = $this->productRepository->getAll($categoryId); + /* fetch category details */ + $categoryDetails = $this->categoryRepository->find($categoryId); - $productItems = $products->items(); - $productsArray = $products->toArray(); - - if ($productItems) { - $formattedProducts = []; - - foreach ($productItems as $product) { - array_push($formattedProducts, $this->velocityHelper->formatProduct($product)); - } - - $productsArray['data'] = $formattedProducts; + /* if category not found then return empty response */ + if (! $categoryDetails) { + return response()->json([ + 'products' => [], + 'paginationHTML' => '' + ]); } - return response()->json($response ?? [ - 'products' => $productsArray, - 'paginationHTML' => $products->appends(request()->input())->links()->toHtml(), + /* fetching products */ + $products = $this->productRepository->getAll($categoryId); + $products->withPath($categoryDetails->slug); + + /* sending response */ + return response()->json([ + 'products' => collect($products->items())->map(function ($product) { + return $this->velocityHelper->formatProduct($product); + }), + 'paginationHTML' => $products->appends(request()->input())->links()->toHtml() ]); } -} +} \ No newline at end of file diff --git a/packages/Webkul/Velocity/src/Resources/views/shop/products/index.blade.php b/packages/Webkul/Velocity/src/Resources/views/shop/products/index.blade.php index 7780a789d..aaf5877c6 100644 --- a/packages/Webkul/Velocity/src/Resources/views/shop/products/index.blade.php +++ b/packages/Webkul/Velocity/src/Resources/views/shop/products/index.blade.php @@ -49,8 +49,6 @@ 'products_and_description' ] ); - - $products = $productRepository->getAll($category->id); @endphp @section('content-wrapper') @@ -126,9 +124,7 @@ {!! view_render_event('bagisto.shop.productOrCategory.index.pagination.before', ['category' => $category]) !!} -
- {{ $products->appends(request()->input())->links() }} -
+
{!! view_render_event('bagisto.shop.productOrCategory.index.pagination.after', ['category' => $category]) !!} @@ -166,7 +162,7 @@ this.$http.get(`${this.$root.baseUrl}/category-products/{{ $category->id }}${window.location.search}`) .then(response => { this.isLoading = false; - this.products = response.data.products.data; + this.products = response.data.products; this.paginationHTML = response.data.paginationHTML; }) .catch(error => { diff --git a/packages/Webkul/Velocity/src/Resources/views/shop/products/view.blade.php b/packages/Webkul/Velocity/src/Resources/views/shop/products/view.blade.php index 7c978246e..943b0a54a 100644 --- a/packages/Webkul/Velocity/src/Resources/views/shop/products/view.blade.php +++ b/packages/Webkul/Velocity/src/Resources/views/shop/products/view.blade.php @@ -32,7 +32,7 @@ @endif - getProductBaseImage($product); ?> + getProductBaseImage($product, $images); ?> diff --git a/packages/Webkul/Velocity/src/Resources/views/shop/products/view/reviews.blade.php b/packages/Webkul/Velocity/src/Resources/views/shop/products/view/reviews.blade.php index e68fb7c86..b45d02b57 100644 --- a/packages/Webkul/Velocity/src/Resources/views/shop/products/view/reviews.blade.php +++ b/packages/Webkul/Velocity/src/Resources/views/shop/products/view/reviews.blade.php @@ -2,15 +2,15 @@ @inject ('customHelper', 'Webkul\Velocity\Helpers\Helper') @php + $reviews = $reviewHelper->getReviews($product)->paginate(10); + if (! isset($total)) { $total = $reviewHelper->getTotalReviews($product); - $avgRatings = $reviewHelper->getAverageRating($product); $avgStarRating = round($avgRatings); } $percentageRatings = $reviewHelper->getPercentageRating($product); - $countRatings = $customHelper->getCountRating($product); @endphp {!! view_render_event('bagisto.shop.products.review.before', ['product' => $product]) !!} @@ -127,7 +127,7 @@
- @foreach ($reviewHelper->getReviews($product)->paginate(10) as $review) + @foreach ($reviews as $review)

{{ $review->title }}

@@ -165,7 +165,7 @@
- @foreach ($reviewHelper->getReviews($product)->paginate(10) as $review) + @foreach ($reviews as $review)

{{ $review->title }}