ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Improved Sorting for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\Sorting\Model\Method;
|
||||
|
||||
use Amasty\Sorting\Model\ConfigProvider;
|
||||
use Amasty\Sorting\Model\MethodProvider;
|
||||
use Magento\Catalog\Model\ResourceModel\Product\Collection;
|
||||
|
||||
class ApplyGlobalSorting
|
||||
{
|
||||
private const FLAG_NAME = 'global_sorting';
|
||||
|
||||
/**
|
||||
* @var ConfigProvider
|
||||
*/
|
||||
private $configProvider;
|
||||
|
||||
/**
|
||||
* @var MethodProvider
|
||||
*/
|
||||
private $methodProvider;
|
||||
|
||||
/**
|
||||
* @var IsAvailableForSorting
|
||||
*/
|
||||
private $isAvailableForSorting;
|
||||
|
||||
public function __construct(
|
||||
ConfigProvider $configProvider,
|
||||
MethodProvider $methodProvider,
|
||||
IsAvailableForSorting $isAvailableForSorting
|
||||
) {
|
||||
$this->configProvider = $configProvider;
|
||||
$this->methodProvider = $methodProvider;
|
||||
$this->isAvailableForSorting = $isAvailableForSorting;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply global sorting if needed to collection.
|
||||
* Set flag in collection for prevent repeated applying.
|
||||
* Resolve custom amasty sorting methods with our plugin
|
||||
* For mysql needed instantly applying with addAttributeToSort.
|
||||
*
|
||||
* @see \Amasty\Sorting\Plugin\Catalog\Product\Collection::beforeSetOrder
|
||||
*/
|
||||
public function execute(Collection $collection): void
|
||||
{
|
||||
$attribute = $this->configProvider->getGlobalSorting();
|
||||
if (!$attribute || !$this->isAvailableForSorting->execute($attribute)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$collection->getFlag(self::FLAG_NAME)) {
|
||||
$direction = $this->configProvider->getGlobalSortingDirection();
|
||||
|
||||
$collection->setOrder($attribute, $direction); // trigger resolving amasty sorting methods
|
||||
if ($method = $this->methodProvider->getMethodByCode($attribute)) {
|
||||
$attribute = $method->getAlias(); // resolve joined attribute for amasty sorting method
|
||||
}
|
||||
$collection->addAttributeToSort($attribute, $direction); // trigger instantly apply for mysql
|
||||
|
||||
$collection->setFlag(self::FLAG_NAME, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Improved Sorting for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\Sorting\Model\Method;
|
||||
|
||||
use Amasty\Sorting\Model\ConfigProvider;
|
||||
use Amasty\Sorting\Model\MethodProvider;
|
||||
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
|
||||
/**
|
||||
* Provide attribute codes used for sorting by amasty sorting.
|
||||
* This attributes must be added for indexation.
|
||||
*/
|
||||
class GetAttributeCodesForSorting
|
||||
{
|
||||
/**
|
||||
* @var ConfigProvider
|
||||
*/
|
||||
private $configProvider;
|
||||
|
||||
/**
|
||||
* @var MethodProvider
|
||||
*/
|
||||
private $methodProvider;
|
||||
|
||||
/**
|
||||
* @var ProductAttributeRepositoryInterface
|
||||
*/
|
||||
private $productAttributeRepository;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $attributeCodesForSorting;
|
||||
|
||||
public function __construct(
|
||||
ConfigProvider $configProvider,
|
||||
MethodProvider $methodProvider,
|
||||
ProductAttributeRepositoryInterface $productAttributeRepository
|
||||
) {
|
||||
$this->configProvider = $configProvider;
|
||||
$this->methodProvider = $methodProvider;
|
||||
$this->productAttributeRepository = $productAttributeRepository;
|
||||
}
|
||||
|
||||
public function execute(): array
|
||||
{
|
||||
if ($this->attributeCodesForSorting === null) {
|
||||
$this->attributeCodesForSorting = $this->getAttributeCodes();
|
||||
}
|
||||
|
||||
return $this->attributeCodesForSorting;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if eav attribute exists.
|
||||
*
|
||||
* Detect if passed attribute code need be processed with magento.
|
||||
* If amasty method exist with same code, amasty method more priority and processed by Sorting module.
|
||||
*/
|
||||
private function isEavAttribute(string $sortAttributeCode): bool
|
||||
{
|
||||
if ($this->methodProvider->getMethodByCode($sortAttributeCode) !== null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->productAttributeRepository->get($sortAttributeCode);
|
||||
$result = true;
|
||||
} catch (NoSuchEntityException $e) {
|
||||
$result = false;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getAttributeCodes(): array
|
||||
{
|
||||
$attributes = [
|
||||
'created_at',
|
||||
'small_image',
|
||||
$this->configProvider->getBestsellerAttributeCode(),
|
||||
$this->configProvider->getRevenueAttributeCode(),
|
||||
$this->configProvider->getMostviewedAttributeCode(),
|
||||
$this->configProvider->getNewAttributeCode(),
|
||||
$this->configProvider->getGlobalSorting()
|
||||
];
|
||||
|
||||
return array_filter($attributes, function (?string $attribute) {
|
||||
return $attribute && $this->isEavAttribute($attribute);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Improved Sorting for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\Sorting\Model\Method;
|
||||
|
||||
use Magento\Catalog\Model\Config as CatalogConfig;
|
||||
|
||||
/**
|
||||
* Check if passed code is amasty method or attribute used for sorting.
|
||||
*/
|
||||
class IsAvailableForSorting
|
||||
{
|
||||
/**
|
||||
* @var CatalogConfig
|
||||
*/
|
||||
private $catalogConfig;
|
||||
|
||||
public function __construct(CatalogConfig $catalogConfig)
|
||||
{
|
||||
$this->catalogConfig = $catalogConfig;
|
||||
}
|
||||
|
||||
public function execute(string $sortingCode): bool
|
||||
{
|
||||
return array_key_exists($sortingCode, $this->catalogConfig->getAttributesUsedForSortBy());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Improved Sorting for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\Sorting\Model\Method;
|
||||
|
||||
use Amasty\Sorting\Model\ConfigProvider;
|
||||
|
||||
class IsMethodDisabledByConfig
|
||||
{
|
||||
/**
|
||||
* @var ConfigProvider
|
||||
*/
|
||||
private $configProvider;
|
||||
|
||||
public function __construct(ConfigProvider $configProvider)
|
||||
{
|
||||
$this->configProvider = $configProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is method disabled.
|
||||
*
|
||||
* @param string $methodCode
|
||||
* @param int|null $storeId
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(string $methodCode, ?int $storeId = null): bool
|
||||
{
|
||||
$result = false;
|
||||
foreach ($this->configProvider->getDisabledMethods($storeId) as $disabledCode) {
|
||||
if ($disabledCode === $methodCode) {
|
||||
$result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Improved Sorting for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\Sorting\Model\Method;
|
||||
|
||||
use Amasty\Sorting\Model\Plugin\Catalog\Config\DisplayAllFlag;
|
||||
|
||||
class IsMethodDisplayed
|
||||
{
|
||||
/**
|
||||
* @var DisplayAllFlag
|
||||
*/
|
||||
private $displayAllFlag;
|
||||
|
||||
/**
|
||||
* @var IsMethodDisabledByConfig
|
||||
*/
|
||||
private $isMethodDisabledByConfig;
|
||||
|
||||
public function __construct(
|
||||
DisplayAllFlag $displayAllFlag,
|
||||
IsMethodDisabledByConfig $isMethodDisabledByConfig
|
||||
) {
|
||||
$this->displayAllFlag = $displayAllFlag;
|
||||
$this->isMethodDisabledByConfig = $isMethodDisabledByConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $methodCode
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(string $methodCode): bool
|
||||
{
|
||||
return $this->displayAllFlag->get()
|
||||
|| !$this->isMethodDisabledByConfig->execute($methodCode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Improved Sorting for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\Sorting\Model\Method;
|
||||
|
||||
use Amasty\Sorting\Model\ConfigProvider;
|
||||
|
||||
class IsMethodEnabled
|
||||
{
|
||||
/**
|
||||
* @var ConfigProvider
|
||||
*/
|
||||
private $configProvider;
|
||||
|
||||
/**
|
||||
* @var IsMethodDisabledByConfig
|
||||
*/
|
||||
private $isMethodDisabledByConfig;
|
||||
|
||||
public function __construct(
|
||||
IsMethodDisabledByConfig $isMethodDisabledByConfig,
|
||||
ConfigProvider $configProvider
|
||||
) {
|
||||
$this->configProvider = $configProvider;
|
||||
$this->isMethodDisabledByConfig = $isMethodDisabledByConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is method enabled for indexation & applying.
|
||||
*
|
||||
* @param string $methodCode
|
||||
* @param int|null $storeId
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(string $methodCode, ?int $storeId = null): bool
|
||||
{
|
||||
return !$this->isMethodDisabledByConfig->execute($methodCode, $storeId)
|
||||
|| in_array($methodCode, $this->configProvider->getDefaultSortingSearchPages($storeId))
|
||||
|| in_array($methodCode, $this->configProvider->getDefaultSortingCategoryPages($storeId))
|
||||
|| $methodCode === $this->configProvider->getGlobalSorting();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user