Skip to content

Commit

Permalink
Merge pull request #1336 from algolia/develop
Browse files Browse the repository at this point in the history
Release/3.10.2
  • Loading branch information
mohitalgolia authored Apr 4, 2023
2 parents b0fc4de + 7326355 commit 4cb4e40
Show file tree
Hide file tree
Showing 13 changed files with 582 additions and 516 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# CHANGE LOG

## 3.10.2

### UPDATES
- Ensured compatibility of the extension with Magento 2.4.6
- Updated the code to allow for convenient customization of object serialization
- Enhanced comments for non-castable attributes in system configuration
- Updated the code to ensure that accurate prices are indexed for bundle products when the dynamic price attribute is turned off
- Updated autocomplete to include userToken and enablePersonalization tag in search requests



### Bug Fixes
- Fixed issue with section.label in autocomplete
- Resolved issue with nbOfQuerySuggestions in autocomplete
- Fixed routing error related to disabling searchBox for instant search page
- Resolved deployment issue with prefixed Magento database tables
- Fixed spacing issue with pagination on instant search page


## 3.10.1

### UPDATES
Expand Down
45 changes: 25 additions & 20 deletions Helper/Entity/Product/PriceManager/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ class Bundle extends ProductWithChildren
*/
protected function addAdditionalData($product, $withTax, $subProducts, $currencyCode, $field) {
$data = $this->getMinMaxPrices($product, $withTax, $subProducts, $currencyCode);
$dashedFormat = $this->getDashedPriceFormat($data['min_price'], $data['max'], $currencyCode);
if ($data['min_price'] !== $data['max']) {
$this->handleBundleNonEqualMinMaxPrices($field, $currencyCode, $data['min_price'], $data['max'], $dashedFormat);
$dashedFormat = $this->getDashedPriceFormat($data['min_price'], $data['max_price'], $currencyCode);
if ($data['min_price'] !== $data['max_price']) {
$this->handleBundleNonEqualMinMaxPrices($field, $currencyCode, $data['min_price'], $data['max_price'], $dashedFormat);
}
$this->handleOriginalPrice($field, $currencyCode, $data['min_price'], $data['max'], $data['min_original'], $data['max_original']);

$this->handleOriginalPrice($field, $currencyCode, $data['min_price'], $data['max_price'], $data['min_original'], $data['max_original']);
if (!$this->customData[$field][$currencyCode]['default']) {
$this->handleZeroDefaultPrice($field, $currencyCode, $data['min_price'], $data['max']);
$this->handleZeroDefaultPrice($field, $currencyCode, $data['min_price'], $data['max_price']);
}
if ($this->areCustomersGroupsEnabled) {
$groupedDashedFormat = $this->getBundleDashedPriceFormat($data['min'], $data['max'], $currencyCode);
Expand All @@ -42,25 +43,29 @@ protected function addAdditionalData($product, $withTax, $subProducts, $currency
*/
protected function getMinMaxPrices(Product $product, $withTax, $subProducts, $currencyCode)
{
$regularPrice = $product->getPriceInfo()->getPrice('regular_price')->getMinimalPrice()->getValue();
$product->setData('website_id', $product->getStore()->getWebsiteId());
$minPrice = $product->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getValue();
$minOriginalPrice = $product->getPriceInfo()->getPrice('regular_price')->getMinimalPrice()->getValue();
$maxOriginalPrice = $product->getPriceInfo()->getPrice('regular_price')->getMaximalPrice()->getValue();
$max = $product->getPriceInfo()->getPrice('final_price')->getMaximalPrice()->getValue();
$minArray = [];
$maxArray = [];
foreach ($this->groups as $group) {
$groupId = (int) $group->getData('customer_group_id');
foreach ($subProducts as $subProduct) {
$subProduct->setData('customer_group_id', $groupId);
$subProductFinalPrice = $this->getTaxPrice($product, $subProduct->getPriceModel()->getFinalPrice(1, $subProduct), $withTax);
$priceDiff = $subProduct->getPrice() - $subProductFinalPrice;
$minArray[$groupId][] = $regularPrice - $priceDiff;
}
$product->setData('customer_group_id', $groupId);
$minPrice = $product->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getValue();
$minArray[$groupId] = $product->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getValue();
$maxArray[$groupId] = $product->getPriceInfo()->getPrice('final_price')->getMaximalPrice()->getValue();
$product->setData('customer_group_id', null);
}

$minPriceArray = [];
foreach ($minArray as $groupId => $min) {
$minPriceArray[$groupId] = min($min);
$minPriceArray[$groupId] = $min;
}
$maxPriceArray = [];
foreach ($maxArray as $groupId => $max) {
$maxPriceArray[$groupId] = $max;
}

if ($currencyCode !== $this->baseCurrencyCode) {
Expand All @@ -70,15 +75,15 @@ protected function getMinMaxPrices(Product $product, $withTax, $subProducts, $cu
foreach ($minPriceArray as $groupId => $price) {
$minPriceArray[$groupId] = $this->convertPrice($price, $currencyCode);
}
if ($min !== $max) {
if ($minPrice !== $max) {
$max = $this->convertPrice($max, $currencyCode);
}
}

return [
'min' => $minPriceArray,
'max' => $max,
'max' => $maxPriceArray,
'min_price' => $minPrice,
'max_price' => $max,
'min_original' => $minOriginalPrice,
'max_original' => $maxOriginalPrice
];
Expand Down Expand Up @@ -117,10 +122,10 @@ protected function handleBundleNonEqualMinMaxPrices($field, $currencyCode, $min,
protected function getBundleDashedPriceFormat($minPrices, $max, $currencyCode) {
$dashedFormatPrice = [];
foreach ($minPrices as $groupId => $min) {
if ($min === $max) {
if ($min === $max[$groupId]) {
$dashedFormatPrice [$groupId] = '';
}
$dashedFormatPrice[$groupId] = $this->formatPrice($min, $currencyCode) . ' - ' . $this->formatPrice($max, $currencyCode);
$dashedFormatPrice[$groupId] = $this->formatPrice($min, $currencyCode) . ' - ' . $this->formatPrice($max[$groupId], $currencyCode);
}
return $dashedFormatPrice;
}
Expand All @@ -139,12 +144,12 @@ protected function setFinalGroupPricesBundle($field, $currencyCode, $min, $max,
foreach ($this->groups as $group) {
$groupId = (int) $group->getData('customer_group_id');
$this->customData[$field][$currencyCode]['group_' . $groupId] = $min[$groupId];
if ($min === $max) {
if ($min[$groupId] === $max[$groupId]) {
$this->customData[$field][$currencyCode]['group_' . $groupId . '_formated'] =
$this->customData[$field][$currencyCode]['default_formated'];
} else {
$this->customData[$field][$currencyCode]['group_' . $groupId . '_formated'] = $dashedFormat[$groupId];
}
}
}
}
}
107 changes: 63 additions & 44 deletions Helper/Entity/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,24 @@ class ProductHelper
* @param ImageHelper $imageHelper
*/
public function __construct(
Config $eavConfig,
ConfigHelper $configHelper,
AlgoliaHelper $algoliaHelper,
Logger $logger,
StoreManagerInterface $storeManager,
ManagerInterface $eventManager,
Visibility $visibility,
Stock $stockHelper,
Config $eavConfig,
ConfigHelper $configHelper,
AlgoliaHelper $algoliaHelper,
Logger $logger,
StoreManagerInterface $storeManager,
ManagerInterface $eventManager,
Visibility $visibility,
Stock $stockHelper,
StockRegistryInterface $stockRegistry,
CurrencyHelper $currencyManager,
CategoryHelper $categoryHelper,
PriceManager $priceManager,
Type $productType,
CollectionFactory $productCollectionFactory,
GroupCollection $groupCollection,
ImageHelper $imageHelper
) {
CurrencyHelper $currencyManager,
CategoryHelper $categoryHelper,
PriceManager $priceManager,
Type $productType,
CollectionFactory $productCollectionFactory,
GroupCollection $groupCollection,
ImageHelper $imageHelper
)
{
$this->eavConfig = $eavConfig;
$this->configHelper = $configHelper;
$this->algoliaHelper = $algoliaHelper;
Expand Down Expand Up @@ -295,7 +296,8 @@ public function getProductCollectionQuery(
$productIds = null,
$onlyVisible = true,
$includeNotVisibleIndividually = false
) {
)
{
$productCollection = $this->productCollectionFactory->create();
$products = $productCollection
->setStoreId($storeId)
Expand Down Expand Up @@ -401,12 +403,12 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
$attributesForFaceting = $this->getAttributesForFaceting($storeId);

$indexSettings = [
'searchableAttributes' => $searchableAttributes,
'customRanking' => $customRanking,
'searchableAttributes' => $searchableAttributes,
'customRanking' => $customRanking,
'unretrievableAttributes' => $unretrievableAttributes,
'attributesForFaceting' => $attributesForFaceting,
'maxValuesPerFacet' => (int) $this->configHelper->getMaxValuesPerFacet($storeId),
'removeWordsIfNoResults' => $this->configHelper->getRemoveWordsIfNoResult($storeId),
'attributesForFaceting' => $attributesForFaceting,
'maxValuesPerFacet' => (int)$this->configHelper->getMaxValuesPerFacet($storeId),
'removeWordsIfNoResults' => $this->configHelper->getRemoveWordsIfNoResult($storeId),
];

// Additional index settings from event observer
Expand All @@ -419,7 +421,7 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
$this->eventManager->dispatch(
'algolia_products_index_before_set_settings',
[
'store_id' => $storeId,
'store_id' => $storeId,
'index_settings' => $transport,
]
);
Expand Down Expand Up @@ -485,7 +487,7 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
} else {
foreach ($sortingIndices as $values) {
$replicaName = $values['name'];
array_unshift($customRanking,$values['ranking'][0]);
array_unshift($customRanking, $values['ranking'][0]);
$replicaSetting['customRanking'] = $customRanking;
$this->algoliaHelper->setSettings($replicaName, $replicaSetting, false, false);
$this->logger->log('Setting settings to "' . $replicaName . '" replica.');
Expand Down Expand Up @@ -578,12 +580,12 @@ public function getObject(Product $product)
];

$customData = [
'objectID' => $product->getId(),
'name' => $product->getName(),
'url' => $product->getUrlModel()->getUrl($product, $urlParams),
'visibility_search' => (int) (in_array($visibility, $visibleInSearch)),
'visibility_catalog' => (int) (in_array($visibility, $visibleInCatalog)),
'type_id' => $product->getTypeId(),
'objectID' => $product->getId(),
'name' => $product->getName(),
'url' => $product->getUrlModel()->getUrl($product, $urlParams),
'visibility_search' => (int)(in_array($visibility, $visibleInSearch)),
'visibility_catalog' => (int)(in_array($visibility, $visibleInCatalog)),
'type_id' => $product->getTypeId(),
];

$additionalAttributes = $this->getAdditionalAttributes($product->getStoreId());
Expand Down Expand Up @@ -886,7 +888,7 @@ protected function addStockQty($defaultData, $customData, $additionalAttributes,

$stockItem = $this->stockRegistry->getStockItem($product->getId());
if ($stockItem) {
$customData['stock_qty'] = (int) $stockItem->getQty();
$customData['stock_qty'] = (int)$stockItem->getQty();
}
}

Expand Down Expand Up @@ -980,7 +982,7 @@ protected function addNullValue($customData, $subProducts, $attribute, Attribute
}

if (is_array($values) && count($values) > 0) {
$customData[$attributeName] = array_values(array_unique($values));
$customData[$attributeName] = $this->getSanitizedArrayValues($values, $attributeName);
}

if (count($subProductImages) > 0) {
Expand All @@ -991,12 +993,27 @@ protected function addNullValue($customData, $subProducts, $attribute, Attribute
}

/**
* @param $valueText
* @param Product $subProduct
* @param AttributeResource $attributeResource
* By default Algolia will remove all redundant attribute values that are fetched from the child simple products.
*
* Overridable via Preference to allow implementer to enforce their own uniqueness rules while leveraging existing indexing code.
* e.g. $values = (in_array($attributeName, self::NON_UNIQUE_ATTRIBUTES)) ? $values : array_unique($values);
*
* @param array $values
* @param string $attributeName
* @return array
*/
protected function getSanitizedArrayValues(array $values, string $attributeName): array
{
return array_values(array_unique($values));
}

/**
* @param string|array $valueText - bit of a misnomer - essentially the retrieved values to be indexed for a given product's attribute
* @param Product $subProduct - the simple product to index
* @param AttributeResource $attributeResource - the attribute being indexed
* @return array
*/
protected function getValues($valueText, Product $subProduct, AttributeResource $attributeResource)
protected function getValues($valueText, Product $subProduct, AttributeResource $attributeResource): array
{
$values = [];

Expand Down Expand Up @@ -1065,7 +1082,8 @@ protected function addNonNullValue(
Product $product,
$attribute,
AttributeResource $attributeResource
) {
)
{
$valueText = null;

if (!is_array($value) && $attributeResource->usesSource()) {
Expand Down Expand Up @@ -1165,7 +1183,7 @@ protected function getAttributesForFaceting($storeId)

if ($this->configHelper->isCustomerGroupsEnabled($storeId)) {
foreach ($this->groupCollection as $group) {
$group_id = (int) $group->getData('customer_group_id');
$group_id = (int)$group->getData('customer_group_id');

$attributesForFaceting[] = 'price.' . $currency_code . '.group_' . $group_id;
}
Expand Down Expand Up @@ -1338,10 +1356,10 @@ public function canProductBeReindexed($product, $storeId, $isChildProduct = fals
}

if ($isChildProduct === false && !in_array($product->getVisibility(), [
Visibility::VISIBILITY_BOTH,
Visibility::VISIBILITY_IN_SEARCH,
Visibility::VISIBILITY_IN_CATALOG,
])) {
Visibility::VISIBILITY_BOTH,
Visibility::VISIBILITY_IN_SEARCH,
Visibility::VISIBILITY_IN_CATALOG,
])) {
throw (new ProductNotVisibleException())
->withProduct($product)
->withStoreId($storeId);
Expand Down Expand Up @@ -1381,10 +1399,11 @@ public function productIsInStock($product, $storeId)
* @param $replica
* @return array
*/
protected function handleVirtualReplica($replicas, $indexName) {
protected function handleVirtualReplica($replicas, $indexName)
{
$virtualReplicaArray = [];
foreach ($replicas as $replica) {
$virtualReplicaArray[] = 'virtual('.$replica.')';
$virtualReplicaArray[] = 'virtual(' . $replica . ')';
}
return $virtualReplicaArray;
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Algolia Search for Magento 2
==================

![Latest version](https://img.shields.io/badge/latest-3.10.1-green)
![Latest version](https://img.shields.io/badge/latest-3.10.2-green)
![Magento 2](https://img.shields.io/badge/Magento-2.4.x-orange)

![PHP](https://img.shields.io/badge/PHP-8.1,7.4-blue)
Expand Down
2 changes: 1 addition & 1 deletion Setup/Patch/Schema/RecommendConfigPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function apply()

$this->moduleDataSetup->getConnection()->startSetup();
$connection = $this->moduleDataSetup->getConnection();
$table = $connection->getTableName('core_config_data');
$table = $this->moduleDataSetup->getTable('core_config_data');
foreach ($movedConfigDirectives as $from => $to) {
try {
$connection->query('UPDATE ' . $table . ' SET path = "' . $to . '" WHERE path = "' . $from . '"');
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Algolia Search integration for Magento 2",
"type": "magento2-module",
"license": ["MIT"],
"version": "3.10.1",
"version": "3.10.2",
"require": {
"magento/framework": "~102.0|~103.0",
"algolia/algoliasearch-client-php": "3.2",
Expand Down
2 changes: 1 addition & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<comment>
<![CDATA[
Choose here the textfield attributes that you don't want to be turned into numeric values (i.e. : if you don't want "00123" turned into "123")
Choose here the textfield/non-castable attributes that you don't want to be turned into numeric values (i.e. : if you don't want "00123" turned into "123" or "34567834545672" turned into "345678E+2")
]]>
</comment>
</field>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Algolia_AlgoliaSearch" setup_version="3.10.1">
<module name="Algolia_AlgoliaSearch" setup_version="3.10.2">
<sequence>
<module name="Magento_Theme"/>
<module name="Magento_Backend"/>
Expand Down
Loading

0 comments on commit 4cb4e40

Please sign in to comment.