Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/2.10.x' into include-child-att…
Browse files Browse the repository at this point in the history
…ributes-toggle
  • Loading branch information
fredden committed Feb 22, 2023
2 parents 42e1afa + bb999d4 commit 938ec1a
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 73 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/01-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
jobs:
qualit:
name: Code Quality
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04
strategy:
matrix:
php-versions: ['7.4']
Expand All @@ -35,7 +35,7 @@ jobs:

- name: "Downgrade Composer"
run: composer self-update 2.1.14

- name: Validate composer.json and composer.lock
run: composer validate

Expand Down
10 changes: 1 addition & 9 deletions .github/workflows/20-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
jobs:
build:
if: (github.event_name != 'pull_request') || contains(github.event.pull_request.labels.*.name, 'safe to test')
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04
name: 'Integration'
services:
mysql:
Expand Down Expand Up @@ -61,15 +61,7 @@ jobs:
- php-versions: '8.1'
magento-versions: '2.4.5-p1'
magento-editions: 'enterprise'
experimental: true
- php-versions: '8.1'
magento-versions: '2.4.6-beta1'
magento-editions: 'community'
experimental: true
- php-versions: '8.1'
magento-versions: '2.4.6-beta1'
magento-editions: 'enterprise'
experimental: true

continue-on-error: ${{ matrix.experimental }}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
*/
namespace Smile\ElasticsuiteCatalogOptimizer\Ui\Component\Optimizer\Listing;

use Magento\Framework\Data\Collection;
use Magento\Ui\DataProvider\AbstractDataProvider;
use Smile\ElasticsuiteCatalogOptimizer\Model\ResourceModel\Optimizer\CollectionFactory;
use Zend_Db_Expr;

