From 713a66455509de24e405f7ce8ecfc1bada7b9460 Mon Sep 17 00:00:00 2001 From: Jean-Michel Batty Date: Thu, 22 Feb 2024 15:13:22 -0600 Subject: [PATCH 1/3] account for url breaking characters some characters break the call to retrieve offers, fix applies to both offer retrieval and product sync, to have consistent category names --- Model/Api/Request/ProductDataBuilder.php | 30 ++++++++++++++++++- view/frontend/web/js/warranty-offers-base.js | 31 +++++++++++++++++--- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/Model/Api/Request/ProductDataBuilder.php b/Model/Api/Request/ProductDataBuilder.php index 42ec2c4e..4df2e7e2 100644 --- a/Model/Api/Request/ProductDataBuilder.php +++ b/Model/Api/Request/ProductDataBuilder.php @@ -266,7 +266,7 @@ private function getCategories(ProductInterface $product): string $names = []; foreach ($pathIds as $id) { if (isset($parentCategories[$id]) && $parentCategories[$id]->getName()) { - $names[] = $parentCategories[$id]->getName(); + $names[] = $this->sanitizeCategoryName($parentCategories[$id]->getName()); } } $categories[] = implode(self::DELIMITER_CATEGORY, $names); @@ -276,6 +276,34 @@ private function getCategories(ProductInterface $product): string return implode(',', $categories); } + /** + * Returns sanitized value for payload + * + * @param string|null $theString + * @return string|null + */ + private function sanitizeCategoryName(?string $theString): ?string + { + if (!$theString) + return null; + + // Use a regular expression to find HTML-encoded sections (e.g., %25) + $encodedSectionRegex = '/%[0-9A-Fa-f]{2}/'; + + // Decode HTML-encoded values using a callback function + $decodedString = preg_replace_callback($encodedSectionRegex, function($match) { + return urldecode($match[0]); + }, $theString); + + // Replace remaining breaking characters + $resultString = str_replace('%', 'pct ', $decodedString); + $resultString = str_replace('?', '.', $resultString); + $resultString = str_replace('#', '.', $resultString); + $resultString = str_replace('&', 'and ', $resultString); + + return $resultString; + } + /** * Get product image url * diff --git a/view/frontend/web/js/warranty-offers-base.js b/view/frontend/web/js/warranty-offers-base.js index 3d8ef6d9..482c3ab2 100644 --- a/view/frontend/web/js/warranty-offers-base.js +++ b/view/frontend/web/js/warranty-offers-base.js @@ -31,7 +31,7 @@ define([ Extend.buttons.render(this.element.get(0), { referenceId: this.options.productSku, - category: this.options.productInfo.category, + category: this.sanitizeValue(this.options.productInfo.category), price:this.options.productInfo.price }); }, @@ -47,7 +47,7 @@ define([ Extend.buttons.renderSimpleOffer(this.element.get(0), { referenceId: this.options.productSku, - category: this.options.productInfo.category, + category: this.sanitizeValue(this.options.productInfo.category), price:this.options.productInfo.price, onAddToCart: function (data) { var warranty = data.plan; @@ -61,7 +61,30 @@ define([ } }); }, + + /** + * Returns sanitized value for payload + * @param {String} theString + * @return {string|null} + */ + sanitizeValue: function (theString ) { + if (!theString) + return; + + // Use a regular expression to find HTML-encoded sections (e.g., %25) + var encodedSectionRegex = /%[0-9A-Fa-f]{2}/g; + // Replace each HTML-encoded section with its decoded equivalent + var decodedString = theString.replace(encodedSectionRegex, function(match) { + return decodeURIComponent(match); + }); + + // replace breaking characters with something non breaking + var theSanitizedString = decodedString.replace(/%/g, "pct ").replace(/\?/g, ".").replace(/#/g, ".").replace(/&/g, "and"); + + return theSanitizedString; + }, + /** * Returns current warranty offers block instance * @@ -86,7 +109,7 @@ define([ let activeProduct = { referenceId:productSku, price: product.price, - category: product.category + category: this.sanitizeValue(product.category) }; component.setActiveProduct(activeProduct); } @@ -108,7 +131,7 @@ define([ Extend.modal.open({ referenceId: productSku, price:productInfo.price, - category:productInfo.category, + category: this.sanitizeValue(productInfo.category), onClose: closeCallback.bind(this) }); }, From 5e0b84455ce26bbd90009ff957ae555ab723c7df Mon Sep 17 00:00:00 2001 From: Jean-Michel Batty Date: Thu, 22 Feb 2024 15:25:34 -0600 Subject: [PATCH 2/3] Update warranty-offers-base.js readibility --- view/frontend/web/js/warranty-offers-base.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/view/frontend/web/js/warranty-offers-base.js b/view/frontend/web/js/warranty-offers-base.js index 482c3ab2..1e0aa601 100644 --- a/view/frontend/web/js/warranty-offers-base.js +++ b/view/frontend/web/js/warranty-offers-base.js @@ -79,7 +79,7 @@ define([ return decodeURIComponent(match); }); - // replace breaking characters with something non breaking + // replace breaking characters var theSanitizedString = decodedString.replace(/%/g, "pct ").replace(/\?/g, ".").replace(/#/g, ".").replace(/&/g, "and"); return theSanitizedString; From f0260b9d565095ee09bff2109cfbd753f78f6fdf Mon Sep 17 00:00:00 2001 From: Jean-Michel Batty Date: Thu, 22 Feb 2024 15:27:30 -0600 Subject: [PATCH 3/3] Update config.xml prep for new version --- etc/config.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/config.xml b/etc/config.xml index 9f4e54e1..ecfadb14 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -2,7 +2,7 @@ - 2.2.8 + 2.2.9 0