From 9f685ceeb59750d7f9d111509b3962dc248d5708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannu=20Po=CC=88lo=CC=88nen?= Date: Fri, 12 Jun 2020 09:08:15 +0300 Subject: [PATCH 1/2] Fix a bug in configurable product prices being zero when taxes are included in product display prices --- CHANGELOG.md | 3 +++ Helper/Price.php | 35 ++++++++++++++++++++++++++++------- composer.json | 2 +- etc/module.xml | 2 +- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a57ad2aed..01cb17b9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning. +# 5.0.1 +* Fix an issue with configurable product prices being zero in Nosto product data when taxes are included in display prices + # 5.0.0 * Refactor the indexing logic to use batched queues & decouple the caching logic from product updates * Use Magento's built-in caching logic for caching Nosto product data diff --git a/Helper/Price.php b/Helper/Price.php index 1c4e67d7c..6616395dd 100644 --- a/Helper/Price.php +++ b/Helper/Price.php @@ -54,6 +54,7 @@ use Magento\Store\Model\Store; use Magento\Tax\Helper\Data as TaxHelper; use Magento\Tax\Model\Config as TaxConfig; +use Nosto\NostoException; use Nosto\Tagging\Model\Product\Repository as NostoProductRepository; /** @@ -67,6 +68,10 @@ class Price extends AbstractHelper private $nostoProductRepository; private $taxHelper; + const KEY_SKU_FINAL_PRICE = 'final_price'; + const KEY_SKU_PRICE = 'price'; + const KEY_SKU_PRODUCT_ID = 'entity_id'; + /** * Constructor. * @@ -100,6 +105,7 @@ public function __construct( * @param Store $store * @return float * @throws LocalizedException + * @throws NostoException */ public function getProductDisplayPrice(Product $product, Store $store) { @@ -119,9 +125,11 @@ public function getProductDisplayPrice(Product $product, Store $store) * @param bool $inclTax if tax is to be included. * @param bool $finalPrice if final price. * @return float + * @throws LocalizedException + * @throws NoSuchEntityException + * @throws NostoException * @suppress PhanTypeMismatchArgument * @suppress PhanDeprecatedFunction - * @throws LocalizedException */ public function getProductPrice(// @codingStandardsIgnoreLine Product $product, @@ -183,6 +191,7 @@ public function getProductPrice(// @codingStandardsIgnoreLine * @param Store $store * @return float * @throws LocalizedException + * @throws NostoException */ public function getProductFinalDisplayPrice(Product $product, Store $store) { @@ -268,6 +277,7 @@ public function addTaxDisplayPriceIfApplicable(Product $product, Store $store, $ * @param Store $store * @return array|float|int|mixed * @throws LocalizedException + * @throws NostoException */ private function getBundleProductPrice(Product $product, $finalPrice, $inclTax, Store $store) { @@ -368,6 +378,7 @@ private function getGroupedProductPrice(Product $product, $finalPrice, $inclTax) * @return float * @throws LocalizedException * @throws NoSuchEntityException + * @throws NostoException */ private function getConfigurableProductPrice(Product $product, $finalPrice, $inclTax, Store $store) { @@ -379,16 +390,26 @@ private function getConfigurableProductPrice(Product $product, $finalPrice, $inc if (count($skuPrices) === 0) { return $price; } - $priceColumn = array_column($skuPrices, "final_price"); + $priceColumn = array_column($skuPrices, self::KEY_SKU_FINAL_PRICE); array_multisort($priceColumn, SORT_ASC, $skuPrices); $minSku = reset($skuPrices); - if ($finalPrice && isset($minSku['final_price'])) { - $price = $minSku['final_price']; - } elseif (isset($minSku['final_price'])) { - $price = $minSku['price']; + if ($finalPrice && isset($minSku[self::KEY_SKU_FINAL_PRICE])) { + $price = $minSku[self::KEY_SKU_FINAL_PRICE]; + } elseif (isset($minSku[self::KEY_SKU_PRICE])) { + $price = $minSku[self::KEY_SKU_PRICE]; } if ($inclTax === true) { - $skuResult = $this->nostoProductRepository->getByIds([$minSku['product_id']]); + if (!isset($minSku[self::KEY_SKU_PRODUCT_ID])) { + throw new NostoException( + sprintf( + 'No %s key in sku prices array. Array content: %s', + self::KEY_SKU_PRODUCT_ID, + print_r($minSku, true) + ) + ); + + } + $skuResult = $this->nostoProductRepository->getByIds([$minSku[self::KEY_SKU_PRODUCT_ID]]); $skuProducts = $skuResult->getItems(); $skuProduct = reset($skuProducts); diff --git a/composer.json b/composer.json index 5a1ab4228..e17cee1f1 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nosto/module-nostotagging", "description": "Increase your conversion rate and average order value by delivering your customers personalized product recommendations throughout their shopping journey.", "type": "magento2-module", - "version": "5.0.0", + "version": "5.0.1", "require-dev": { "php": ">=7.1.0", "phpmd/phpmd": "^2.5", diff --git a/etc/module.xml b/etc/module.xml index 01e6694a1..943e505ab 100755 --- a/etc/module.xml +++ b/etc/module.xml @@ -37,5 +37,5 @@ - + From dde9d875978ef5d9673a30c84c42879626212e24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannu=20Po=CC=88lo=CC=88nen?= Date: Fri, 12 Jun 2020 09:26:04 +0300 Subject: [PATCH 2/2] Remove print_r usage --- Helper/Price.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Helper/Price.php b/Helper/Price.php index 6616395dd..a53e433c1 100644 --- a/Helper/Price.php +++ b/Helper/Price.php @@ -400,14 +400,18 @@ private function getConfigurableProductPrice(Product $product, $finalPrice, $inc } if ($inclTax === true) { if (!isset($minSku[self::KEY_SKU_PRODUCT_ID])) { + // Since print_r is discouraged we use this + $arrayContents = ''; + foreach ($minSku as $key => $val) { + $arrayContents .= sprintf('%s => %s, ', $key, $val); + } throw new NostoException( sprintf( 'No %s key in sku prices array. Array content: %s', self::KEY_SKU_PRODUCT_ID, - print_r($minSku, true) + $arrayContents ) ); - } $skuResult = $this->nostoProductRepository->getByIds([$minSku[self::KEY_SKU_PRODUCT_ID]]); $skuProducts = $skuResult->getItems();