Wednesday 22 November 2017

Magento 2 : Display All Products Review in one Page

Magento 2 :  Display All Products Review in Separate Page

Create a static block 'Review list'

{{block class="Magento\Framework\View\Element\Template" template="Magento_Review::review_list.phtml"}}

The Create a CMS page 'Review'  and insert a widget like this

{{widget type="Magento\Cms\Block\Widget\Block" template="widget/static_block/default.phtml" block_id="36"}}

app/design/frontend/themes/themename/default/Magento_Review/templates/review_list.phtml

paste the below code or enjoy the all product review in one page.

<?php 
use Magento\Framework\App\Action\Action;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()
            ->addAttributeToSelect('*')
            ->addStoreFilter()
            ->load();

foreach ($collection as $product){
 
$imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');
$image_url = $imageHelper->init($product, 'product_page_image_small')->setImageFile($product->getFile())->resize(250, 250)->getUrl();

$rating = $objectManager->get("Magento\Review\Model\ResourceModel\Review\CollectionFactory");
$collectionReview = $rating->create()
        ->addStatusFilter(
            \Magento\Review\Model\Review::STATUS_APPROVED
        )->addEntityFilter(
            'product',
            $product->getId()
        )->setDateOrder();
      
$this->_storeManagerInterface = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
$reviewFactory = $objectManager->create('Magento\Review\Model\Review');
$storeId = $this->_storeManagerInterface->getStore()->getId();
$reviewFactory->getEntitySummary($product, $storeId);
$ratingPer = $product->getRatingSummary()->getRatingSummary();
if($ratingPer){
?>
<div class="review-all-page">
<div class="product-contener">
<div class="col-md-6" >
   <a href="<?php echo $product->getUrlKey().'.html';  ?> " > <img src="<?php echo $image_url;  ?>" /></a>
</div>

<div class="col-md-18">
    <div class="product-name"><a href="<?php echo $product->getUrlKey().'.html';  ?>" ><strong> <?php echo $product->getName(); ?> </strong></a></div>
    <div class="review-contener">
      
       
            <div class="product-reviews-summary short">
                <span>Rating:</span>
                <div class="rating-summary"><span class="label"><span>Rating:</span></span>
                    <div class="rating-result" title=" <?php echo $ratingPer.'%'; ?> ">
                        <span style="width:<?php echo $ratingPer.'%'; ?>"><span><?php echo $ratingPer.'%'; ?></span></span>
                    </div>
                </div>
            </div>
            <div class="all-review">
          <?php  foreach ($collectionReview as $reviewDetail){  ?>
          <div class="review-name-desc">
            <div class="data data_name">
                <b>By <?php  echo  $reviewDetail->getNickname();  ?></b></br>
                <span>Date Added: <?php echo date("l d F, Y", strtotime($reviewDetail->getCreatedAt()));  ?></span>
            </div>
          
            <div class="data data_desc">  <?php echo  $reviewDetail->getDetail(); ?>  </div>
         </div>
       
          
         <?php   }  ?>
            </div>
    </div>
  
</div>
</div>
</div>
 <?php  } }  ?>
  <script type="text/javascript">
            require(['jquery','domReady!'],function($){
                

                     
                        $('.all-review').each(function() {
                            var $times = $(this).children('.review-name-desc');
                            if ($times.length > 1) {  
                                 $(this).children(':nth-of-type(n+2)').addClass('more-times').hide();
                                 $(this).append('<a class="more-timesclick">+ More Review</a>');
                            }
                        });
                      
                        $(document).on('click', 'a.more-timesclick', function() {
                          $(this).parent().find('.review-name-desc.more-times').eq(0).removeClass('more-times').show();
                           $len =  $(this).parent().find('.review-name-desc.more-times').length;
                          if ($len == 0) {
                            $(this).removeClass('more-timesclick').addClass('less-timesclick').text('- Less Review');
                          }
                        }); 
                      
                         $(document).on('click', 'a.less-timesclick', function() {
                           
                            var $times = $(this).parent().children('.review-name-desc');
                            if ($times.length > 1) {  
                              
                                 $(this).parent().children(':nth-of-type(n+2)').addClass('more-times').hide();
                                 $(this).removeClass('less-timesclick').addClass('more-timesclick').text('+ More Review');
                              
                            }
                          
                        });
                
                
            });
 </script>

if any issues please email me.

Thanks,
Kuldeep Singh

Email: kuldeep4110@gmail.com

No comments:

Post a Comment