ltsecurityinc安防产品在线商城代码
This commit is contained in:
+73
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* Resize Blog Featured Image
|
||||
*
|
||||
* @category Olegnax
|
||||
* @package Olegnax_Athlete2
|
||||
* @copyright Copyright (c) 2023 Olegnax (http://www.olegnax.com/)
|
||||
* @license https://www.olegnax.com/license
|
||||
*/
|
||||
|
||||
namespace Olegnax\Athlete2\Plugin\Blog\Model;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Olegnax\Athlete2\Helper\Helper;
|
||||
use Olegnax\Athlete2\Helper\Image;
|
||||
|
||||
class Post
|
||||
{
|
||||
|
||||
protected $imageHelper;
|
||||
protected $scopeConfig;
|
||||
/**
|
||||
* @var Helper
|
||||
*/
|
||||
private $helper;
|
||||
|
||||
public function __construct(
|
||||
Image $imageHelper,
|
||||
Helper $helper,
|
||||
ScopeConfigInterface $scopeConfig
|
||||
) {
|
||||
$this->helper = $helper;
|
||||
$this->imageHelper = $imageHelper;
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
}
|
||||
|
||||
public function aroundGetFeaturedImage(
|
||||
$subject,
|
||||
$proceed,
|
||||
$size = null,
|
||||
$attributes = ['aspect_ratio' => true, 'crop' => true,]
|
||||
) {
|
||||
if ($this->helper->isEnabled() && !empty($size)) {
|
||||
list($width, $height) = $this->prepareSize($size);
|
||||
$dataName = sprintf('featured_image_resized_%s_%s', $width, $height);
|
||||
if (!$subject->hasData($dataName)) {
|
||||
if ($file = $subject->getData('featured_img')) {
|
||||
$image = $this->imageHelper->init($file, $attributes)->adaptiveResize($size)->getUrl();
|
||||
} else {
|
||||
$image = false;
|
||||
}
|
||||
$subject->setData($dataName, $image);
|
||||
$subject->setData('featured_image', $image);
|
||||
}
|
||||
}
|
||||
|
||||
return $proceed();
|
||||
}
|
||||
|
||||
private function prepareSize($size)
|
||||
{
|
||||
if (is_array($size) && 1 >= count($size)) {
|
||||
$size = array_shift($size);
|
||||
}
|
||||
if (!is_array($size)) {
|
||||
$size = [$size, $size];
|
||||
}
|
||||
$size = array_map('intval', $size);
|
||||
$size = array_map('abs', $size);
|
||||
return $size;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Olegnax\Athlete2\Plugin\Catalog\Block\Product\View;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\View\Element\Template\Context;
|
||||
use Olegnax\Athlete2\Helper\Helper;
|
||||
|
||||
/**
|
||||
* Product details block.
|
||||
*
|
||||
* Holds a group of blocks to show as tabs.
|
||||
*
|
||||
* @api
|
||||
* @since 103.0.1
|
||||
*/
|
||||
class Details extends \Magento\Catalog\Block\Product\View\Details
|
||||
{
|
||||
/**
|
||||
* @var Helper
|
||||
*/
|
||||
protected $_helper;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
Helper $helper,
|
||||
array $data = []
|
||||
) {
|
||||
$this->_helper = $helper;
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sorted child block names.
|
||||
*
|
||||
* @param string $groupName
|
||||
* @param string $callback
|
||||
* @return array
|
||||
* @throws LocalizedException
|
||||
*
|
||||
* @since 103.0.1
|
||||
*/
|
||||
public function getGroupSortedChildNames(string $groupName, string $callback): array
|
||||
{
|
||||
if (!$this->_helper->isEnabled()) {
|
||||
return parent::getGroupSortedChildNames($groupName, $callback);
|
||||
}
|
||||
$groupChildNames = $this->getGroupChildNames($groupName, $callback);
|
||||
$layout = $this->getLayout();
|
||||
|
||||
$childNamesSortOrder = [];
|
||||
|
||||
foreach ($groupChildNames as $childName) {
|
||||
$alias = $layout->getElementAlias($childName);
|
||||
$sortOrder = (int)$this->getChildData($alias, 'sort_order') ?? 0;
|
||||
|
||||
$childNamesSortOrder[$childName] = $sortOrder;
|
||||
}
|
||||
|
||||
asort($childNamesSortOrder, SORT_NUMERIC);
|
||||
$childNamesSortOrder = array_keys($childNamesSortOrder);
|
||||
|
||||
return $childNamesSortOrder;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
namespace Olegnax\Athlete2\Plugin\Catalog\Block\Product\View;
|
||||
|
||||
use Magento\Catalog\Helper\Image;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\Product\Image\UrlBuilder;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\Data\Collection;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Olegnax\Athlete2\Helper\Helper;
|
||||
use Olegnax\Athlete2\Helper\ProductImage;
|
||||
|
||||
/**
|
||||
* Description of Gallery
|
||||
*
|
||||
* @author Master
|
||||
*/
|
||||
class Gallery
|
||||
{
|
||||
const XML_IMAGE_WIDTH = 'athlete2_settings/product_images/product_image_width';
|
||||
const XML_IMAGE_HEIGHT = 'athlete2_settings/product_images/product_image_height';
|
||||
/**
|
||||
*
|
||||
* @var ObjectManager
|
||||
*/
|
||||
public $_objectManager;
|
||||
|
||||
public function afterGetGalleryImages($subject, $result)
|
||||
{
|
||||
if (!$this->getConfig(Helper::XML_ENABLED)) {
|
||||
return $result;
|
||||
}
|
||||
$size = [(string)$this->getConfig(self::XML_IMAGE_WIDTH), (string)$this->getConfig(self::XML_IMAGE_HEIGHT)];
|
||||
$size = array_map('trim', $size);
|
||||
$size = array_filter($size);
|
||||
if (!empty($size)) {
|
||||
$product = $subject->getProduct();
|
||||
$images = $product->getMediaGalleryImages();
|
||||
if (!$images instanceof Collection) {
|
||||
return $images;
|
||||
}
|
||||
|
||||
foreach ($images as $image) {
|
||||
$galleryImagesConfig = $subject->getGalleryImagesConfig()->getItems();
|
||||
foreach ($galleryImagesConfig as $imageConfig) {
|
||||
$dataObjectKey = $imageConfig->getData('data_object_key');
|
||||
$imageUrl = '';
|
||||
$imageId = $imageConfig['image_id'];
|
||||
if ('medium_image_url' == $dataObjectKey) {
|
||||
$imageUrl = $this->getUrlResizedImage($product, $image->getFile(), $imageId, $size);
|
||||
} else {
|
||||
$imageUrl = $this->_loadObject(UrlBuilder::class)->getUrl($image->getFile(), $imageId);
|
||||
}
|
||||
$image->setData($dataObjectKey, $imageUrl);
|
||||
}
|
||||
}
|
||||
|
||||
return $images;
|
||||
}
|
||||
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getConfig($path, $storeCode = null)
|
||||
{
|
||||
return $this->getScopeConfig()->getValue($path, ScopeInterface::SCOPE_STORE, $storeCode);
|
||||
if (is_null($value)) {
|
||||
$value = '';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected function getScopeConfig()
|
||||
{
|
||||
return $this->_loadObject(ScopeConfigInterface::class);
|
||||
}
|
||||
|
||||
protected function _loadObject($object)
|
||||
{
|
||||
return $this->_getObjectManager()->get($object);
|
||||
}
|
||||
|
||||
protected function _getObjectManager()
|
||||
{
|
||||
return ObjectManager::getInstance();
|
||||
}
|
||||
|
||||
public function getUrlResizedImage(Product $product, $imageFile, $imageId, $size, $properties = [])
|
||||
{
|
||||
$image = $this->resizeImage($product, $imageFile, $imageId, $size, $properties);
|
||||
return $image->getUrl();
|
||||
}
|
||||
|
||||
public function resizeImage(Product $product, $imageFile, $imageId, $size, $properties = [])
|
||||
{
|
||||
$size = $this->prepareSize($size);
|
||||
$image = $this->_getImage($product, $imageFile, $imageId, $properties);
|
||||
$image->resize($size[0], $size[1]);
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
private function prepareSize($size)
|
||||
{
|
||||
if (is_array($size) && 1 >= count($size)) {
|
||||
$size = array_shift($size);
|
||||
}
|
||||
if (!is_array($size)) {
|
||||
$size = [$size, $size];
|
||||
}
|
||||
$size = array_map('floatval', $size);
|
||||
$size = array_map('abs', $size);
|
||||
return $size;
|
||||
}
|
||||
|
||||
private function _getImage(Product $product, $imageFile, $imageId, $properties = [])
|
||||
{
|
||||
return $this->_loadObject(Image::class)->init($product, $imageId, $properties)->setImageFile($imageFile);
|
||||
}
|
||||
|
||||
protected function getProductHelper()
|
||||
{
|
||||
return $this->_loadObject(ProductImage::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Olegnax\Athlete2\Plugin\Cms\Model\Template;
|
||||
|
||||
|
||||
use Olegnax\Athlete2\Helper\LazyLoad;
|
||||
|
||||
class Filter
|
||||
{
|
||||
|
||||
/**
|
||||
* @var LazyLoad
|
||||
*/
|
||||
protected $lazyLoad;
|
||||
|
||||
public function __construct(LazyLoad $lazyLoad)
|
||||
{
|
||||
$this->lazyLoad = $lazyLoad;
|
||||
}
|
||||
|
||||
public function afterFilter($subject, $result)
|
||||
{
|
||||
return $this->lazyLoad->replaceImageToLazy($result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Olegnax\Athlete2\Plugin\ConfigurableProduct\Helper;
|
||||
|
||||
use Exception;
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Catalog\Helper\Image as ImageHelper;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\Product\Image;
|
||||
use Magento\Catalog\Model\Product\Image\UrlBuilder;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\Data\Collection;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Olegnax\Athlete2\Helper\Helper;
|
||||
use Olegnax\Athlete2\Helper\ProductImage;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Data
|
||||
{
|
||||
const XML_IMAGE_WIDTH = 'athlete2_settings/product_images/product_image_width';
|
||||
const XML_IMAGE_HEIGHT = 'athlete2_settings/product_images/product_image_height';
|
||||
/**
|
||||
*
|
||||
* @var ObjectManager
|
||||
*/
|
||||
public $_objectManager;
|
||||
|
||||
public function aroundGetGalleryImages($subject, $proceed, ProductInterface $product)
|
||||
{
|
||||
if (!$this->getConfig(Helper::XML_ENABLED)) {
|
||||
return $proceed($product);
|
||||
}
|
||||
$size = [(string)$this->getConfig(self::XML_IMAGE_WIDTH), (string)$this->getConfig(self::XML_IMAGE_HEIGHT)];
|
||||
$size = array_map('trim', $size);
|
||||
$size = array_filter($size);
|
||||
|
||||
if (!empty($size)) {
|
||||
$images = $product->getMediaGalleryImages();
|
||||
if ($images instanceof Collection) {
|
||||
$galleryImagesConfig = [
|
||||
'small_image_url' => 'product_page_image_small',
|
||||
'medium_image_url' => 'product_page_image_medium',
|
||||
'large_image_url' => 'product_page_image_large',
|
||||
];
|
||||
/** @var $image Image */
|
||||
foreach ($images as $image) {
|
||||
foreach ($galleryImagesConfig as $dataObjectKey => $imageId) {
|
||||
$imageUrl = '';
|
||||
if ('medium_image_url' == $dataObjectKey) {
|
||||
try {
|
||||
$imageUrl = $this->getUrlResizedImage($product, $image->getFile(), $imageId, $size);
|
||||
} catch ( Exception $e) {
|
||||
$this->_loadObject( LoggerInterface::class)->error($e->getMessage());
|
||||
$imageUrl = $this->_loadObject(UrlBuilder::class)->getUrl($image->getFile(), $imageId);
|
||||
}
|
||||
} else {
|
||||
$imageUrl = $this->_loadObject(UrlBuilder::class)->getUrl($image->getFile(), $imageId);
|
||||
}
|
||||
|
||||
$image->setData($dataObjectKey, $imageUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $images;
|
||||
}
|
||||
return $proceed($product);
|
||||
}
|
||||
|
||||
public function getConfig($path, $storeCode = null)
|
||||
{
|
||||
$value = $this->getScopeConfig()->getValue($path, ScopeInterface::SCOPE_STORE, $storeCode);
|
||||
if (is_null($value)) {
|
||||
$value = '';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected function getScopeConfig()
|
||||
{
|
||||
return $this->_loadObject(ScopeConfigInterface::class);
|
||||
}
|
||||
|
||||
protected function _loadObject($object)
|
||||
{
|
||||
return ObjectManager::getInstance()->get($object);
|
||||
}
|
||||
|
||||
public function getUrlResizedImage(Product $product, $imageFile, $imageId, $size, $properties = [])
|
||||
{
|
||||
$image = $this->resizeImage($product, $imageFile, $imageId, $size, $properties);
|
||||
return $image->getUrl();
|
||||
}
|
||||
|
||||
public function resizeImage(Product $product, $imageFile, $imageId, $size, $properties = [])
|
||||
{
|
||||
$size = $this->prepareSize($size);
|
||||
$image = $this->_getImage($product, $imageFile, $imageId, $properties);
|
||||
$image->resize($size[0], $size[1]);
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
private function prepareSize($size)
|
||||
{
|
||||
if (is_array($size) && 1 >= count($size)) {
|
||||
$size = array_shift($size);
|
||||
}
|
||||
if (!is_array($size)) {
|
||||
$size = [$size, $size];
|
||||
}
|
||||
$size = array_map('floatval', $size);
|
||||
$size = array_map('abs', $size);
|
||||
return $size;
|
||||
}
|
||||
|
||||
private function _getImage(Product $product, $imageFile, $imageId, $properties = [])
|
||||
{
|
||||
return $this->_loadObject(ImageHelper::class)->init($product, $imageId, $properties)->setImageFile($imageFile);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Olegnax
|
||||
* @package Olegnax_Athlete2
|
||||
* @copyright Copyright (c) 2023 Olegnax (http://olegnax.com/). All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Olegnax\Athlete2\Plugin\Frontend\Magento\Customer\Model\Account;
|
||||
|
||||
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\App\Response\RedirectInterface;
|
||||
|
||||
class Redirect
|
||||
{
|
||||
/**
|
||||
* @var RedirectInterface
|
||||
*/
|
||||
protected $redirect;
|
||||
/**
|
||||
* @var RequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
public function __construct(
|
||||
RedirectInterface $redirect,
|
||||
RequestInterface $request
|
||||
) {
|
||||
$this->redirect = $redirect;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function afterGetRedirectCookie(
|
||||
\Magento\Customer\Model\Account\Redirect $subject,
|
||||
$result
|
||||
) {
|
||||
if (empty($result)) {
|
||||
$login = $this->request->getPost('login_redirect');
|
||||
if ('referer' === $login) {
|
||||
$result = $this->redirect->getRefererUrl();
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace Olegnax\Athlete2\Plugin\Frontend\Magento\Swatches\Block\Product\Renderer\Listing;
|
||||
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
|
||||
use Magento\Swatches\Block\Product\Renderer\Listing\Configurable as Subject;
|
||||
use Magento\Swatches\Helper\Data as SwatchData;
|
||||
|
||||
class Configurable {
|
||||
/**
|
||||
* @var JsonSerializer
|
||||
*/
|
||||
protected $jsonSerializer;
|
||||
/**
|
||||
* @var Subject
|
||||
*/
|
||||
protected $subject;
|
||||
/**
|
||||
* @var SwatchData
|
||||
*/
|
||||
protected $swatchHelper;
|
||||
|
||||
public function __construct(
|
||||
SwatchData $swatchHelper,
|
||||
JsonSerializer $serializer
|
||||
) {
|
||||
|
||||
|
||||
$this->swatchHelper = $swatchHelper;
|
||||
$this->jsonSerializer = $serializer;
|
||||
}
|
||||
|
||||
public function afterGetJsonConfig(
|
||||
Subject $subject,
|
||||
$result
|
||||
) {
|
||||
if ( ! empty( $subject->getRequest()->getQuery()->toArray() ) ) {
|
||||
$data = $this->jsonSerializer->unserialize( $result );
|
||||
if ( isset( $data['preSelectedGallery'] ) ) {
|
||||
if ( empty( $data['preSelectedGallery'] ) ) {
|
||||
$data['preSelectedGallery'] = false;
|
||||
} else {
|
||||
$this->subject = $subject;
|
||||
$data['preSelectedId'] = $this->getProductVariation(
|
||||
$subject->getProduct(),
|
||||
$subject->getRequest()->getQuery()->toArray()
|
||||
);
|
||||
}
|
||||
}
|
||||
$result = $this->jsonSerializer->serialize( $data );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getProductVariation(
|
||||
Product $configurableProduct,
|
||||
array $additionalAttributes = []
|
||||
) {
|
||||
$configurableAttributes = $this->getLayeredAttributesIfExists($configurableProduct, $additionalAttributes);
|
||||
if (!$configurableAttributes) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$product = $this->swatchHelper->loadVariationByFallback($configurableProduct, $configurableAttributes);
|
||||
|
||||
return $product ? $product->getId() : 0;
|
||||
}
|
||||
private function getLayeredAttributesIfExists(Product $configurableProduct, array $additionalAttributes)
|
||||
{
|
||||
$configurableAttributes = $this->swatchHelper->getAttributesFromConfigurable($configurableProduct);
|
||||
|
||||
$layeredAttributes = [];
|
||||
|
||||
$configurableAttributes = array_map(
|
||||
function ($attribute) {
|
||||
return $attribute->getAttributeCode();
|
||||
},
|
||||
$configurableAttributes
|
||||
);
|
||||
|
||||
$commonAttributeCodes = array_intersect(
|
||||
$configurableAttributes,
|
||||
array_keys($additionalAttributes)
|
||||
);
|
||||
|
||||
foreach ($commonAttributeCodes as $attributeCode) {
|
||||
$layeredAttributes[$attributeCode] = $additionalAttributes[$attributeCode];
|
||||
}
|
||||
|
||||
return $layeredAttributes;
|
||||
}
|
||||
}
|
||||
+208
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Olegnax\Athlete2\Plugin\Frontend\Magento\Swatches\Helper;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface;
|
||||
use Magento\Catalog\Helper\Image as ImageHelper;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\Product as ModelProduct;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\Swatches\Helper\Data as MagentoData;
|
||||
use Olegnax\Athlete2\Helper\Helper;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Data
|
||||
{
|
||||
|
||||
const XML_PRODUCT_IMAGE_WIDTH = 'athlete2_settings/product_images/product_image_width';
|
||||
const XML_PRODUCT_IMAGE_HEIGHT = 'athlete2_settings/product_images/product_image_height';
|
||||
const XML_IMAGE_WIDTH = 'athlete2_settings/product_images/listing_image_width';
|
||||
const XML_IMAGE_HEIGHT = 'athlete2_settings/product_images/listing_image_height';
|
||||
/**
|
||||
* @var ImageHelper
|
||||
*/
|
||||
protected $imageHelper;
|
||||
/**
|
||||
* @var ScopeConfigInterface
|
||||
*/
|
||||
protected $scopeConfig;
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
public function __construct(
|
||||
ImageHelper $imageHelper,
|
||||
ScopeConfigInterface $scopeConfig,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->imageHelper = $imageHelper;
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function aroundGetProductMediaGallery(
|
||||
MagentoData $subject,
|
||||
Closure $proceed,
|
||||
ModelProduct $product
|
||||
) {
|
||||
$result = $proceed($product);
|
||||
if (!$this->getConfig(Helper::XML_ENABLED)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$mediaGallery = $product->getMediaGalleryEntries();
|
||||
foreach ($mediaGallery as $mediaEntry) {
|
||||
if ($mediaEntry->isDisabled() || !isset($result['gallery'][$mediaEntry->getId()])) {
|
||||
continue;
|
||||
}
|
||||
$result['gallery'][$mediaEntry->getId()]['isHover'] = $this->isHoverImage($mediaEntry);
|
||||
}
|
||||
$size = [(string)$this->getConfig(self::XML_IMAGE_WIDTH), (string)$this->getConfig(self::XML_IMAGE_HEIGHT)];
|
||||
$size = array_map('trim', $size);
|
||||
$size = array_filter($size);
|
||||
|
||||
$size2 = [(string)$this->getConfig(self::XML_PRODUCT_IMAGE_WIDTH), (string)$this->getConfig(self::XML_PRODUCT_IMAGE_HEIGHT)];
|
||||
$size2 = array_map('trim', $size2);
|
||||
$size2 = array_filter($size2);
|
||||
|
||||
if (empty($size) && empty($size2)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$baseImage = null;
|
||||
/** @var ProductAttributeMediaGalleryEntryInterface $mediaEntry */
|
||||
foreach ($mediaGallery as $mediaEntry) {
|
||||
if ($mediaEntry->isDisabled()) {
|
||||
continue;
|
||||
}
|
||||
if (!$baseImage || $this->isMainImage($mediaEntry)) {
|
||||
$baseImage = $mediaEntry;
|
||||
}
|
||||
|
||||
if (!empty($size)) {
|
||||
try {
|
||||
$imageUrl = $this->getUrlResizedImage(
|
||||
$product,
|
||||
$mediaEntry->getFile(),
|
||||
'product_page_image_medium',
|
||||
$size
|
||||
);
|
||||
$result['gallery'][$mediaEntry->getId()]['medium_resized'] = $imageUrl;
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($size2)) {
|
||||
try {
|
||||
$imageUrl = $this->getUrlResizedImage(
|
||||
$product,
|
||||
$mediaEntry->getFile(),
|
||||
'product_page_image_medium',
|
||||
$size2
|
||||
);
|
||||
$result['gallery'][$mediaEntry->getId()]['medium'] = $imageUrl;
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$baseImage) {
|
||||
return [];
|
||||
}
|
||||
if (!empty($size)) {
|
||||
try {
|
||||
$imageUrl = $this->getUrlResizedImage(
|
||||
$product,
|
||||
$baseImage->getFile(),
|
||||
'product_swatch_image_medium',
|
||||
$size
|
||||
);
|
||||
$result['medium_resized'] = $imageUrl;
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
if (!empty($size2)) {
|
||||
try {
|
||||
$imageUrl = $this->getUrlResizedImage(
|
||||
$product,
|
||||
$baseImage->getFile(),
|
||||
'product_page_image_medium',
|
||||
$size2
|
||||
);
|
||||
$result['medium'] = $imageUrl;
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getConfig($path, $storeCode = null)
|
||||
{
|
||||
$value = $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeCode);
|
||||
if (is_null($value)) {
|
||||
$value = '';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if image is main image in gallery
|
||||
*
|
||||
* @param ProductAttributeMediaGalleryEntryInterface $mediaEntry
|
||||
* @return bool
|
||||
*/
|
||||
private function isMainImage(ProductAttributeMediaGalleryEntryInterface $mediaEntry): bool
|
||||
{
|
||||
return in_array('image', $mediaEntry->getTypes(), true);
|
||||
}
|
||||
|
||||
public function getUrlResizedImage(Product $product, $imageFile, $imageId, $size, $properties = [])
|
||||
{
|
||||
$image = $this->resizeImage($product, $imageFile, $imageId, $size, $properties);
|
||||
return $image->getUrl();
|
||||
}
|
||||
|
||||
public function resizeImage(Product $product, $imageFile, $imageId, $size, $properties = [])
|
||||
{
|
||||
$size = $this->prepareSize($size);
|
||||
$image = $this->_getImage($product, $imageFile, $imageId, $properties);
|
||||
$image->resize($size[0], $size[1]);
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
private function prepareSize($size)
|
||||
{
|
||||
if (is_array($size) && 1 >= count($size)) {
|
||||
$size = array_shift($size);
|
||||
}
|
||||
if (!is_array($size)) {
|
||||
$size = [$size, $size];
|
||||
}
|
||||
$size = array_map('floatval', $size);
|
||||
$size = array_map('abs', $size);
|
||||
return $size;
|
||||
}
|
||||
|
||||
private function _getImage(Product $product, $imageFile, $imageId, $properties = [])
|
||||
{
|
||||
return $this->imageHelper->init($product, $imageId, $properties)->setImageFile($imageFile);
|
||||
}
|
||||
|
||||
private function isHoverImage(ProductAttributeMediaGalleryEntryInterface $mediaEntry): bool
|
||||
{
|
||||
return in_array('img_hover', $mediaEntry->getTypes(), true);
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Olegnax\Athlete2\Plugin\Frontend\Olegnax\Core\Helper;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Magento\Catalog\Helper\Image;
|
||||
use Olegnax\Athlete2\Helper\Image as Athlete2Image;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
|
||||
use Magento\Framework\View\ConfigInterface;
|
||||
use Olegnax\Athlete2\Helper\Video;
|
||||
use Olegnax\Core\Helper\ProductImage as CoreProductImage;
|
||||
|
||||
class ProductImage
|
||||
{ const PLACEHOLDERE = 'athlete2/placeholder.png';
|
||||
/**
|
||||
* @var Video
|
||||
*/
|
||||
protected $_videoHelper;
|
||||
/**
|
||||
* @var Athlete2Image
|
||||
*/
|
||||
protected $_imageHelper;
|
||||
|
||||
public function __construct(
|
||||
Athlete2Image $imageHelper,
|
||||
Video $videoHelper
|
||||
) {
|
||||
$this->_videoHelper = $videoHelper;
|
||||
$this->_imageHelper = $imageHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CoreProductImage $subject
|
||||
* @param Closure $proceed
|
||||
* @param Product $product
|
||||
* @param $imageId
|
||||
* @param $imageId_hover
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function aroundHasHoverImage(
|
||||
CoreProductImage $subject,
|
||||
Closure $proceed,
|
||||
Product $product,
|
||||
$imageId,
|
||||
$imageId_hover
|
||||
) {
|
||||
return $this->_videoHelper->videoOnHover($product) || $proceed($product, $imageId, $imageId_hover);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CoreProductImage $subject
|
||||
* @param Closure $proceed
|
||||
* @param Product $product
|
||||
* @param $imageId
|
||||
* @param $size
|
||||
* @param $properties
|
||||
*
|
||||
* @return Image|Athlete2Image
|
||||
*/
|
||||
public function aroundResizeImage(
|
||||
CoreProductImage $subject,
|
||||
Closure $proceed,
|
||||
Product $product,
|
||||
$imageId,
|
||||
$size,
|
||||
$properties = []
|
||||
) {
|
||||
$_imageId = $subject->getImageParams($imageId);
|
||||
if ($this->_videoHelper->videoOnHover($product)
|
||||
&& 'img_hover' === $_imageId['image_type']
|
||||
) {
|
||||
$result = $this->getPlaceholderHelper();
|
||||
} else {
|
||||
$result = $proceed($product, $imageId, $size, $properties);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Athlete2Image
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getPlaceholderHelper()
|
||||
{
|
||||
return $this->_imageHelper->init(
|
||||
static::PLACEHOLDERE,
|
||||
[
|
||||
'quality' => 1,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user