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

383 lines
13 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;
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;
2020-03-05 13:37:08 +00:00
use Webkul\Attribute\Repositories\AttributeOptionRepository;
2020-01-26 08:47:05 +00:00
use Webkul\Product\Repositories\ProductReviewRepository;
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;
$this->orderBrandsRepository->create([
'order_item_id' => $orderItem->id,
2020-03-19 11:09:29 +00:00
'order_id' => $orderItem->order_id,
2020-02-20 07:38:52 +00:00
'product_id' => $orderItem->product_id,
'brand' => $products[$key]->brand,
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
* @param \Webkul\Product\Contracts\Product $product
2020-03-19 11:09:29 +00:00
*
2020-03-05 13:37:08 +00:00
* @return int
*/
2020-01-26 08:47:05 +00:00
public function getCountRating($product)
{
2020-02-27 08:03:03 +00:00
$reviews = $product->reviews()
->where('status', 'approved')
->select('rating', DB::raw('count(*) as total'))
->groupBy('rating')
->orderBy('rating','desc')
->get();
2020-01-26 08:47:05 +00:00
$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;
}
2020-03-05 13:37:08 +00:00
/**
* Returns the count rating of the product
*
* @return array
*/
2020-01-26 08:47:05 +00:00
public function getVelocityMetaData()
{
try {
$metaData = $this->velocityMetadataRepository->get();
if (! ($metaData && isset($metaData[0]) && $metaData = $metaData[0])) {
$metaData = null;
}
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-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');
$productImageHelper = app('Webkul\Product\Helpers\ProductImage');
$totalReviews = $reviewHelper->getTotalReviews($product);
$avgRatings = ceil($reviewHelper->getAverageRating($product));
$galleryImages = $productImageHelper->getGalleryImages($product);
$productImage = $productImageHelper->getProductBaseImage($product)['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);
}
}
return [
'avgRating' => $avgRatings,
'totalReviews' => $totalReviews,
'image' => $productImage,
'galleryImages' => $galleryImages,
'name' => $product->name,
'slug' => $product->url_key,
'description' => $product->description,
'shortDescription' => $product->short_description,
'firstReviewText' => trans('velocity::app.products.be-first-review'),
'priceHTML' => view('shop::products.price', ['product' => $product])->render(),
2020-03-25 07:40:33 +00:00
'defaultAddToCart' => view('shop::products.add-buttons', ['product' => $product])->render(),
'addToCartHtml' => view('shop::products.add-to-cart', [
'showCompare' => true,
2020-03-05 08:14:34 +00:00
'product' => $product,
2020-03-04 06:32:53 +00:00
'addWishlistClass' => ! (isset($list) && $list) ? '' : '',
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,
'moveToCart' => (isset($metaInformation['moveToCart']) && $metaInformation['moveToCart'])
? $metaInformation['moveToCart'] : null,
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
{
$productCollection = [];
2020-03-06 06:55:59 +00:00
$productIds = explode($separator, $items);
2020-03-03 15:01:43 +00:00
2020-03-06 06:55:59 +00:00
foreach ($productIds as $productId) {
// @TODO:- query only once insted of 2
$productFlat = $this->productFlatRepository->findOneWhere(['id' => $productId]);
2020-03-03 15:01:43 +00:00
2020-03-06 06:55:59 +00:00
if ($productFlat) {
$product = $this->productRepository->findOneWhere(['id' => $productFlat->product_id]);
2020-03-03 15:01:43 +00:00
2020-03-06 06:55:59 +00:00
if ($product) {
2020-03-19 11:09:29 +00:00
$formattedProduct = $this->formatProduct($productFlat, false, [
'moveToCart' => $moveToCart,
'btnText' => $moveToCart ? trans('shop::app.customer.account.wishlist.move-to-cart') : null,
]);
2020-03-03 15:01:43 +00:00
2020-03-06 06:55:59 +00:00
$productMetaDetails = [];
$productMetaDetails['slug'] = $product->url_key;
$productMetaDetails['image'] = $formattedProduct['image'];
$productMetaDetails['priceHTML'] = $formattedProduct['priceHTML'];
$productMetaDetails['addToCartHtml'] = $formattedProduct['addToCartHtml'];
$productMetaDetails['galleryImages'] = $formattedProduct['galleryImages'];
2020-03-25 07:40:33 +00:00
$productMetaDetails['defaultAddToCart'] = $formattedProduct['defaultAddToCart'];
2020-03-03 15:01:43 +00:00
$product = array_merge($productFlat->toArray(), $productMetaDetails);
2020-03-06 06:55:59 +00:00
array_push($productCollection, $product);
}
2020-03-03 15:01:43 +00:00
}
}
return $productCollection;
}
}