Skip to content

Commit

Permalink
Minor Release - Merge pull request #404 from helloextend/CategoryFix
Browse files Browse the repository at this point in the history
Minor Release - account for url breaking characters
  • Loading branch information
jm-extend authored Feb 22, 2024
2 parents 8b52b2b + f0260b9 commit 6967edb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
30 changes: 29 additions & 1 deletion Model/Api/Request/ProductDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<default>
<warranty>
<version>
<tag>2.2.8</tag>
<tag>2.2.9</tag>
</version>
<enableExtend>
<enable>0</enable>
Expand Down
31 changes: 27 additions & 4 deletions view/frontend/web/js/warranty-offers-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
},
Expand All @@ -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;
Expand All @@ -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
var theSanitizedString = decodedString.replace(/%/g, "pct ").replace(/\?/g, ".").replace(/#/g, ".").replace(/&/g, "and");

return theSanitizedString;
},

/**
* Returns current warranty offers block instance
*
Expand All @@ -86,7 +109,7 @@ define([
let activeProduct = {
referenceId:productSku,
price: product.price,
category: product.category
category: this.sanitizeValue(product.category)
};
component.setActiveProduct(activeProduct);
}
Expand All @@ -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)
});
},
Expand Down

0 comments on commit 6967edb

Please sign in to comment.