Skip to content

Commit

Permalink
fix: update product listing ratings on updateProduct event
Browse files Browse the repository at this point in the history
  • Loading branch information
tblivet committed May 31, 2024
1 parent 59be87a commit 8c45500
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions views/js/productListingComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ var productListingComments = (function () {
prestashop.on('updateProductList', function() {
addProductsIDs();
});

prestashop.on("updatedProduct", function () {
updateRatings();
});
}


Expand Down Expand Up @@ -138,6 +142,56 @@ var productListingComments = (function () {
});
}

function updateRatings() {
// Collect all product IDs that need to be updated
var productIDs = [];
var $productsList = $(DOMStrings.productContainer);

$productsList.each(function () {
var productID = $(this).find(DOMStrings.productListReviewsContainer).data("id");
if (productID) {
productIDs.push(productID);
}
});

if (productIDs.length > 0) {
$.get(
data.ajaxUrl,
{ id_products: productIDs },
function (jsonData) {
if (jsonData && jsonData.products.length > 0) {
// Create a map of product data
var productDataMap = {};
jsonData.products.forEach(function (product) {
productDataMap[product.id_product] = product;
});

// Update each product's ratings
$productsList.each(function () {
var $self = $(this);
var productID = $self.find(DOMStrings.productListReviewsContainer).data("id");

if (productDataMap[productID]) {
var productData = productDataMap[productID];
var $productReviewContainer = $self.find(DOMStrings.productListReviewsContainer);

if (productData.comments_nb > 0) {
$productReviewContainer
.find(DOMStrings.productListReviewsStarsContainer)
.rating({ grade: productData.average_grade, starWidth: 16 });
$productReviewContainer
.find(DOMStrings.productListReviewsNumberOfComments)
.text("(" + productData.comments_nb + ")");
$self.addClass(DOMClasses.hasReviews);
$productReviewContainer.css("visibility", "visible");
}
}
});
}
}
);
}
}

return {
load: function () {
Expand Down

0 comments on commit 8c45500

Please sign in to comment.