ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Indexer\Data;
|
||||
|
||||
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
|
||||
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperResolverInterface;
|
||||
|
||||
class DataMapperResolver implements DataMapperResolverInterface
|
||||
{
|
||||
public const DEFAULT_DATA_INDEXER = 'catalogsearch_fulltext';
|
||||
public const INDEXER_ID = 'indexer_id';
|
||||
|
||||
/**
|
||||
* @since 1.17.1 added mapped data to context
|
||||
*/
|
||||
public const MAPPED_DATA_KEY = 'document';
|
||||
|
||||
/**
|
||||
* @var DataMapperInterface[][]
|
||||
*/
|
||||
private $mappers = [];
|
||||
|
||||
public function __construct(array $dataMappers = [])
|
||||
{
|
||||
foreach ($dataMappers as $type => $mappers) {
|
||||
$this->mappers[$type] = [];
|
||||
foreach ($mappers as $mapper) {
|
||||
$this->mappers[$type][] = $mapper;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $indexData [attribute_id(int) => attribute_value(string|int|string[]|int[])]
|
||||
* @param int $storeId
|
||||
* @param array $context
|
||||
* @return array
|
||||
*/
|
||||
public function mapEntityData(array $indexData, $storeId, $context = [])
|
||||
{
|
||||
$data = [];
|
||||
$indexerId = $context[self::INDEXER_ID] ?? self::DEFAULT_DATA_INDEXER;
|
||||
if (isset($this->mappers[$indexerId]) && is_array($this->mappers[$indexerId])) {
|
||||
/** @var DataMapperInterface $mapper */
|
||||
foreach ($this->mappers[$indexerId] as $mapper) {
|
||||
$context[self::MAPPED_DATA_KEY] = $data;
|
||||
$mappedData = $mapper->map($indexData, $storeId, $context);
|
||||
$data = $this->mergeData($mappedData, $data);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function mergeData(array $mappedData, array $data): array
|
||||
{
|
||||
/** @var array $entityData */
|
||||
foreach ($mappedData as $entityId => $entityData) {
|
||||
if (isset($data[$entityId])) {
|
||||
$data[$entityId] = $entityData + $data[$entityId];
|
||||
} else {
|
||||
$data[$entityId] = $entityData;
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Indexer\Data\External;
|
||||
|
||||
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
|
||||
use Amasty\ElasticSearch\Model\Indexer\Structure\EntityBuilder\External\Finder;
|
||||
use Magento\Catalog\Api\Data\ProductAttributeInterface;
|
||||
|
||||
class FinderDataMapper implements DataMapperInterface
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute
|
||||
*/
|
||||
private $eavAttribute;
|
||||
|
||||
/**
|
||||
* @var Finder
|
||||
*/
|
||||
private $externalBuilder;
|
||||
|
||||
public function __construct(
|
||||
\Magento\Eav\Model\ResourceModel\Entity\Attribute $eavAttribute,
|
||||
Finder $externalBuilder
|
||||
) {
|
||||
$this->eavAttribute = $eavAttribute;
|
||||
$this->externalBuilder = $externalBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $documentData
|
||||
* @param int $storeId
|
||||
* @param array $context
|
||||
* @return array
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function map(array $documentData, $storeId, array $context = [])
|
||||
{
|
||||
$documents = [];
|
||||
$externalAttributeIds = [];
|
||||
foreach (array_keys($this->externalBuilder->getExtraValueFields()) as $attributeCode) {
|
||||
$externalAttributeIds[$attributeCode . '_value']
|
||||
= $this->eavAttribute->getIdByCode(ProductAttributeInterface::ENTITY_TYPE_CODE, $attributeCode);
|
||||
}
|
||||
|
||||
foreach ($documentData as $productId => $indexData) {
|
||||
foreach ($externalAttributeIds as $filterName => $attributeId) {
|
||||
if (isset($indexData[$attributeId])) {
|
||||
if (is_array($indexData[$attributeId])) {
|
||||
$value = isset($indexData[$attributeId][$productId])
|
||||
? $indexData[$attributeId][$productId] : '';
|
||||
} else {
|
||||
$value = $indexData[$attributeId];
|
||||
}
|
||||
|
||||
$documents[$productId][$filterName] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $documents;
|
||||
}
|
||||
}
|
||||
Vendored
Executable
+55
@@ -0,0 +1,55 @@
|
||||
<?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\Model\Indexer\Data\External;
|
||||
|
||||
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
|
||||
use Amasty\ElasticSearch\Api\RelevanceRuleRepositoryInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class RelevanceBoostDataMapper implements DataMapperInterface
|
||||
{
|
||||
public const DEFAULT_RELEVANCE = 1.;
|
||||
public const ATTRIBUTE_NAME = 'amasty_product_relevance';
|
||||
|
||||
/**
|
||||
* @var RelevanceRuleRepositoryInterface
|
||||
*/
|
||||
private $relevanceRuleRepository;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
public function __construct(
|
||||
RelevanceRuleRepositoryInterface $relevanceRuleRepository,
|
||||
StoreManagerInterface $storeManager
|
||||
) {
|
||||
$this->relevanceRuleRepository = $relevanceRuleRepository;
|
||||
$this->storeManager = $storeManager;
|
||||
}
|
||||
|
||||
public function map(array $indexData, $storeId, array $context = []): array
|
||||
{
|
||||
$websiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
|
||||
$productIds = array_keys($indexData);
|
||||
$productBoostData = $this->relevanceRuleRepository->getProductBoostMultipliers($productIds, (int) $websiteId);
|
||||
$indexBoostData = [];
|
||||
|
||||
foreach ($indexData as $productId => $productData) {
|
||||
$indexBoostData[$productId] = [
|
||||
self::ATTRIBUTE_NAME => $productBoostData[$productId] ?? self::DEFAULT_RELEVANCE
|
||||
];
|
||||
}
|
||||
|
||||
return $indexBoostData;
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Indexer\Data\Product;
|
||||
|
||||
class AttributeDataProvider
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Eav\Model\Entity\Attribute[]
|
||||
*/
|
||||
private $attributesById;
|
||||
|
||||
/**
|
||||
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory
|
||||
*/
|
||||
private $productAttributeCollectionFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Eav\Model\Config
|
||||
*/
|
||||
private $eavConfig;
|
||||
|
||||
public function __construct(
|
||||
\Magento\Eav\Model\Config $eavConfig,
|
||||
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttributeCollectionFactory
|
||||
) {
|
||||
$this->eavConfig = $eavConfig;
|
||||
$this->productAttributeCollectionFactory = $prodAttributeCollectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $backendType
|
||||
* @return $this
|
||||
*/
|
||||
private function initSearchableAttributes()
|
||||
{
|
||||
if ($this->attributesById === null) {
|
||||
/** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection $productAttributes */
|
||||
$productAttributes = $this->productAttributeCollectionFactory->create();
|
||||
$productAttributes->addToIndexFilter(true);
|
||||
|
||||
/** @var \Magento\Eav\Model\Entity\Attribute[] $attributes */
|
||||
$attributes = $productAttributes->getItems();
|
||||
|
||||
$entity = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getEntity();
|
||||
foreach ($attributes as $attribute) {
|
||||
$options = [];
|
||||
if ($attribute->getFrontendInput() === 'select' || $attribute->getFrontendInput() === 'multiselect') {
|
||||
foreach ($attribute->getOptions() as $option) {
|
||||
$options[$option->getValue()] = $option->getLabel();
|
||||
}
|
||||
}
|
||||
$attribute->setAttributeOptionsArray($options);
|
||||
|
||||
$attribute->setEntity($entity);
|
||||
$this->attributesById[$attribute->getAttributeId()] = $attribute;
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|string $attribute
|
||||
* @return \Magento\Eav\Model\Entity\Attribute|null
|
||||
*/
|
||||
public function getAttribute($attributeId)
|
||||
{
|
||||
$this->initSearchableAttributes();
|
||||
|
||||
if (isset($this->attributesById[$attributeId])) {
|
||||
return $this->attributesById[$attributeId];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
<?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\Model\Indexer\Data\Product;
|
||||
|
||||
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
|
||||
use Amasty\ElasticSearch\Model\Di\Wrapper as DiWrapper;
|
||||
use Amasty\ElasticSearch\Model\ResourceModel\Product\ConvertProductIdToSku;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
|
||||
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
|
||||
use Magento\Store\Api\StoreRepositoryInterface;
|
||||
|
||||
/**
|
||||
* Provide is_out_of_stock value.
|
||||
* Need because we have our data mapper, which different with magento data mapper.
|
||||
*
|
||||
* @see \Magento\InventoryElasticsearch\Plugin\Model\Adapter\BatchDataMapper\ProductDataMapperPlugin
|
||||
*/
|
||||
class OutofStockDataMapper implements DataMapperInterface
|
||||
{
|
||||
public const OUT_OF_STOCK_FIELD = 'is_out_of_stock';
|
||||
|
||||
/**
|
||||
* @var StoreRepositoryInterface
|
||||
*/
|
||||
private $storeRepository;
|
||||
|
||||
/**
|
||||
* @var DiWrapper|StockByWebsiteIdResolverInterface
|
||||
*/
|
||||
private $stockByWebsiteIdResolver;
|
||||
|
||||
/**
|
||||
* @var DiWrapper|GetStockItemDataInterface
|
||||
*/
|
||||
private $getStockItemData;
|
||||
|
||||
/**
|
||||
* @var AttributeDataProvider
|
||||
*/
|
||||
private $attributeDataProvider;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
private $skuAttributeId;
|
||||
|
||||
/**
|
||||
* @var ConvertProductIdToSku
|
||||
*/
|
||||
private $convertProductIdToSku;
|
||||
|
||||
public function __construct(
|
||||
StoreRepositoryInterface $storeRepository,
|
||||
DiWrapper $stockByWebsiteIdResolver,
|
||||
DiWrapper $getStockItemData,
|
||||
AttributeDataProvider $attributeDataProvider,
|
||||
ConvertProductIdToSku $convertProductIdToSku
|
||||
) {
|
||||
$this->storeRepository = $storeRepository;
|
||||
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
|
||||
$this->getStockItemData = $getStockItemData;
|
||||
$this->attributeDataProvider = $attributeDataProvider;
|
||||
$this->convertProductIdToSku = $convertProductIdToSku;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $indexData
|
||||
* @param int $storeId
|
||||
* @param array $context
|
||||
* @return array
|
||||
*/
|
||||
public function map(array $indexData, $storeId, array $context = [])
|
||||
{
|
||||
$store = $this->storeRepository->getById($storeId);
|
||||
$stock = $this->stockByWebsiteIdResolver->execute((int) $store->getWebsiteId());
|
||||
if ($stock === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$outOfStockValues = [];
|
||||
foreach ($indexData as $productId => $document) {
|
||||
$sku = $this->getSku($productId, $document);
|
||||
if (!$sku) {
|
||||
$isOutOfStock = 1;
|
||||
} else {
|
||||
try {
|
||||
$stockItemData = $this->getStockItemData->execute($sku, $stock->getStockId());
|
||||
} catch (NoSuchEntityException $e) {
|
||||
$stockItemData = null;
|
||||
}
|
||||
$isOutOfStock = null !== $stockItemData
|
||||
? (int) $stockItemData[GetStockItemDataInterface::IS_SALABLE] : 1;
|
||||
}
|
||||
$outOfStockValues[$productId] = [self::OUT_OF_STOCK_FIELD => $isOutOfStock];
|
||||
}
|
||||
|
||||
return $outOfStockValues;
|
||||
}
|
||||
|
||||
private function getSku($productId, array $document): string
|
||||
{
|
||||
if ($this->skuAttributeId === null) {
|
||||
$this->resolveSkuAttributeId($document);
|
||||
}
|
||||
|
||||
if ($this->skuAttributeId) {
|
||||
$sku = $document[$this->skuAttributeId] ?? '';
|
||||
if (is_array($sku)) {
|
||||
$sku = $sku[$productId] ?? reset($sku);
|
||||
}
|
||||
} else {
|
||||
$sku = $this->convertProductIdToSku->execute((int)$productId);
|
||||
}
|
||||
|
||||
return $sku;
|
||||
}
|
||||
|
||||
private function resolveSkuAttributeId(array $document): void
|
||||
{
|
||||
$this->skuAttributeId = 0; // skip next sku attribute id search
|
||||
foreach ($document as $attributeId => $value) {
|
||||
$attribute = $this->attributeDataProvider->getAttribute($attributeId);
|
||||
if ($attribute && $attribute->getAttributeCode() === 'sku') {
|
||||
$this->skuAttributeId = $attributeId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+140
@@ -0,0 +1,140 @@
|
||||
<?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\Model\Indexer\Data\Product\PriceDataMapper;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Indexer\Data\Product\ProductPriceDataMapper;
|
||||
use Magento\Catalog\Model\Indexer\Product\Price\DimensionModeConfiguration;
|
||||
use Magento\Customer\Model\Indexer\CustomerGroupDimensionProvider;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Indexer\Dimension;
|
||||
use Magento\Framework\Indexer\DimensionFactory;
|
||||
use Magento\Framework\Search\Request\IndexScopeResolverInterface;
|
||||
use Magento\Store\Model\Indexer\WebsiteDimensionProvider;
|
||||
|
||||
class PriceResolver
|
||||
{
|
||||
/**
|
||||
* @var IndexScopeResolverInterface
|
||||
*/
|
||||
private $priceTableResolver;
|
||||
|
||||
/**
|
||||
* @var DimensionFactory
|
||||
*/
|
||||
private $dimensionFactory;
|
||||
|
||||
/**
|
||||
* @var DimensionModeConfiguration
|
||||
*/
|
||||
private $dimensionModeConfiguration;
|
||||
|
||||
/**
|
||||
* @var ResourceConnection
|
||||
*/
|
||||
private $resource;
|
||||
|
||||
public function __construct(
|
||||
IndexScopeResolverInterface $priceTableResolver,
|
||||
DimensionFactory $dimensionFactory,
|
||||
DimensionModeConfiguration $dimensionModeConfiguration,
|
||||
ResourceConnection $resourceConnection
|
||||
) {
|
||||
$this->priceTableResolver = $priceTableResolver;
|
||||
$this->dimensionFactory = $dimensionFactory;
|
||||
$this->dimensionModeConfiguration = $dimensionModeConfiguration;
|
||||
$this->resource = $resourceConnection;
|
||||
}
|
||||
|
||||
public function getProductPriceData(array $productIds, array $customerGroupIds, int $websiteId): array
|
||||
{
|
||||
$priceTableNames = $this->resolvePriceTableNames($websiteId, $customerGroupIds);
|
||||
$result = [];
|
||||
$connection = $this->resource->getConnection();
|
||||
|
||||
foreach ($priceTableNames as $tableName) {
|
||||
$select = $connection->select()->from(
|
||||
$this->resource->getTableName($tableName),
|
||||
['entity_id', 'customer_group_id', 'min_price']
|
||||
);
|
||||
|
||||
$select->where('website_id = ?', $websiteId);
|
||||
$select->where('entity_id IN (?)', $productIds);
|
||||
|
||||
foreach ($connection->fetchAll($select) as $row) {
|
||||
$result[$row['entity_id']][$row['customer_group_id']] = round(
|
||||
(float)$row['min_price'],
|
||||
ProductPriceDataMapper::DEFAULT_PRECISION
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect price index table names
|
||||
*
|
||||
* Price index can have dimensions for customer group and website
|
||||
*
|
||||
* @param int $websiteId
|
||||
* @param int[]|string[] $customerGroupIds
|
||||
* @return string[] DB table names
|
||||
*/
|
||||
private function resolvePriceTableNames(int $websiteId, array $customerGroupIds): array
|
||||
{
|
||||
$dimensionConfiguration = $this->dimensionModeConfiguration->getDimensionConfiguration();
|
||||
$isCustomerGroup = false;
|
||||
$websiteDimension = null;
|
||||
foreach ($dimensionConfiguration as $dimensionName) {
|
||||
if ($dimensionName === WebsiteDimensionProvider::DIMENSION_NAME) {
|
||||
$websiteDimension = $this->createDimensionFromWebsite($websiteId);
|
||||
}
|
||||
if ($dimensionName === CustomerGroupDimensionProvider::DIMENSION_NAME) {
|
||||
$isCustomerGroup = true;
|
||||
}
|
||||
}
|
||||
|
||||
$priceTableNames = [];
|
||||
|
||||
if ($isCustomerGroup) {
|
||||
foreach ($customerGroupIds as $groupId) {
|
||||
$dimensions = [];
|
||||
if ($websiteDimension) {
|
||||
$dimensions[] = $websiteDimension;
|
||||
}
|
||||
$dimensions[] = $this->createDimensionFromCustomerGroup((int)$groupId);
|
||||
$priceTableNames[] = $this->priceTableResolver->resolve('catalog_product_index_price', $dimensions);
|
||||
}
|
||||
} elseif ($websiteDimension) {
|
||||
$priceTableNames[] = $this->priceTableResolver->resolve('catalog_product_index_price', [$websiteDimension]);
|
||||
} else {
|
||||
$priceTableNames[] = 'catalog_product_index_price';
|
||||
}
|
||||
|
||||
return $priceTableNames;
|
||||
}
|
||||
|
||||
private function createDimensionFromWebsite(int $websiteId): Dimension
|
||||
{
|
||||
return $this->dimensionFactory->create(
|
||||
WebsiteDimensionProvider::DIMENSION_NAME,
|
||||
(string)$websiteId
|
||||
);
|
||||
}
|
||||
|
||||
private function createDimensionFromCustomerGroup(int $customerGroupId): Dimension
|
||||
{
|
||||
return $this->dimensionFactory->create(
|
||||
CustomerGroupDimensionProvider::DIMENSION_NAME,
|
||||
(string)$customerGroupId
|
||||
);
|
||||
}
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Indexer\Data\Product;
|
||||
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
|
||||
|
||||
class ProductCategoryDataMapper implements DataMapperInterface
|
||||
{
|
||||
/**
|
||||
* @var ResourceConnection
|
||||
*/
|
||||
private $resource;
|
||||
|
||||
public function __construct(ResourceConnection $resourceConnection)
|
||||
{
|
||||
$this->resource = $resourceConnection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $documentData
|
||||
* @param int $storeId
|
||||
* @param array $context
|
||||
* @return array
|
||||
*/
|
||||
public function map(array $documentData, $storeId, array $context = [])
|
||||
{
|
||||
$categoryData = $this->getProductCategoryData($storeId, array_keys($documentData));
|
||||
$categoryDocumentData = [];
|
||||
foreach ($documentData as $productId => $document) {
|
||||
$categoryDocumentData[$productId]['category_ids'] = $categoryData[$productId]['category_ids'] ?? [];
|
||||
if (isset($categoryData[$productId]['position'])) {
|
||||
foreach ($categoryData[$productId]['position'] as $field => $value) {
|
||||
$categoryDocumentData[$productId][$field] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $categoryDocumentData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $storeId
|
||||
* @param array $productIds
|
||||
* @return array
|
||||
*/
|
||||
private function getProductCategoryData($storeId, array $productIds = [])
|
||||
{
|
||||
$result = [];
|
||||
if (!empty($productIds)) {
|
||||
$connection = $this->resource->getConnection();
|
||||
$tableName = $this->resource->getTableName('catalog_category_product_index_store' . $storeId);
|
||||
$tableName = $connection->isTableExists($tableName)
|
||||
? $tableName : $this->resource->getTableName('catalog_category_product_index');
|
||||
$select = $connection->select()->from(
|
||||
[$tableName],
|
||||
['category_id', 'product_id', 'position']
|
||||
)->where('product_id IN (?)', $productIds);
|
||||
|
||||
foreach ($connection->fetchAll($select) as $row) {
|
||||
$result[$row['product_id']]['category_ids'][] = $row['category_id'];
|
||||
$result[$row['product_id']]['position']['category_position_' . $row['category_id']] = $row['position'];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
+292
@@ -0,0 +1,292 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Indexer\Data\Product;
|
||||
|
||||
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
|
||||
use Amasty\ElasticSearch\Model\ResourceModel\ConfigurableResolver;
|
||||
|
||||
class ProductDataMapper implements DataMapperInterface
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $excludedAttributes = [
|
||||
'price',
|
||||
'media_gallery',
|
||||
'tier_price',
|
||||
'quantity_and_stock_status',
|
||||
'media_gallery',
|
||||
'giftcard_amounts'
|
||||
];
|
||||
|
||||
/**
|
||||
* @var AttributeDataProvider
|
||||
*/
|
||||
private $attributeDataProvider;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $attributesExcludedFromMerge = [
|
||||
'status',
|
||||
'visibility',
|
||||
'tax_class_id'
|
||||
];
|
||||
|
||||
/**
|
||||
* @var ConfigurableResolver
|
||||
*/
|
||||
private $configurableResolver;
|
||||
|
||||
public function __construct(
|
||||
AttributeDataProvider $attributeDataProvider,
|
||||
ConfigurableResolver $configurableResolver,
|
||||
array $excludedAttributes = []
|
||||
) {
|
||||
$this->attributeDataProvider = $attributeDataProvider;
|
||||
$this->excludedAttributes = array_merge($this->excludedAttributes, $excludedAttributes);
|
||||
$this->configurableResolver = $configurableResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $documentData
|
||||
* @param int $storeId
|
||||
* @param array $context
|
||||
* @return array
|
||||
*/
|
||||
public function map(array $documentData, $storeId, array $context = [])
|
||||
{
|
||||
// reset attribute data for new store
|
||||
$documents = [];
|
||||
$skuValues = $this->configurableResolver->getRelationSkuValues(array_keys($documentData));
|
||||
foreach ($documentData as $productId => $indexData) {
|
||||
$document = ['store_id' => $storeId];
|
||||
$productIndexData = $this->getProductData($productId, $indexData, $storeId);
|
||||
$productIndexData = $this->updateSkuValues($productIndexData, $skuValues);
|
||||
foreach ($productIndexData as $attributeCode => $value) {
|
||||
if (in_array($attributeCode, $this->excludedAttributes, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$document[$attributeCode] = $value;
|
||||
}
|
||||
$documents[$productId] = $document;
|
||||
}
|
||||
|
||||
return $documents;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $productIndexData
|
||||
* @param array $skuValues
|
||||
* @return array
|
||||
*/
|
||||
private function updateSkuValues(array $productIndexData, array $skuValues)
|
||||
{
|
||||
$id = $productIndexData['entity_id'];
|
||||
if (isset($skuValues[$id]) && isset($productIndexData['sku'])) {
|
||||
$productIndexData['sku'] = array_merge([$productIndexData['sku']], explode(',', $skuValues[$id]));
|
||||
$productIndexData['sku'] = implode(' ', $productIndexData['sku']);
|
||||
}
|
||||
|
||||
return $productIndexData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $productId
|
||||
* @param array $indexData
|
||||
* @param int $storeId
|
||||
* @return array
|
||||
*/
|
||||
private function getProductData($productId, array $indexData, $storeId)
|
||||
{
|
||||
$productAttributes = [self::ENTITY_ID_FIELD_NAME => $productId];
|
||||
foreach ($indexData as $attributeId => $attributeValue) {
|
||||
$attribute = $this->attributeDataProvider->getAttribute($attributeId);
|
||||
|
||||
if (!$attribute) {
|
||||
continue;
|
||||
}
|
||||
// phpcs:ignore
|
||||
$productAttributes = array_merge(
|
||||
$productAttributes,
|
||||
$this->prepareProductData(
|
||||
$productId,
|
||||
$attributeValue,
|
||||
$attribute
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $productAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $productId
|
||||
* @param mixed $value
|
||||
* @param \Magento\Eav\Model\Entity\Attribute $attribute
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function prepareProductData($productId, $value, \Magento\Eav\Model\Entity\Attribute $attribute)
|
||||
{
|
||||
$productAttributes = [];
|
||||
$attributeCode = $attribute->getAttributeCode();
|
||||
$attributeFrontendInput = $attribute->getFrontendInput();
|
||||
$preparedValue = $this->getProductAttributeValue($productId, $attribute, $value);
|
||||
if (is_array($value) && in_array($attributeFrontendInput, ['select', 'multiselect'], true)) {
|
||||
$productAttributes = $this->prepareOptionalAttributeValues($attribute, $value, $preparedValue);
|
||||
} elseif ($preparedValue) {
|
||||
$productAttributes[$attributeCode] = $preparedValue;
|
||||
if (!is_array($preparedValue)) {
|
||||
$attributeOptions = $this->getAttributeOptions($attribute);
|
||||
if (isset($attributeOptions[$preparedValue])) {
|
||||
$productAttributes[$attributeCode . '_value'] = $attributeOptions[$preparedValue];
|
||||
}
|
||||
|
||||
if ($attributeFrontendInput === 'date'
|
||||
|| in_array($attribute->getBackendType(), ['datetime', 'timestamp'], true)
|
||||
) {
|
||||
if (preg_replace('#[ 0:-]#', '', $preparedValue) !== '') {
|
||||
$dateObj = new \DateTime($preparedValue, new \DateTimeZone('UTC'));
|
||||
$productAttributes[$attributeCode] = $dateObj->format('c');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $productAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $productId
|
||||
* @param \Magento\Eav\Model\Entity\Attribute $attribute
|
||||
* @param mixed $attributeValue
|
||||
* @return array|string|int
|
||||
*/
|
||||
private function getProductAttributeValue($productId, $attribute, $attributeValue)
|
||||
{
|
||||
if (is_array($attributeValue)) {
|
||||
if (isset($attributeValue[$productId])
|
||||
&& (!$attribute->getIsSearchable() || $this->isAttributeExcludedFromMerge($attribute))
|
||||
) {
|
||||
if (in_array($attribute->getFrontendInput(), ['text', 'textarea'], true)) {
|
||||
return $attributeValue[$productId];
|
||||
}
|
||||
|
||||
$explodedValue = explode(',', (string) $attributeValue[$productId]);
|
||||
return count($explodedValue) > 1 ? $explodedValue : $attributeValue[$productId];
|
||||
}
|
||||
|
||||
return $this->prepareAttributeValues($attributeValue);
|
||||
}
|
||||
|
||||
return $attributeValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix for compatibility with our Grouped Options module, due to which an attribute
|
||||
* option can have several values that come through ',' and if they are also written to the
|
||||
* elastic in this form, then the search for them will not work
|
||||
*/
|
||||
private function prepareAttributeValues(array $values): array
|
||||
{
|
||||
$values = array_values(array_unique($values));
|
||||
|
||||
foreach ($values as $key => $value) {
|
||||
$attrCodeValues = explode(',', (string) $value);
|
||||
unset($values[$key]);
|
||||
|
||||
foreach ($attrCodeValues as $attrCodeValue) {
|
||||
$values[] = $attrCodeValue;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(array_unique($values));
|
||||
}
|
||||
|
||||
public function isAttributeExcludedFromMerge(\Magento\Eav\Model\Entity\Attribute $attribute)
|
||||
{
|
||||
$result = false;
|
||||
|
||||
if (in_array($attribute->getAttributeCode(), $this->attributesExcludedFromMerge, true)
|
||||
|| in_array($attribute->getFrontendInput(), ['text', 'textarea'], true)) {
|
||||
$result = true;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Magento\Eav\Model\Entity\Attribute $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $preparedValue
|
||||
* @return array
|
||||
*/
|
||||
private function prepareOptionalAttributeValues(
|
||||
\Magento\Eav\Model\Entity\Attribute $attribute,
|
||||
$value,
|
||||
$preparedValue = null
|
||||
) {
|
||||
/**
|
||||
* @TODO add prices mapper
|
||||
*/
|
||||
$productAttributes = [];
|
||||
$attributeCode = $attribute->getAttributeCode();
|
||||
$attributeFrontendInput = $attribute->getFrontendInput();
|
||||
$attributeOptions = $this->getAttributeOptions($attribute);
|
||||
$textAttributeValue = [];
|
||||
|
||||
if ($attributeFrontendInput == 'select') {
|
||||
$productAttributes[$attributeCode] = $preparedValue;
|
||||
if ($attribute->getIsSearchable()) {
|
||||
foreach ($value as $optionIds) {
|
||||
$optionIds = explode(',', $optionIds);
|
||||
foreach ($optionIds as $optionId) {
|
||||
if (isset($attributeOptions[$optionId])) {
|
||||
$textAttributeValue[] = $attributeOptions[$optionId];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif ($attributeFrontendInput == 'multiselect') {
|
||||
$preparedValue = [];
|
||||
foreach ($value as $valueByAttribute) {
|
||||
$valueByAttribute = explode(',', $valueByAttribute);
|
||||
foreach ($valueByAttribute as $optionIds) {
|
||||
$optionIds = explode(',', $optionIds);
|
||||
foreach ($optionIds as $optionId) {
|
||||
if (isset($attributeOptions[$optionId]) && $attribute->getIsSearchable()) {
|
||||
$preparedValue[] = $optionId;
|
||||
$textAttributeValue[] = $attributeOptions[$optionId];
|
||||
} elseif (isset($attributeOptions[$optionId])) {
|
||||
$preparedValue[] = $optionId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$productAttributes[$attributeCode] = array_values(array_unique($preparedValue));
|
||||
}
|
||||
|
||||
if (!empty($textAttributeValue)) {
|
||||
$productAttributes[$attributeCode . '_value'] = array_values(array_unique($textAttributeValue));
|
||||
}
|
||||
|
||||
return $productAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Magento\Eav\Model\Entity\Attribute $attribute
|
||||
* @return array
|
||||
*/
|
||||
public function getAttributeOptions(\Magento\Eav\Model\Entity\Attribute $attribute)
|
||||
{
|
||||
return $attribute->getAttributeOptionsArray();
|
||||
}
|
||||
}
|
||||
+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\Model\Indexer\Data\Product;
|
||||
|
||||
use Amasty\ElasticSearch\Api\Data\Indexer\Data\DataMapperInterface;
|
||||
use Amasty\ElasticSearch\Model\Indexer\Data\Product\PriceDataMapper\PriceResolver;
|
||||
use DateTime;
|
||||
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Stdlib\DateTime as StdlibDateTime;
|
||||
use Magento\Store\Model\StoreManager;
|
||||
|
||||
/**
|
||||
* Map minimum product price in store with considering customer group.
|
||||
*
|
||||
* Indexed price data can be used in product sorting by price.
|
||||
*/
|
||||
class ProductPriceDataMapper implements DataMapperInterface
|
||||
{
|
||||
public const DEFAULT_PRECISION = 4;
|
||||
|
||||
/**
|
||||
* @var null|int[]|string[]
|
||||
*/
|
||||
private $customerGroupIds = null;
|
||||
|
||||
/**
|
||||
* @var StdlibDateTime
|
||||
*/
|
||||
protected $date;
|
||||
|
||||
/**
|
||||
* @var ResourceConnection
|
||||
*/
|
||||
private $resource;
|
||||
|
||||
/**
|
||||
* @var StoreManager
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var CollectionFactory
|
||||
*/
|
||||
private $groupCollectionFactory;
|
||||
|
||||
/**
|
||||
* @var PriceDataMapper\PriceResolver
|
||||
*/
|
||||
private $priceResolver;
|
||||
|
||||
public function __construct(
|
||||
ResourceConnection $resourceConnection,
|
||||
CollectionFactory $customerGroupCollectionFactory,
|
||||
StoreManager $storeManager,
|
||||
StdlibDateTime $date,
|
||||
PriceResolver $priceResolver
|
||||
) {
|
||||
$this->resource = $resourceConnection;
|
||||
$this->groupCollectionFactory = $customerGroupCollectionFactory;
|
||||
$this->storeManager = $storeManager;
|
||||
$this->date = $date;
|
||||
$this->priceResolver = $priceResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $documentData
|
||||
* @param int $storeId
|
||||
* @param array $context
|
||||
* @return array
|
||||
*/
|
||||
public function map(array $documentData, $storeId, array $context = [])
|
||||
{
|
||||
$productIds = array_keys($documentData);
|
||||
$prices = $this->getProductPriceData($productIds, (int)$storeId);
|
||||
$priceDocumentData = [];
|
||||
foreach ($productIds as $productId) {
|
||||
foreach ($this->getCustomerGroupIds() as $customerGroupId) {
|
||||
$priceValue = $prices[$productId][$customerGroupId] ?? 0;
|
||||
$priceDocumentData[$productId]['price_' . $customerGroupId] = $priceValue;
|
||||
}
|
||||
}
|
||||
|
||||
return $priceDocumentData;
|
||||
}
|
||||
|
||||
private function getCustomerGroupIds(): array
|
||||
{
|
||||
if ($this->customerGroupIds === null) {
|
||||
$this->customerGroupIds = $this->groupCollectionFactory->create()->getAllIds();
|
||||
}
|
||||
|
||||
return $this->customerGroupIds;
|
||||
}
|
||||
|
||||
private function getProductPriceData(array $productIds = [], int $storeId = null): array
|
||||
{
|
||||
$result = [];
|
||||
if (!empty($productIds)) {
|
||||
$websiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
|
||||
$result = $this->priceResolver->getProductPriceData($productIds, $this->getCustomerGroupIds(), $websiteId);
|
||||
|
||||
$connection = $this->resource->getConnection();
|
||||
|
||||
$select = $connection->select()->from(
|
||||
$this->resource->getTableName('catalogrule_product_price'),
|
||||
['product_id', 'customer_group_id', 'rule_price']
|
||||
);
|
||||
$now = new DateTime();
|
||||
$select->where('website_id = ?', $websiteId)
|
||||
->where('rule_date = ?', $this->date->formatDate($now, false));
|
||||
$select->where('product_id IN (?)', $productIds);
|
||||
foreach ($connection->fetchAll($select) as $row) {
|
||||
$rulePrice = round($row['rule_price'], self::DEFAULT_PRECISION);
|
||||
$productId = $row['product_id'];
|
||||
$customerGroupId = $row['customer_group_id'];
|
||||
if (isset($result[$productId][$customerGroupId])
|
||||
&& $result[$productId][$customerGroupId] > $rulePrice
|
||||
) {
|
||||
$result[$productId][$customerGroupId] = $rulePrice;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user