如何计算每个产品的评论并在大型商业的产品列表中显示?

时间:2016-12-18 14:17:27

标签: bigcommerce

请找到附带的图片,它会给你一个想法 我想计算每个产品的评论 enter image description here

请找到附件代码

const isoRoutes = ['/home', '/about'];
app.get([isoRoutes], (req, res))

2 个答案:

答案 0 :(得分:1)

可以在类别页面上插入此jQuery代码段,以异步方式访问每个产品,并确定页面上的评论数量。

// For each product on the category page...
$('.ProductDetails').each(function(index) {
  var $self = $(this); // Make local reference to 'this'.
  // Parse the URL from the individual product, and retrieve its Reviews Count:
  getProductPageReviewCount($self.find('>:first-child').attr('href')).then(function(count) {
    // Insert the number of reviews below the Product Name:
    $self.find('>:first-child').after("<a>" +count +" Reviews</a>");
  }).catch(function(err) {
    // Catch any Ajax Errors:
    console.log('Error - ', err);
  });
});

  /**
   * Determines the total number of reviews for a particular
   * external product page, according to its URL. 
   * @param url <string>   - The product page's URL. 
   * @return Promise\<int> - The number of reviews on the page, or error on failed request.
   */
  function getProductPageReviewCount(url) {
    return new Promise(function(fulfill, reject) {
        $.ajax({
          method: 'GET',
          url: url,
          success: function(res) {
            fulfill($(res).find('.ProductReviewList > li').length);
          },
          error: function(err) {
            reject(err);
          }
        });
    });
  }

答案 1 :(得分:0)

$(document).ready(function(){
    $('.ProductList').each(function(){
        $(this).find('li').each(function(){
            var current = $(this);
            var mainURL = window.location.href;
            var productUrl = current.find('div div a').attr('href');
            if(mainURL.indexOf('https')!==-1){
                productUrl = current.find('div div a').attr('href').replace('http','https');
            }
            $.ajax({
                url: productUrl,
                type: "POST",
                dataType: "html",
                success: function (data) {
                    var ht = $(data);               
                    var review = ht.find('#productTotalReview').text();
                    current.find('div span').append("<br>&nbsp"+review);//.replace('product reviews','Reviews'));
                }
            });
        });
    });
});
相关问题