sarga/packages/Webkul/Velocity/src/Helpers/Helper.php

363 lines
12 KiB
PHP
Raw Normal View History

2020-01-26 08:47:05 +00:00
<?php
namespace Webkul\Velocity\Helpers;
use Illuminate\Support\Facades\DB;
2020-01-26 08:47:05 +00:00
use Webkul\Product\Helpers\Review;
2021-02-04 12:42:02 +00:00
use Webkul\Product\Facades\ProductImage;
2020-01-26 08:47:05 +00:00
use Webkul\Product\Models\Product as ProductModel;
2020-03-06 06:55:59 +00:00
use Webkul\Product\Repositories\ProductRepository;
use Webkul\Product\Repositories\ProductFlatRepository;
2020-01-26 08:47:05 +00:00
use Webkul\Velocity\Repositories\OrderBrandsRepository;
use Webkul\Product\Repositories\ProductReviewRepository;
use Webkul\Attribute\Repositories\AttributeOptionRepository;
2020-01-26 08:47:05 +00:00
use Webkul\Velocity\Repositories\VelocityMetadataRepository;
class Helper extends Review
{
2020-03-05 13:37:08 +00:00
/**
2020-03-19 11:09:29 +00:00
* productModel object
*
* @var \Webkul\Product\Contracts\Product
*/
2020-03-05 13:37:08 +00:00
protected $productModel;
2020-01-26 08:47:05 +00:00
/**
* orderBrands object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Velocity\Repositories\OrderBrandsRepository
2020-01-26 08:47:05 +00:00
*/
2020-03-05 13:37:08 +00:00
protected $orderBrandsRepository;
2020-01-26 08:47:05 +00:00
/**
2020-03-06 10:32:52 +00:00
* ProductRepository object
2020-01-26 08:47:05 +00:00
*
2020-03-06 10:32:52 +00:00
* @var \Webkul\Product\Repositories\ProductRepository
2020-01-26 08:47:05 +00:00
*/
protected $productRepository;
2020-03-05 13:37:08 +00:00
/**
2020-03-06 10:32:52 +00:00
* ProductFlatRepository object
2020-03-06 06:55:59 +00:00
*
2020-03-06 10:32:52 +00:00
* @var \Webkul\Product\Repositories\ProductFlatRepository
2020-03-06 06:55:59 +00:00
*/
protected $productFlatRepository;
2020-03-19 11:09:29 +00:00
/**
2020-01-26 08:47:05 +00:00
* productModel object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Attribute\Repositories\AttributeOptionRepository
2020-01-26 08:47:05 +00:00
*/
2020-03-05 13:37:08 +00:00
protected $attributeOptionRepository;
2020-01-26 08:47:05 +00:00
/**
* ProductReviewRepository object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Product\Repositories\ProductReviewRepository
2020-01-26 08:47:05 +00:00
*/
protected $productReviewRepository;
/**
* VelocityMetadata object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Velocity\Repositories\VelocityMetadataRepository
2020-01-26 08:47:05 +00:00
*/
protected $velocityMetadataRepository;
2020-03-05 13:37:08 +00:00
/**
* Create a helper instamce
*
2020-03-19 05:55:05 +00:00
* @param \Webkul\Product\Contracts\Product $productModel
* @param \Webkul\Velocity\Repositories\OrderBrandsRepository $orderBrands
* @param \Webkul\Attribute\Repositories\AttributeOptionRepository $attributeOptionRepository
* @param \Webkul\Product\Repositories\ProductReviewRepository $productReviewRepository
* @param \Webkul\Velocity\Repositories\VelocityMetadataRepository $velocityMetadataRepository
*
2020-03-05 13:37:08 +00:00
* @return void
*/
2020-01-26 08:47:05 +00:00
public function __construct(
ProductModel $productModel,
ProductRepository $productRepository,
AttributeOptionRepository $attributeOptionRepository,
2020-03-06 06:55:59 +00:00
ProductFlatRepository $productFlatRepository,
2020-01-26 08:47:05 +00:00
OrderBrandsRepository $orderBrandsRepository,
ProductReviewRepository $productReviewRepository,
VelocityMetadataRepository $velocityMetadataRepository
) {
$this->productModel = $productModel;
2020-02-20 07:38:52 +00:00
$this->attributeOptionRepository = $attributeOptionRepository;
2020-01-26 08:47:05 +00:00
$this->productRepository = $productRepository;
2020-02-20 07:38:52 +00:00
2020-03-06 06:55:59 +00:00
$this->productFlatRepository = $productFlatRepository;
2020-03-19 05:55:05 +00:00
2020-01-26 08:47:05 +00:00
$this->orderBrandsRepository = $orderBrandsRepository;
2020-02-20 07:38:52 +00:00
2020-01-26 08:47:05 +00:00
$this->productReviewRepository = $productReviewRepository;
2020-02-20 07:38:52 +00:00
2020-01-26 08:47:05 +00:00
$this->velocityMetadataRepository = $velocityMetadataRepository;
}
2020-03-05 13:37:08 +00:00
/**
2020-03-19 05:55:05 +00:00
* @param \Webkul\Sales\Contracts\Order $order
*
2020-03-05 13:37:08 +00:00
* @return void
*/
2020-01-26 08:47:05 +00:00
public function topBrand($order)
{
$orderItems = $order->items;
foreach ($orderItems as $key => $orderItem) {
$products[] = $orderItem->product;
2020-04-15 07:38:11 +00:00
try {
$this->orderBrandsRepository->create([
'order_item_id' => $orderItem->id,
'order_id' => $orderItem->order_id,
'product_id' => $orderItem->product_id,
'brand' => $products[$key]->brand,
]);
2020-04-15 13:04:22 +00:00
} catch(\Exception $exception) {}
2020-01-26 08:47:05 +00:00
}
}
2020-03-05 13:37:08 +00:00
/**
* @return \Illuminate\Support\Collection|\Exception
*/
2020-01-26 08:47:05 +00:00
public function getBrandsWithCategories()
{
try {
$orderBrand = $this->orderBrandsRepository->get()->toArray();
if (isset($orderBrand) && ! empty($orderBrand)) {
foreach ($orderBrand as $product) {
$product_id[] = $product['product_id'];
$product_categories = $this->productRepository->with('categories')->findWhereIn('id', $product_id)->toArray();
}
$categoryName = $brandName = $brandImplode = [];
2020-02-20 07:38:52 +00:00
2020-01-26 08:47:05 +00:00
foreach($product_categories as $totalData) {
2020-02-20 07:38:52 +00:00
$brand = $this->attributeOptionRepository->findOneWhere(['id' => $totalData['brand']]);
2020-01-26 08:47:05 +00:00
foreach ($totalData['categories'] as $categories) {
foreach($categories['translations'] as $catName) {
if (isset($brand->admin_name)) {
$brandData[$brand->admin_name][] = $catName['name'];
$categoryName[] = $catName['name'];
}
}
}
}
$uniqueCategoryName = array_unique($categoryName);
foreach($uniqueCategoryName as $key => $categoryNameValue) {
foreach($brandData as $brandDataKey => $brandDataValue) {
if(in_array($categoryNameValue,$brandDataValue)) {
$brandName[$categoryNameValue][] = $brandDataKey;
}
}
}
foreach($brandName as $brandKey => $brandvalue) {
$brandImplode[$brandKey][] = implode(' | ',array_map("ucfirst", $brandvalue));
}
return $brandImplode;
}
} catch (Exception $exception){
throw $exception;
}
}
/**
* Returns the count rating of the product.
2020-03-05 13:37:08 +00:00
*
2021-06-08 13:15:44 +00:00
* @return \Webkul\Velocity\Repositories\VelocityMetadataRepository
2020-03-05 13:37:08 +00:00
*/
2020-08-17 05:58:12 +00:00
public function getVelocityMetaData($locale = null, $channel = null, $default = true)
2020-01-26 08:47:05 +00:00
{
static $metaData;
if ($metaData) {
return $metaData;
}
2020-06-11 09:32:39 +00:00
if (! $locale) {
$locale = core()->getRequestedLocaleCode();
2020-06-11 09:32:39 +00:00
}
2020-01-26 08:47:05 +00:00
2020-08-17 05:58:12 +00:00
if (! $channel) {
2020-12-14 13:54:30 +00:00
$channel = request()->get('channel') ?: core()->getCurrentChannelCode() ?: 'default';
2020-08-17 05:58:12 +00:00
}
2020-06-11 09:32:39 +00:00
try {
$metaData = $this->velocityMetadataRepository->findOneWhere([
2020-08-17 05:58:12 +00:00
'locale' => $locale,
'channel' => $channel
2020-06-11 09:32:39 +00:00
]);
2020-01-26 08:47:05 +00:00
if (! $metaData && $default) {
2020-06-11 09:39:06 +00:00
$metaData = $this->velocityMetadataRepository->findOneWhere([
2020-08-17 05:58:12 +00:00
'locale' => 'en',
'channel' => 'default'
2020-06-11 09:39:06 +00:00
]);
}
2020-01-26 08:47:05 +00:00
return $metaData;
} catch (\Exception $exception) {
}
}
2020-03-05 13:37:08 +00:00
/**
* @param int $reviewCount
* @return \Illuminate\Support\Collection
*/
2020-01-26 08:47:05 +00:00
public function getShopRecentReviews($reviewCount = 4)
{
2020-03-19 11:09:29 +00:00
$reviews = $this->productReviewRepository
->getModel()
->orderBy('id', 'desc')
->where('status', 'approved')
->take($reviewCount)->get();
2020-01-26 08:47:05 +00:00
return $reviews;
}
2020-02-08 13:30:25 +00:00
2020-03-05 13:37:08 +00:00
/**
* @return array
*/
2020-02-08 13:30:25 +00:00
public function jsonTranslations()
{
$currentLocale = app()->getLocale();
$path = __DIR__ . "/../Resources/lang/$currentLocale/app.php";
if (is_string($path) && is_readable($path)) {
return include $path;
2020-04-09 14:49:36 +00:00
} else {
$currentLocale = "en";
$path = __DIR__ . "/../Resources/lang/$currentLocale/app.php";
2020-02-08 13:30:25 +00:00
2020-04-09 14:49:36 +00:00
return include $path;
}
2020-02-08 13:30:25 +00:00
}
2020-02-12 13:45:34 +00:00
2020-03-05 13:37:08 +00:00
/**
* @param \Webkul\Checkout\Contracts\CartItem $item
* @return array
*/
2020-02-12 13:45:34 +00:00
public function formatCartItem($item)
{
$product = $item->product;
2020-03-05 13:37:08 +00:00
2020-02-12 13:45:34 +00:00
$images = $product->getTypeInstance()->getBaseImage($item);
return [
2020-02-20 07:38:52 +00:00
'images' => $images,
'itemId' => $item->id,
'name' => $item->name,
'quantity' => $item->quantity,
2020-03-19 11:09:29 +00:00
'url_key' => $product->url_key,
2020-02-12 13:45:34 +00:00
'baseTotal' => core()->currency($item->base_total),
];
}
2020-03-05 13:37:08 +00:00
/**
* @param \Webkul\Product\Contracts\Product $product
2020-03-24 05:03:03 +00:00
* @param bool $list
* @param array $metaInformation
2020-08-17 05:58:12 +00:00
*
2020-03-05 13:37:08 +00:00
* @return array
*/
2020-03-19 11:09:29 +00:00
public function formatProduct($product, $list = false, $metaInformation = [])
{
$reviewHelper = app('Webkul\Product\Helpers\Review');
2021-02-04 12:42:02 +00:00
$galleryImages = ProductImage::getGalleryImages($product);
2021-02-05 14:22:20 +00:00
$productImage = ProductImage::getProductBaseImage($product, $galleryImages)['medium_image_url'];
$largeProductImageName = "large-product-placeholder.png";
$mediumProductImageName = "meduim-product-placeholder.png";
if (strpos($productImage, $mediumProductImageName) > -1) {
$productImageNameCollection = explode('/', $productImage);
$productImageName = $productImageNameCollection[sizeof($productImageNameCollection) - 1];
if ($productImageName == $mediumProductImageName) {
$productImage = str_replace($mediumProductImageName, $largeProductImageName, $productImage);
}
}
2020-07-03 06:38:20 +00:00
$priceHTML = view('shop::products.price', ['product' => $product])->render();
$isProductNew = ($product->new && ! strpos($priceHTML, 'sticker sale') > 0) ? __('shop::app.products.new') : false;
return [
2020-07-03 06:38:20 +00:00
'priceHTML' => $priceHTML,
'avgRating' => ceil($reviewHelper->getAverageRating($product)),
'totalReviews' => $reviewHelper->getTotalReviews($product),
'image' => $productImage,
2020-07-03 06:38:20 +00:00
'new' => $isProductNew,
'galleryImages' => $galleryImages,
'name' => $product->name,
'slug' => $product->url_key,
'description' => $product->description,
'shortDescription' => $product->short_description,
'firstReviewText' => trans('velocity::app.products.be-first-review'),
'addToCartHtml' => view('shop::products.add-to-cart', [
2020-03-05 08:14:34 +00:00
'product' => $product,
2020-03-04 06:32:53 +00:00
'addWishlistClass' => ! (isset($list) && $list) ? '' : '',
2020-06-19 07:46:01 +00:00
'showCompare' => core()->getConfigData('general.content.shop.compare_option') == "1"
? true : false,
2020-03-19 11:09:29 +00:00
'btnText' => (isset($metaInformation['btnText']) && $metaInformation['btnText'])
2020-03-24 05:03:03 +00:00
? $metaInformation['btnText'] : null,
2020-06-19 07:46:01 +00:00
2020-03-24 05:03:03 +00:00
'moveToCart' => (isset($metaInformation['moveToCart']) && $metaInformation['moveToCart'])
? $metaInformation['moveToCart'] : null,
2020-06-19 07:46:01 +00:00
2020-03-04 06:32:53 +00:00
'addToCartBtnClass' => ! (isset($list) && $list) ? 'small-padding' : '',
])->render(),
];
}
2020-01-26 08:47:05 +00:00
2020-03-03 15:01:43 +00:00
/**
* Returns the count rating of the product
*
2020-03-06 06:55:59 +00:00
* @param $items
* @param $separator
2020-03-03 15:01:43 +00:00
*
* @return array
2020-03-06 06:55:59 +00:00
*/
2020-03-19 11:09:29 +00:00
public function fetchProductCollection($items, $moveToCart = false, $separator='&')
2020-03-03 15:01:43 +00:00
{
$productIds = collect(explode($separator, $items));
2020-03-03 15:01:43 +00:00
return $productIds->map(function ($productId) use ($moveToCart) {
2020-03-06 06:55:59 +00:00
$productFlat = $this->productFlatRepository->findOneWhere(['id' => $productId]);
2020-03-03 15:01:43 +00:00
2020-03-06 06:55:59 +00:00
if ($productFlat) {
$formattedProduct = $this->formatProduct($productFlat, false, [
'moveToCart' => $moveToCart,
'btnText' => $moveToCart ? trans('shop::app.customer.account.wishlist.move-to-cart') : null,
]);
2020-08-17 05:58:12 +00:00
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']
]);
}
})->toArray();
2020-03-03 15:01:43 +00:00
}
}