/**
* Data Provider for UI components based on Sellers
Expand Down Expand Up @@ -107,4 +109,26 @@ public function addFilter(\Magento\Framework\Api\Filter $filter)
}
parent::addFilter($filter);
}

/**
* {@inheritdoc}
*/
public function addOrder($field, $direction)
{
// Add custom behavior for boost_weight sorting.
if ($field == 'boost_weight') {
// Put optimizer without boost weight at the end of the list.
parent::addOrder('model', Collection::SORT_ORDER_DESC);

// Extract boost weight value from config field.
parent::addOrder(
new Zend_Db_Expr("SUBSTRING_INDEX(SUBSTRING_INDEX(config, 'constant_score_value\":\"', -1), '\"', 1)"),
$direction
);

return;
}

parent::addOrder($field, $direction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Magento\Search\Controller\Adminhtml\Term;
use Magento\Search\Model\QueryFactory;
use Smile\ElasticsuiteCatalog\Model\Search\PreviewFactory;
use Smile\ElasticsuiteCore\Api\Search\ContextInterface;

/**
* Search term merchandiser preview load controller.
Expand All @@ -43,24 +44,32 @@ class Load extends Term
*/
private $previewFactory;

/**
* @var ContextInterface
*/
private $searchContext;

/**
* Constructor.
*
* @param Context $context Controller context.
* @param QueryFactory $queryFactory Search query factory.
* @param Data $jsonHelper Json Helper.
* @param PreviewFactory $previewFactory Preview factory.
* @param Context $context Controller context.
* @param QueryFactory $queryFactory Search query factory.
* @param Data $jsonHelper Json Helper.
* @param PreviewFactory $previewFactory Preview factory.
* @param ContextInterface $searchContext Search context.
*/
public function __construct(
Context $context,
\Magento\Search\Model\QueryFactory $queryFactory,
Data $jsonHelper,
PreviewFactory $previewFactory
Context $context,
QueryFactory $queryFactory,
Data $jsonHelper,
PreviewFactory $previewFactory,
ContextInterface $searchContext
) {
parent::__construct($context);
$this->queryFactory = $queryFactory;
$this->jsonHelper = $jsonHelper;
$this->previewFactory = $previewFactory;
$this->searchContext = $searchContext;
}

/**
Expand All @@ -77,6 +86,7 @@ public function execute()
$responseData = ['products' => [], 'size' => 0];

if ($query->getId()) {
$this->searchContext->setCurrentSearchQuery($query);
$productPositions = $this->getRequest()->getParam('product_position', []);

$query->setSortedProductIds(array_keys($productPositions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
*/
namespace Smile\ElasticsuiteCatalog\Model\Autocomplete\Product;

use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Fulltext\Collection as ProductCollection;
use Magento\Search\Model\Autocomplete\DataProviderInterface;
use Smile\ElasticsuiteCatalog\Helper\Autocomplete as ConfigurationHelper;
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Fulltext\Collection as ProductCollection;
use Smile\ElasticsuiteCore\Api\Search\ContextInterface;

/**
* Catalog product autocomplete data provider.
Expand Down Expand Up @@ -53,22 +54,30 @@ class DataProvider implements DataProviderInterface
*/
private $productCollection;

/**
* @var ContextInterface
*/
private $searchContext;

/**
* Constructor.
*
* @param ItemFactory $itemFactory Suggest item factory.
* @param Collection\Provider $productCollectionProvider Product collection provider.
* @param ConfigurationHelper $configurationHelper Autocomplete configuration helper.
* @param ContextInterface $searchContext Query search context.
* @param string $type Autocomplete provider type.
*/
public function __construct(
ItemFactory $itemFactory,
Collection\Provider $productCollectionProvider,
ConfigurationHelper $configurationHelper,
ContextInterface $searchContext,
$type = self::AUTOCOMPLETE_TYPE
) {
$this->itemFactory = $itemFactory;
$this->configurationHelper = $configurationHelper;
$this->searchContext = $searchContext;
$this->type = $type;
$this->productCollection = $this->prepareProductCollection($productCollectionProvider->getProductCollection());
}
Expand Down Expand Up @@ -107,6 +116,18 @@ public function getItems()
private function prepareProductCollection(ProductCollection $productCollection)
{
$productCollection->setPageSize($this->getResultsPageSize());
$productCollection->setOrder('relevance', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);

if ($searchQuery = $this->searchContext->getCurrentSearchQuery()) {
if ($searchQuery->getId()) {
$productCollection->addSortFilterParameters(
'relevance',
'search_query.position',
'search_query',
['search_query.query_id' => $searchQuery->getId()]
);
}
}

return $productCollection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@

namespace Smile\ElasticsuiteCatalog\Model\Product\Indexer\Fulltext\Datasource;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\ScopeInterface;
use Smile\ElasticsuiteCore\Api\Index\DatasourceInterface;
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\PriceData as ResourceModel;
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\AttributeData as AttributeResourceModel;

/**
* Datasource used to append prices data to product during indexing.
Expand All @@ -26,36 +30,72 @@
*/
class PriceData implements DatasourceInterface
{
/** @var string */
private const XML_PATH_COMPUTE_CHILD_PRODUCT_DISCOUNT
= 'smile_elasticsuite_catalogsearch_settings/catalogsearch/compute_child_product_discount';

/**
* @var \Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\PriceData
*/
private $resourceModel;

/**
* @var \Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\AttributeData
*/
private $attributeResourceModel;

/**
* Scope configuration
*
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var PriceData\PriceDataReaderInterface[]
*/
private $priceReaderPool = [];

/**
* @var boolean
*/
private $isComputeChildDiscountEnabled;

/**
* Constructor.
*
* @param ResourceModel $resourceModel Resource model
* @param PriceData\PriceDataReaderInterface[] $priceReaderPool Price modifiers pool.
* @param ResourceModel $resourceModel Resource model
* @param AttributeResourceModel $attributeResourceModel Attribute Resource model
* @param PriceData\PriceDataReaderInterface[] $priceReaderPool Price modifiers pool.
* @param ScopeConfigInterface $scopeConfig Scope Config.
*/
public function __construct(ResourceModel $resourceModel, $priceReaderPool = [])
{
$this->resourceModel = $resourceModel;
$this->priceReaderPool = $priceReaderPool;
public function __construct(
ResourceModel $resourceModel,
AttributeResourceModel $attributeResourceModel,
$priceReaderPool = [],
ScopeConfigInterface $scopeConfig = null
) {
$this->resourceModel = $resourceModel;
$this->priceReaderPool = $priceReaderPool;
$this->attributeResourceModel = $attributeResourceModel;
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
}

/**
* Add price data to the index data.
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*
* {@inheritdoc}
*/
public function addData($storeId, array $indexData)
{
$priceData = $this->resourceModel->loadPriceData($storeId, array_keys($indexData));
$productIds = array_keys($indexData);
$priceData = $this->resourceModel->loadPriceData($storeId, $productIds);

if ($this->isComputeChildDiscountEnabled()) {
$allChildrenIds = $this->attributeResourceModel->loadChildrens($productIds, $storeId);
$childPriceData = $this->resourceModel->loadPriceData($storeId, array_keys($allChildrenIds));
}

foreach ($priceData as $priceDataRow) {
$productId = (int) $priceDataRow['entity_id'];
Expand All @@ -65,10 +105,27 @@ public function addData($storeId, array $indexData)
$originalPrice = $priceModifier->getOriginalPrice($priceDataRow);
$price = $priceModifier->getPrice($priceDataRow);

$isDiscount = $price < $originalPrice;

if ($this->isComputeChildDiscountEnabled()) {
if (in_array($productTypeId, $this->attributeResourceModel->getCompositeTypes())) {
$isDiscount = false;
$priceModifier = $this->getPriceDataReader('default');
foreach ($childPriceData as $childPrice) {
if ($childPrice['customer_group_id'] == $priceDataRow['customer_group_id']) {
if ($priceModifier->getPrice($childPrice) < $priceModifier->getOriginalPrice($childPrice)) {
$isDiscount = true;
break;
}
}
}
}
}

$indexData[$productId]['price'][] = [
'price' => (float) $price,
'original_price' => (float) $originalPrice,
'is_discount' => $price < $originalPrice,
'is_discount' => $isDiscount,
'customer_group_id' => (int) $priceDataRow['customer_group_id'],
'tax_class_id' => (int) $priceDataRow['tax_class_id'],
'final_price' => (float) $priceDataRow['final_price'],
Expand Down Expand Up @@ -103,4 +160,21 @@ private function getPriceDataReader($typeId)

return $priceModifier;
}

/**
* Is computing child product discount enabled.
*
* @return bool
*/
private function isComputeChildDiscountEnabled(): bool
{
if (!isset($this->isIndexingChildProductSkuEnabled)) {
$this->isComputeChildDiscountEnabled = (bool) $this->scopeConfig->getValue(
self::XML_PATH_COMPUTE_CHILD_PRODUCT_DISCOUNT,
ScopeInterface::SCOPE_STORE
);
}

return $this->isComputeChildDiscountEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -704,13 +704,13 @@ private function prepareSortOrders()
{
$sortOrders = [];

$useProductuctLimitation = isset($this->_productLimitationFilters['sortParams']);
$useProductLimitation = isset($this->_productLimitationFilters['sortParams']);

foreach ($this->_orders as $attribute => $direction) {
$sortParams = ['direction' => $direction];
$sortField = $this->mapFieldName($attribute);

if ($useProductuctLimitation && isset($this->_productLimitationFilters['sortParams'][$attribute])) {
if ($useProductLimitation && isset($this->_productLimitationFilters['sortParams'][$attribute])) {
$sortField = $this->_productLimitationFilters['sortParams'][$attribute]['sortField'];
$sortParams = array_merge($sortParams, $this->_productLimitationFilters['sortParams'][$attribute]);
} elseif ($attribute == 'price') {
Expand Down
7 changes: 6 additions & 1 deletion src/module-elasticsuite-catalog/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,16 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[If enabled, child products SKUs of composite products will be indexed in a separate field and the "sku" field will only contain the parent product sku.]]></comment>
</field>
<field id="include_child_attributes" translate="label comment" type="select" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<field id="include_child_attributes" translate="label comment" type="select" sortOrder="55" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Include child attributes during indexing</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[If enabled, attributes of associated child products will be included with their parents when indexing. This will impact what filters are shown in category pages. For example, if a child of a bundle product has a particular colour then its parent will have this colour associated with it. <br><strong>Note when changing this setting</strong>, it is recommended to perform a full reindex of the <q>Catalog Search</q> indexer.]]></comment>
</field>
<field id="compute_child_product_discount" translate="label" type="select" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Enable indexing discount on child products</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[Enable this if your catalog contains configurable products that are having childrens with different prices that could have separated discounts.]]></comment>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions src/module-elasticsuite-catalog/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<adaptive_slider_enabled>0</adaptive_slider_enabled>
<index_child_product_sku>0</index_child_product_sku>
<include_child_attributes>1</include_child_attributes>
<compute_child_product_discount>0</compute_child_product_discount>
</catalogsearch>
</smile_elasticsuite_catalogsearch_settings>
</default>
Expand Down
2 changes: 2 additions & 0 deletions src/module-elasticsuite-catalog/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,5 @@ Attributes,Attributes
"If the category can be displayed in autocomplete results.","If the category can be displayed in autocomplete results."
"Enable indexing child product SKU in dedicated subfield","Enable indexing child product SKU in dedicated subfield"
"If enabled, child products SKUs of composite products will be indexed in a separate field and the ""sku"" field will only contain the parent product sku.","If enabled, child products SKUs of composite products will be indexed in a separate field and the ""sku"" field will only contain the parent product sku."
"Enable indexing discount on child products","Enable indexing discount on child products"
"Enable this if your catalog contains configurable products that are having childrens with different prices that could have separated discounts.","Enable this if your catalog contains configurable products that are having childrens with different prices that could have separated discounts."
2 changes: 2 additions & 0 deletions src/module-elasticsuite-catalog/i18n/fr_FR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,5 @@ Attributes,Attributs
"If the category can be displayed in autocomplete results.","Si activé, la catégorie pourra être affichée dans les résultats de l'autocomplétion"
"Enable indexing child product SKU in dedicated subfield","Indexer les SKUs des produits enfants séparéments"
"If enabled, child products SKUs of composite products will be indexed in a separate field and the ""sku"" field will only contain the parent product sku.","Si activé, les SKUs des produits enfants des produits composites seront stockés dans un champ à part. Le champ ""sku"" du parent ne contiendra que son SKU."
"Enable indexing discount on child products","Calculer les discounts sur les produits enfants"
"Enable this if your catalog contains configurable products that are having childrens with different prices that could have separated discounts.","Activez cette option si votre catalogue contient des produits configurables ayant des enfants dont les prix de base sont différents et pouvant avoir des prix barrés différents."
Loading

0 comments on commit 938ec1a

Please sign in to comment.