ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\Backend\Model\Menu;
|
||||
|
||||
class Item
|
||||
{
|
||||
/**
|
||||
* @param \Magento\Backend\Model\Menu\Item $subject
|
||||
* @param string $result
|
||||
* @return mixed
|
||||
*/
|
||||
public function afterGetUrl(
|
||||
\Magento\Backend\Model\Menu\Item $subject,
|
||||
$result
|
||||
) {
|
||||
/* hack for having correct url key ( we cant add params in menu.xml file)*/
|
||||
if ($subject->getId() == 'Amasty_ElasticSearch::settings') {
|
||||
$find = 'admin/system_config/edit';
|
||||
$result = str_replace($find, $find . '/section/amasty_elastic', $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\Catalog\Model;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Indexer\RelevanceRule\ProductRuleProcessor;
|
||||
use Magento\Catalog\Model\Category;
|
||||
|
||||
class CategoryPlugin
|
||||
{
|
||||
/**
|
||||
* @var ProductRuleProcessor
|
||||
*/
|
||||
private $productRuleProcessor;
|
||||
|
||||
public function __construct(ProductRuleProcessor $productRuleProcessor)
|
||||
{
|
||||
$this->productRuleProcessor = $productRuleProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Category $subject
|
||||
* @param Category $result
|
||||
* @return Category
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function afterSave(Category $subject, Category $result)
|
||||
{
|
||||
$productIds = $result->getAffectedProductIds();
|
||||
if ($productIds && !$this->productRuleProcessor->isIndexerScheduled()) {
|
||||
$this->productRuleProcessor->reindexList($productIds);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Category $subject
|
||||
* @param Category $result
|
||||
* @return Category
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function afterDelete(Category $subject, Category $result)
|
||||
{
|
||||
$this->productRuleProcessor->markIndexerAsInvalid();
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\Catalog\Model;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Indexer\RelevanceRule\ProductRuleProcessor;
|
||||
use Magento\Catalog\Model\Product;
|
||||
|
||||
class ProductPlugin
|
||||
{
|
||||
/**
|
||||
* @var ProductRuleProcessor
|
||||
*/
|
||||
private $productRuleProcessor;
|
||||
|
||||
/**
|
||||
* @param ProductRuleProcessor $productRuleProcessor
|
||||
*/
|
||||
public function __construct(ProductRuleProcessor $productRuleProcessor)
|
||||
{
|
||||
$this->productRuleProcessor = $productRuleProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply catalog rules after product resource model save
|
||||
*
|
||||
* @param Product $subject
|
||||
* @return void
|
||||
*/
|
||||
public function afterReindex(Product $subject)
|
||||
{
|
||||
$this->productRuleProcessor->reindexRow($subject->getId());
|
||||
}
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\Catalog\Model\ResourceModel;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Indexer\RelevanceRule\RuleProductProcessor;
|
||||
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
|
||||
use Amasty\ElasticSearch\Model\ResourceModel\RelevanceRule\CollectionFactory as RuleCollectionFactory;
|
||||
use Amasty\ElasticSearch\Model\ResourceModel\RelevanceRule\Collection;
|
||||
use Amasty\ElasticSearch\Model\RelevanceRule;
|
||||
use Magento\CatalogRule\Model\Rule\Condition\Combine;
|
||||
use Magento\Framework\Message\ManagerInterface;
|
||||
use Magento\Rule\Model\Condition\Product\AbstractProduct;
|
||||
|
||||
class EavAttribute
|
||||
{
|
||||
/**
|
||||
* @var RuleCollectionFactory
|
||||
*/
|
||||
private $ruleCollectionFactory;
|
||||
|
||||
/**
|
||||
* @var RuleProductProcessor
|
||||
*/
|
||||
private $ruleProductProcessor;
|
||||
|
||||
/**
|
||||
* @var ManagerInterface
|
||||
*/
|
||||
private $messageManager;
|
||||
|
||||
public function __construct(
|
||||
RuleCollectionFactory $ruleCollectionFactory,
|
||||
RuleProductProcessor $ruleProductProcessor,
|
||||
ManagerInterface $messageManager
|
||||
) {
|
||||
$this->ruleCollectionFactory = $ruleCollectionFactory;
|
||||
$this->ruleProductProcessor = $ruleProductProcessor;
|
||||
$this->messageManager = $messageManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Attribute $subject
|
||||
* @param Attribute $attribute
|
||||
* @return Attribute
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function afterSave(Attribute $subject, Attribute $attribute)
|
||||
{
|
||||
if ($attribute->dataHasChangedFor('is_used_for_promo_rules') && !$attribute->getIsUsedForPromoRules()) {
|
||||
$this->checkRelativeRulesAvailability($attribute->getAttributeCode());
|
||||
}
|
||||
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Attribute $subject
|
||||
* @param Attribute $attribute
|
||||
* @return Attribute
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function afterDelete(Attribute $subject, Attribute $attribute)
|
||||
{
|
||||
if ($attribute->getIsUsedForPromoRules()) {
|
||||
$this->checkRelativeRulesAvailability($attribute->getAttributeCode());
|
||||
}
|
||||
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $attributeCode
|
||||
* @return $this
|
||||
*/
|
||||
private function checkRelativeRulesAvailability($attributeCode)
|
||||
{
|
||||
/* @var $collection Collection */
|
||||
$collection = $this->ruleCollectionFactory->create()->addAttributeInConditionFilter($attributeCode);
|
||||
$disabledRulesCount = 0;
|
||||
foreach ($collection as $rule) {
|
||||
/* @var $rule RelevanceRule */
|
||||
$rule->setIsEnabled(false);
|
||||
$this->removeAttributeFromConditions($rule->getCatalogRule()->getConditions(), $attributeCode);
|
||||
$rule->setConditions($rule->getConditionsSerialized());
|
||||
$rule->save();
|
||||
$disabledRulesCount++;
|
||||
}
|
||||
|
||||
if ($disabledRulesCount) {
|
||||
$this->ruleProductProcessor->markIndexerAsInvalid();
|
||||
$this->messageManager->addWarning(
|
||||
__(
|
||||
'You disabled %1 ElasticSearch Relevance Rules based on "%2" attribute.',
|
||||
$disabledRulesCount,
|
||||
$attributeCode
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Combine $combine
|
||||
* @param string $attributeCode
|
||||
* @return void
|
||||
*/
|
||||
private function removeAttributeFromConditions(Combine $combine, $attributeCode)
|
||||
{
|
||||
$conditions = $combine->getConditions();
|
||||
foreach ($conditions as $conditionId => $condition) {
|
||||
if ($condition instanceof Combine) {
|
||||
$this->removeAttributeFromConditions($condition, $attributeCode);
|
||||
}
|
||||
|
||||
if ($condition instanceof AbstractProduct) {
|
||||
if ($condition->getAttribute() == $attributeCode) {
|
||||
unset($conditions[$conditionId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$combine->setConditions($conditions);
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\Catalog\Model\ResourceModel;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Indexer\RelevanceRule\ProductRuleProcessor;
|
||||
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
|
||||
class Product
|
||||
{
|
||||
/**
|
||||
* @var ProductRuleProcessor
|
||||
*/
|
||||
private $productRuleProcessor;
|
||||
|
||||
public function __construct(ProductRuleProcessor $productRuleProcessor)
|
||||
{
|
||||
$this->productRuleProcessor = $productRuleProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ProductResource $subject
|
||||
* @param \Clousure $proceed
|
||||
* @param \Magento\Framework\Model\AbstractModel $product
|
||||
* @return \Magento\Catalog\Model\ResourceModel\Product
|
||||
*/
|
||||
public function aroundSave(
|
||||
ProductResource $subject,
|
||||
\Closure $proceed,
|
||||
\Magento\Framework\Model\AbstractModel $product
|
||||
) {
|
||||
$productResource = $proceed($product);
|
||||
if (!$product->getIsMassupdate()) {
|
||||
$this->productRuleProcessor->reindexRow($product->getId());
|
||||
}
|
||||
|
||||
return $productResource;
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\CatalogRule\Model\ResourceModel\Product\ConditionsToCollectionApplier;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Rule\Condition\CustomConditionInterface;
|
||||
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
|
||||
use Magento\CatalogRule\Model\ResourceModel\Product\ConditionsToCollectionApplier;
|
||||
use Magento\CatalogRule\Model\Rule\Condition\Combine;
|
||||
use Magento\CatalogRule\Model\Rule\Condition\MappableConditionsProcessor;
|
||||
|
||||
/**
|
||||
* Remove our custom bestseller condition because Magento when forming product collection validates
|
||||
* custom condition types and throws an error if they do not match.
|
||||
* @see MappableConditionsProcessor::rebuildCombinedCondition
|
||||
*/
|
||||
class DisableValidateCondition
|
||||
{
|
||||
/**
|
||||
* @see ConditionsToCollectionApplier::applyConditionsToCollection()
|
||||
*
|
||||
* @param ConditionsToCollectionApplier $subject
|
||||
* @param Combine $conditions
|
||||
* @param ProductCollection $productCollection
|
||||
* @return array
|
||||
*/
|
||||
public function beforeApplyConditionsToCollection(
|
||||
ConditionsToCollectionApplier $subject,
|
||||
Combine $conditions,
|
||||
ProductCollection $productCollection
|
||||
): array {
|
||||
$newConditions = clone $conditions;
|
||||
$this->removeOurConditions($newConditions);
|
||||
|
||||
return [$newConditions, $productCollection];
|
||||
}
|
||||
|
||||
private function removeOurConditions(Combine $conditions): void
|
||||
{
|
||||
$conditionsArray = [];
|
||||
|
||||
foreach ($conditions->getConditions() as $condition) {
|
||||
if (!$condition instanceof CustomConditionInterface) {
|
||||
if ($condition instanceof Combine) {
|
||||
$newCondition = clone $condition;
|
||||
$this->removeOurConditions($newCondition);
|
||||
$condition = $newCondition;
|
||||
}
|
||||
|
||||
$conditionsArray[] = $condition;
|
||||
}
|
||||
}
|
||||
|
||||
$conditions->setConditions($conditionsArray);
|
||||
}
|
||||
}
|
||||
Executable
+59
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\CatalogRuleConfigurable;
|
||||
|
||||
use Magento\CatalogRule\Model\Rule;
|
||||
use Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\Rule\ConfigurableProductHandler
|
||||
as CatalogRuleConfigurableHandler;
|
||||
|
||||
/**
|
||||
* Disabling addVariationsToProductRule original plugin by using same name, and calling it by condition
|
||||
*/
|
||||
class ConfigurableProductHandler
|
||||
{
|
||||
/**
|
||||
* @var CatalogRuleConfigurableHandler
|
||||
*/
|
||||
private $parentHandler;
|
||||
|
||||
public function __construct(
|
||||
CatalogRuleConfigurableHandler $parentHandler
|
||||
) {
|
||||
$this->parentHandler = $parentHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \Magento\CatalogRule\Model\Rule::getMatchingProductIds()
|
||||
*
|
||||
* @param Rule $rule
|
||||
* @param \Closure $proceed
|
||||
* @return array
|
||||
*/
|
||||
public function aroundGetMatchingProductIds(Rule $rule, \Closure $proceed): array
|
||||
{
|
||||
$productIds = null;
|
||||
|
||||
if (!$rule->getAmastyRelevanceRule()) {
|
||||
if (method_exists($this->parentHandler, 'aroundGetMatchingProductIds')) {
|
||||
$productIds = $this->parentHandler->aroundGetMatchingProductIds($rule, $proceed);
|
||||
} elseif (method_exists($this->parentHandler, 'afterGetMatchingProductIds')) {
|
||||
$productIds = $proceed();
|
||||
$productIds = $this->parentHandler->afterGetMatchingProductIds($rule, $productIds);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$productIds) {
|
||||
$productIds = $proceed();
|
||||
}
|
||||
|
||||
return $productIds;
|
||||
}
|
||||
}
|
||||
Executable
+77
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\CatalogSearch\Block\Result;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Config;
|
||||
use Amasty\ElasticSearch\Model\Search\SubQuery\Helper as SubQueryHelper;
|
||||
use Amasty\ElasticSearch\ViewModel\CatalogSearch\FurtherQueryViewModel;
|
||||
use Magento\CatalogSearch\Block\Result as ResultBlock;
|
||||
use Magento\Framework\View\Element\BlockFactory;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
|
||||
class AddSecondSearchQueryInput
|
||||
{
|
||||
private const TEMPLATE_NAME = 'Amasty_ElasticSearch::search/result/further_input.phtml';
|
||||
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var BlockFactory
|
||||
*/
|
||||
private $blockFactory;
|
||||
|
||||
/**
|
||||
* @var FurtherQueryViewModel
|
||||
*/
|
||||
private $furtherQueryViewModel;
|
||||
|
||||
/**
|
||||
* @var SubQueryHelper
|
||||
*/
|
||||
private $subQueryHelper;
|
||||
|
||||
public function __construct(
|
||||
Config $config,
|
||||
BlockFactory $blockFactory,
|
||||
FurtherQueryViewModel $furtherQueryViewModel,
|
||||
SubQueryHelper $subQueryHelper
|
||||
) {
|
||||
$this->config = $config;
|
||||
$this->blockFactory = $blockFactory;
|
||||
$this->furtherQueryViewModel = $furtherQueryViewModel;
|
||||
$this->subQueryHelper = $subQueryHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add further query block for search result block if allowed.
|
||||
*
|
||||
* @see ResultBlock::toHtml()
|
||||
*/
|
||||
public function afterToHtml(ResultBlock $subject, string $result): string
|
||||
{
|
||||
if (!$subject->getResultCount() && $this->subQueryHelper->getQueryText() === null) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ($this->config->isFurtherQueryAllowed()) {
|
||||
$furtherBlock = $this->blockFactory->createBlock(Template::class, ['data' => [
|
||||
'template' => self::TEMPLATE_NAME,
|
||||
'furtherQueryViewModel' => $this->furtherQueryViewModel
|
||||
]]);
|
||||
$result = preg_replace('@<div[^>]*>@s', '$0' . $furtherBlock->toHtml(), $result, 1);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Executable
+47
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\CatalogSearch\Controller\Result\Index;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Search\SubQuery\Helper as SubQueryHelper;
|
||||
use Magento\CatalogSearch\Controller\Result\Index as CatalogSearchController;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
|
||||
class RegisterSubQuery
|
||||
{
|
||||
/**
|
||||
* @var RequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* @var SubQueryHelper
|
||||
*/
|
||||
private $subQueryHelper;
|
||||
|
||||
public function __construct(RequestInterface $request, SubQueryHelper $subQueryHelper)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->subQueryHelper = $subQueryHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register sub query from request.
|
||||
*
|
||||
* @see CatalogSearchController::execute
|
||||
*/
|
||||
public function beforeExecute(): void
|
||||
{
|
||||
$subQueryText = $this->request->getParam($this->subQueryHelper->getQueryParamName());
|
||||
if ($subQueryText !== null) {
|
||||
$this->subQueryHelper->setQueryText($subQueryText);
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+114
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\CatalogSearch\Model\ResourceModel\Fulltext;
|
||||
|
||||
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection as FulltextCollection;
|
||||
use Magento\Catalog\Model\Product\ProductList\Toolbar as ToolbarModel;
|
||||
use Magento\Framework\App\ProductMetadataInterface;
|
||||
use Magento\Framework\Exception\StateException;
|
||||
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Collection
|
||||
{
|
||||
/**
|
||||
* @var ToolbarModel
|
||||
*/
|
||||
private $toolbarModel;
|
||||
|
||||
/**
|
||||
* @var MessageManagerInterface
|
||||
*/
|
||||
private $messageManager;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* @var \Amasty\Base\Model\MagentoVersion
|
||||
*/
|
||||
private $magentoVersion;
|
||||
|
||||
public function __construct(
|
||||
\Amasty\Base\Model\MagentoVersion $magentoVersion,
|
||||
ToolbarModel $toolbarModel,
|
||||
MessageManagerInterface $messageManager,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->toolbarModel = $toolbarModel;
|
||||
$this->messageManager = $messageManager;
|
||||
$this->logger = $logger;
|
||||
$this->magentoVersion = $magentoVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FulltextCollection $collection
|
||||
* @param bool $printQuery
|
||||
* @param bool $logQuery
|
||||
* @return array
|
||||
*/
|
||||
public function beforeLoad(FulltextCollection $collection, $printQuery = false, $logQuery = false)
|
||||
{
|
||||
if (!$collection->isLoaded() && $this->isLessThan22()) {
|
||||
$this->setRelevanceOrder($collection);
|
||||
}
|
||||
|
||||
return [$printQuery, $logQuery];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FulltextCollection $collection
|
||||
*/
|
||||
public function beforeGetSize(FulltextCollection $collection)
|
||||
{
|
||||
if (!$collection->isLoaded() && $this->isLessThan22()) {
|
||||
$this->setRelevanceOrder($collection);
|
||||
}
|
||||
}
|
||||
|
||||
private function isLessThan22()
|
||||
{
|
||||
return version_compare($this->magentoVersion->get(), '2.2', '<');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FulltextCollection $collection
|
||||
* @return FulltextCollection
|
||||
*/
|
||||
private function setRelevanceOrder(FulltextCollection $collection)
|
||||
{
|
||||
$direction = strtolower($this->toolbarModel->getDirection());
|
||||
if ($direction) {
|
||||
$collection->setOrder('relevance', $direction);
|
||||
} else {
|
||||
$collection->setOrder('relevance');
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $subject
|
||||
* @param \Closure $proceed
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function aroundGetFacetedData($subject, \Closure $proceed, $argument)
|
||||
{
|
||||
try {
|
||||
$result = $proceed($argument);
|
||||
} catch (StateException $exception) {
|
||||
$this->messageManager->addErrorMessage(__('Sorry, search engine is currently unavailable'));
|
||||
$this->logger->error($exception->getMessage());
|
||||
$result = [];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\CatalogSearch\Model\Search;
|
||||
|
||||
use Magento\CatalogSearch\Model\Search\RequestGenerator as SearchRequestGenerator;
|
||||
|
||||
class RequestGenerator
|
||||
{
|
||||
/**
|
||||
* @var \Amasty\Base\Model\MagentoVersion
|
||||
*/
|
||||
private $magentoVersion;
|
||||
|
||||
public function __construct(\Amasty\Base\Model\MagentoVersion $magentoVersion)
|
||||
{
|
||||
$this->magentoVersion = $magentoVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SearchRequestGenerator $subject
|
||||
* @param array $requests
|
||||
* @return array
|
||||
*/
|
||||
public function afterGenerate(SearchRequestGenerator $subject, $requests)
|
||||
{
|
||||
//TODO deprecated. remove in next releases
|
||||
if (version_compare($this->magentoVersion->get(), '2.2.0', '<')) {
|
||||
foreach ($requests as $requestContainer => &$request) {
|
||||
if (isset($request['queries'][$requestContainer]['queryReference'])
|
||||
&& !empty($request['queries'][$requestContainer]['queryReference'])
|
||||
) {
|
||||
foreach ($request['queries'][$requestContainer]['queryReference'] as &$reference) {
|
||||
$reference['clause'] = 'must';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $requests;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\Config\Model;
|
||||
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Indexer\IndexerRegistry
|
||||
*/
|
||||
private $indexerRegistry;
|
||||
|
||||
/**
|
||||
* @var \Amasty\ElasticSearch\Model\Config
|
||||
*/
|
||||
private $elasticConfig;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Config\ScopeConfigInterface
|
||||
*/
|
||||
private $config;
|
||||
|
||||
public function __construct(
|
||||
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
|
||||
\Amasty\ElasticSearch\Model\Config $elasticConfig,
|
||||
\Magento\Framework\App\Config\ScopeConfigInterface $config
|
||||
) {
|
||||
$this->indexerRegistry = $indexerRegistry;
|
||||
$this->elasticConfig = $elasticConfig;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Magento\Config\Model\Config $subject
|
||||
* @return array
|
||||
*/
|
||||
public function beforeSave(\Magento\Config\Model\Config $subject)
|
||||
{
|
||||
$groups = $subject->getGroups();
|
||||
if ($this->isSpecialCharsOptionChanged($groups) || $this->isShowOutOfStockOptionChanged($groups)) {
|
||||
$this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate();
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $groups
|
||||
* @return bool
|
||||
*/
|
||||
private function isSpecialCharsOptionChanged($groups)
|
||||
{
|
||||
$result = false;
|
||||
if (isset($groups['catalog']['fields']['long_tail']['value'])) {
|
||||
$specialCharValue = $groups['catalog']['fields']['long_tail']['value'];
|
||||
$specialCharValueOrigin = $this->elasticConfig->getModuleConfig('catalog/long_tail');
|
||||
$result = $specialCharValue !== $specialCharValueOrigin;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $groups
|
||||
* @return bool
|
||||
*/
|
||||
private function isShowOutOfStockOptionChanged($groups)
|
||||
{
|
||||
$result = false;
|
||||
$outOfStockValue = false;
|
||||
if (isset($groups['options']['fields']['show_out_of_stock']['value'])) {
|
||||
$outOfStockValue = $groups['options']['fields']['show_out_of_stock']['value'];
|
||||
} elseif (isset($groups['options']['fields']['show_out_of_stock']['inherit'])) {
|
||||
$outOfStockValue = '0';
|
||||
}
|
||||
|
||||
if ($outOfStockValue !== false) {
|
||||
$outOfStockValueOrigin = $this->config->getValue('cataloginventory/options/show_out_of_stock');
|
||||
$result = $outOfStockValue !== $outOfStockValueOrigin;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Executable
+150
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\Framework\Api\Search\SearchInterface;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Search\SubQuery\Helper as SubQueryHelper;
|
||||
use Magento\Framework\Api\FilterBuilder;
|
||||
use Magento\Framework\Api\Search\DocumentInterface;
|
||||
use Magento\Framework\Api\Search\FilterGroupBuilder;
|
||||
use Magento\Framework\Api\Search\SearchCriteriaInterface;
|
||||
use Magento\Framework\Api\Search\SearchInterface;
|
||||
use Magento\Framework\Api\Search\SearchResultInterface;
|
||||
|
||||
class SearchWithSubQuery
|
||||
{
|
||||
public const PRODUCTS_LIMIT_MAIN_QUERY = 9000;
|
||||
|
||||
private const SEARCH_TERM_FILTER_CODE = 'search_term';
|
||||
|
||||
/**
|
||||
* @var SubQueryHelper
|
||||
*/
|
||||
private $subQueryHelper;
|
||||
|
||||
/**
|
||||
* @var null|int
|
||||
*/
|
||||
private $originalCurrentPage;
|
||||
|
||||
/**
|
||||
* @var null|int
|
||||
*/
|
||||
private $originalPageSize;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isSubQuerySearch = false;
|
||||
|
||||
/**
|
||||
* @var FilterGroupBuilder
|
||||
*/
|
||||
private $filterGroupBuilder;
|
||||
|
||||
/**
|
||||
* @var FilterBuilder
|
||||
*/
|
||||
private $filterBuilder;
|
||||
|
||||
public function __construct(
|
||||
SubQueryHelper $subQueryHelper,
|
||||
FilterGroupBuilder $filterGroupBuilder,
|
||||
FilterBuilder $filterBuilder
|
||||
) {
|
||||
$this->subQueryHelper = $subQueryHelper;
|
||||
$this->filterGroupBuilder = $filterGroupBuilder;
|
||||
$this->filterBuilder = $filterBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function beforeSearch(SearchInterface $search, SearchCriteriaInterface $searchCriteria): array
|
||||
{
|
||||
if ($this->isSubQuerySearchAllowed() && !$this->isSubQuerySearch) {
|
||||
$this->originalCurrentPage = $searchCriteria->getCurrentPage();
|
||||
$this->originalPageSize = $searchCriteria->getPageSize();
|
||||
$searchCriteria->setCurrentPage(0);
|
||||
$searchCriteria->setPageSize(static::PRODUCTS_LIMIT_MAIN_QUERY);
|
||||
}
|
||||
|
||||
return [$searchCriteria];
|
||||
}
|
||||
|
||||
public function afterSearch(
|
||||
SearchInterface $search,
|
||||
SearchResultInterface $searchResult,
|
||||
SearchCriteriaInterface $searchCriteria
|
||||
): SearchResultInterface {
|
||||
if ($this->isSubQuerySearchAllowed() && !$this->isSubQuerySearch) {
|
||||
$this->isSubQuerySearch = true;
|
||||
$entityIds = $this->getEntityIds($searchResult);
|
||||
if (empty($entityIds)) {
|
||||
return $searchResult;
|
||||
}
|
||||
|
||||
$this->modifySearchTerm($searchCriteria);
|
||||
$this->applyEntityIdsFilter($searchCriteria, $entityIds);
|
||||
$searchResult = $search->search($searchCriteria);
|
||||
$this->isSubQuerySearch = false;
|
||||
}
|
||||
|
||||
return $searchResult;
|
||||
}
|
||||
|
||||
private function isSubQuerySearchAllowed(): bool
|
||||
{
|
||||
return $this->subQueryHelper->getQueryText() !== null;
|
||||
}
|
||||
|
||||
private function modifySearchTerm(SearchCriteriaInterface $searchCriteria): void
|
||||
{
|
||||
$searchCriteria->setCurrentPage($this->originalCurrentPage);
|
||||
$searchCriteria->setPageSize($this->originalPageSize);
|
||||
foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
|
||||
foreach ($filterGroup->getFilters() as $filter) {
|
||||
if ($filter->getField() === self::SEARCH_TERM_FILTER_CODE) {
|
||||
$filter->setValue($this->subQueryHelper->getQueryText());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add entity ids filter for $searchCriteria.
|
||||
*/
|
||||
private function applyEntityIdsFilter(
|
||||
SearchCriteriaInterface $searchCriteria,
|
||||
array $entityIds
|
||||
): void {
|
||||
$this->filterBuilder->setField('amasty_entity_ids');
|
||||
$this->filterBuilder->setValue($entityIds);
|
||||
$this->filterBuilder->setConditionType('in');
|
||||
|
||||
$this->filterGroupBuilder->setFilters([]);
|
||||
$this->filterGroupBuilder->addFilter($this->filterBuilder->create());
|
||||
$filterGroup = $this->filterGroupBuilder->create();
|
||||
|
||||
$filterGroups = $searchCriteria->getFilterGroups();
|
||||
$filterGroups[] = $filterGroup;
|
||||
$searchCriteria->setFilterGroups($filterGroups);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve entity ids from search result.
|
||||
*/
|
||||
private function getEntityIds(SearchResultInterface $searchResult): array
|
||||
{
|
||||
return array_map(function (DocumentInterface $item) {
|
||||
return $item->getId();
|
||||
}, $searchResult->getItems());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\Search\Request;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Config;
|
||||
use Magento\Catalog\Block\Product\ProductList\Toolbar;
|
||||
use Magento\Framework\View\LayoutInterface;
|
||||
|
||||
class Binder
|
||||
{
|
||||
public const ALL_TOOLBAR_OPTION = 'all';
|
||||
|
||||
/**
|
||||
* @var Toolbar
|
||||
*/
|
||||
protected $toolbar;
|
||||
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
protected $configProvider;
|
||||
|
||||
public function __construct(
|
||||
Toolbar $toolbar,
|
||||
Config $configProvider
|
||||
) {
|
||||
$this->toolbar = $toolbar;
|
||||
$this->configProvider = $configProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix all products toolbar option on Magento version > 2.3.2
|
||||
* @param \Magento\Framework\Search\Request\Binder $subject
|
||||
* @param array $resultData
|
||||
* @param array $requestData
|
||||
* @param array $bindData
|
||||
* @return array
|
||||
*/
|
||||
public function afterBind(
|
||||
\Magento\Framework\Search\Request\Binder $subject,
|
||||
array $resultData,
|
||||
array $requestData,
|
||||
array $bindData
|
||||
) {
|
||||
if ($this->toolbar && $this->toolbar->getLimit() === self::ALL_TOOLBAR_OPTION
|
||||
&& !$resultData['size'] && $this->configProvider->isElasticEngine()
|
||||
) {
|
||||
$resultData['size'] = $requestData['size'];
|
||||
}
|
||||
|
||||
return $resultData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\Store\Model;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Indexer\RelevanceRule\RuleProductProcessor;
|
||||
use Magento\Store\Model\Website;
|
||||
|
||||
class WebsitePlugin
|
||||
{
|
||||
/**
|
||||
* @var RuleProductProcessor
|
||||
*/
|
||||
private $ruleProductProcessor;
|
||||
|
||||
public function __construct(RuleProductProcessor $ruleProductProcessor)
|
||||
{
|
||||
$this->ruleProductProcessor = $ruleProductProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Website $subject
|
||||
* @param Website $result
|
||||
* @return Website
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function afterDelete(Website $subject, Website $result)
|
||||
{
|
||||
$this->ruleProductProcessor->markIndexerAsInvalid();
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\Xsearch\Helper;
|
||||
|
||||
use \Amasty\ElasticSearch\Model\Search\GetRequestQuery\InjectMatchQuery;
|
||||
|
||||
class Data
|
||||
{
|
||||
/**
|
||||
* @var InjectMatchQuery
|
||||
*/
|
||||
private $injectMatchQuery;
|
||||
|
||||
public function __construct(
|
||||
InjectMatchQuery $injectMatchQuery
|
||||
) {
|
||||
$this->injectMatchQuery = $injectMatchQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Amasty\Xsearch\Helper\Data $subject
|
||||
* @param string $text
|
||||
* @param string $query
|
||||
* @return array
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function beforeHighlight(
|
||||
\Amasty\Xsearch\Helper\Data $subject,
|
||||
$text,
|
||||
$query
|
||||
) {
|
||||
if ($query) {
|
||||
/**
|
||||
* Quick fix magento bug in \Magento\Framework\DB\Adapter\Pdo::_splitMultiQuery(sql)
|
||||
* This algorithm does not work properly
|
||||
*/
|
||||
$query = str_replace(';', '', $query);
|
||||
$words = $this->injectMatchQuery->removeStopWords(explode(' ', $query));
|
||||
$query = implode(' ', $words);
|
||||
}
|
||||
|
||||
return [$text, $query];
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Plugin\Xsearch\Plugin\CatalogSearch\Block\Result\ChangeNoResultText;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Search\SubQuery\Helper as SubQueryHelper;
|
||||
use Amasty\Xsearch\Plugin\CatalogSearch\Block\Result\ChangeNoResultText;
|
||||
|
||||
class ReplaceQueryText
|
||||
{
|
||||
/**
|
||||
* @var SubQueryHelper
|
||||
*/
|
||||
private $subQueryHelper;
|
||||
|
||||
public function __construct(SubQueryHelper $subQueryHelper)
|
||||
{
|
||||
$this->subQueryHelper = $subQueryHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function aroundGetQueryText(ChangeNoResultText $subject, callable $proceed): string
|
||||
{
|
||||
if ($subQueryText = $this->subQueryHelper->getQueryText()) {
|
||||
return $subQueryText;
|
||||
}
|
||||
|
||||
return $proceed();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user