2016-12-18 39 views
0

隨函附上圖像這是給你的想法 我要計算每個產品的意見 enter image description here如何計算每件商品的評論以及在大商業中的產品列表中展示?

請找武官碼

<li class="%%GLOBAL_AlternateClass%%"> 
<div class="inner"> 
<div class="ProductImage QuickView" data-product="%%GLOBAL_ProductId%%"> 
%%GLOBAL_ProductThumb%% 
</div> 
<div class="ProductDetails"> 
<a href="%%GLOBAL_ProductLink%%" class="%%GLOBAL_SearchTrackClass%% pname">%%GLOBAL_ProductName%%</a> 
</div> 
<em class="p-price">%%GLOBAL_ProductPrice%%</em> 
<div class="ProductPriceRating"> 
<span class="Rating Rating%%GLOBAL_ProductRating%%"> 
<img src="%%GLOBAL_IMG_PATH%%/IcoRating%%GLOBAL_ProductRating%%.png" alt="" style="%%GLOBAL_HideProductRating%%"/> 
</span> 
</div> 
<div> 
<div class="ProductCompareButton" style="display:%%GLOBAL_HideCompareItems%%"> 
<input type="checkbox" class="CheckBox" name="compare_product_ids" id="compare_%%GLOBAL_ProductId%%" value="%%GLOBAL_ProductId%%" onclick="product_comparison_box_changed(this.checked)"/> <label for="compare_%%GLOBAL_ProductId%%">%%LNG_Compare%%</label> <br> 
</div> 
<div class="ProductActionAdd" style="display:%%GLOBAL_HideActionAdd%%;"> 
<a href="%%GLOBAL_ProductURL%%" class="btn icon-%%GLOBAL_ProductAddText%%" title="%%GLOBAL_ProductAddText%%">%%GLOBAL_ProductAddText%%</a> 
</div> 
</div> 
</li> 
+0

請出示您已經嘗試實現這一代碼。 – Alyss

+0

@Alyss:請檢查代碼是否已更新 – Dgo

+0

@Alyss:我正在使用%% GLOBAL_ProductNumReviews %%,但它不起作用 – Dgo

回答

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); 
      } 
     }); 
    }); 
    } 
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')); 
       } 
      }); 
     }); 
    }); 
}); 
相關問題