Пример #1
1
 /**
  * Get rating summary
  *
  * @return string
  */
 public function getRatingSummary()
 {
     if (!$this->getRatingSummaryCache()) {
         $this->setRatingSummaryCache($this->_ratingFactory->create()->getReviewSummary($this->getReviewId()));
     }
     return $this->getRatingSummaryCache();
 }
Пример #2
0
 /**
  * @return void
  */
 protected function _construct()
 {
     parent::_construct();
     $this->_objectId = 'id';
     $this->_controller = 'adminhtml_rating';
     $this->_blockGroup = 'Magento_Review';
     $this->buttonList->update('save', 'label', __('Save Rating'));
     $this->buttonList->update('delete', 'label', __('Delete Rating'));
     if ($this->getRequest()->getParam($this->_objectId)) {
         $ratingData = $this->_ratingFactory->create()->load($this->getRequest()->getParam($this->_objectId));
         $this->_coreRegistry->register('rating_data', $ratingData);
     }
 }
Пример #3
0
 /**
  * Aggregate
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return void
  */
 public function aggregate($object)
 {
     $readAdapter = $this->_getReadAdapter();
     $writeAdapter = $this->_getWriteAdapter();
     if (!$object->getEntityPkValue() && $object->getId()) {
         $object->load($object->getReviewId());
     }
     $ratingModel = $this->_ratingFactory->create();
     $ratingSummaries = $ratingModel->getEntitySummary($object->getEntityPkValue(), false);
     foreach ($ratingSummaries as $ratingSummaryObject) {
         if ($ratingSummaryObject->getCount()) {
             $ratingSummary = round($ratingSummaryObject->getSum() / $ratingSummaryObject->getCount());
         } else {
             $ratingSummary = $ratingSummaryObject->getSum();
         }
         $reviewsCount = $this->getTotalReviews($object->getEntityPkValue(), true, $ratingSummaryObject->getStoreId());
         $select = $readAdapter->select()->from($this->_aggregateTable)->where('entity_pk_value = :pk_value')->where('entity_type = :entity_type')->where('store_id = :store_id');
         $bind = [':pk_value' => $object->getEntityPkValue(), ':entity_type' => $object->getEntityId(), ':store_id' => $ratingSummaryObject->getStoreId()];
         $oldData = $readAdapter->fetchRow($select, $bind);
         $data = new \Magento\Framework\Object();
         $data->setReviewsCount($reviewsCount)->setEntityPkValue($object->getEntityPkValue())->setEntityType($object->getEntityId())->setRatingSummary($ratingSummary > 0 ? $ratingSummary : 0)->setStoreId($ratingSummaryObject->getStoreId());
         $writeAdapter->beginTransaction();
         try {
             if ($oldData['primary_id'] > 0) {
                 $condition = ["{$this->_aggregateTable}.primary_id = ?" => $oldData['primary_id']];
                 $writeAdapter->update($this->_aggregateTable, $data->getData(), $condition);
             } else {
                 $writeAdapter->insert($this->_aggregateTable, $data->getData());
             }
             $writeAdapter->commit();
         } catch (\Exception $e) {
             $writeAdapter->rollBack();
         }
     }
 }
Пример #4
0
 /**
  * Get rating summary
  *
  * @return array
  */
 public function getRatingSummary()
 {
     if (!$this->getRatingSummaryCache()) {
         $this->setRatingSummaryCache($this->_ratingFactory->create()->getEntitySummary($this->getProductData()->getId()));
     }
     return $this->getRatingSummaryCache();
 }
Пример #5
0
 /**
  * @return int
  */
 protected function getRatingEntityId()
 {
     if (!$this->ratingProductEntityId) {
         $rating = $this->ratingFactory->create();
         $this->ratingProductEntityId = $rating->getEntityIdByCode(\Magento\Review\Model\Rating::ENTITY_PRODUCT_CODE);
     }
     return $this->ratingProductEntityId;
 }
Пример #6
0
 /**
  * Add review summary
  *
  * @return $this
  */
 public function addReviewSummary()
 {
     foreach ($this->getItems() as $item) {
         $model = $this->_ratingFactory->create();
         $model->getReviewSummary($item->getReviewId());
         $item->addData($model->getData());
     }
     return $this;
 }
Пример #7
0
 /**
  * @return string
  */
 protected function _toHtml()
 {
     $entityId = $this->_request->getParam('id');
     if (intval($entityId) <= 0) {
         return '';
     }
     $reviewsCount = $this->_ratingFactory->create()->getTotalReviews($entityId, true);
     if ($reviewsCount == 0) {
         #return __('Be the first to review this product');
         $this->setTemplate('empty.phtml');
         return parent::_toHtml();
     }
     $ratingCollection = $this->_ratingFactory->create()->getResourceCollection()->addEntityFilter('product')->setPositionOrder()->setStoreFilter($this->_storeManager->getStore()->getId())->addRatingPerStoreName($this->_storeManager->getStore()->getId())->load();
     if ($entityId) {
         $ratingCollection->addEntitySummaryToItem($entityId, $this->_storeManager->getStore()->getId());
     }
     $this->assign('collection', $ratingCollection);
     return parent::_toHtml();
 }
Пример #8
0
 /**
  * Aggregate
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return void
  */
 public function aggregate($object)
 {
     if (!$object->getEntityPkValue() && $object->getId()) {
         $object->load($object->getReviewId());
     }
     $ratingModel = $this->_ratingFactory->create();
     $ratingSummaries = $ratingModel->getEntitySummary($object->getEntityPkValue(), false);
     foreach ($ratingSummaries as $ratingSummaryObject) {
         $this->aggregateReviewSummary($object, $ratingSummaryObject);
     }
 }
Пример #9
0
    /**
     * Check postAction method and assert that review model storeId equals null.
     */
    public function testPostAction()
    {
        $this->requestMock->expects($this->any())
            ->method('getParam')
            ->willReturnMap(
                [
                    ['product_id', false, 1],
                    ['ratings', [], ['1' => '1']]
                ]
            );
        $this->requestMock->expects($this->once())
            ->method('getPostValue')
            ->willReturn(['status_id' => 1]);
        $this->objectManagerMock->expects($this->any())
            ->method('get')
            ->with('Magento\Store\Model\StoreManagerInterface')
            ->willReturn($this->storeManagerMock);
        $this->reviewFactoryMock->expects($this->once())
            ->method('create')
            ->willReturn($this->reviewMock);
        $this->ratingFactoryMock->expects($this->once())
            ->method('create')
            ->willReturn($this->ratingMock);
        $this->storeManagerMock->expects($this->once())
            ->method('hasSingleStore')
            ->willReturn(true);
        $this->storeManagerMock->expects($this->once())
            ->method('getStore')
            ->willReturn($this->storeModelMock);
        $this->storeModelMock->expects($this->once())
            ->method('getId')
            ->willReturn(1);
        $this->reviewMock->expects($this->once())
            ->method('save')
            ->willReturn($this->reviewMock);
        $this->reviewMock->expects($this->once())
            ->method('getId')
            ->willReturn(1);
        $this->reviewMock->expects($this->once())
            ->method('aggregate')
            ->willReturn($this->reviewMock);
        $this->ratingMock->expects($this->once())
            ->method('setRatingId')
            ->willReturnSelf();
        $this->ratingMock->expects($this->once())
            ->method('setReviewId')
            ->willReturnSelf();
        $this->ratingMock->expects($this->once())
            ->method('addOptionVote')
            ->willReturnSelf();

        $this->assertSame($this->resultRedirectMock, $this->postController->executeInternal());
    }
Пример #10
0
 /**
  * Get collection of ratings
  *
  * @return RatingCollection
  */
 public function getRatings()
 {
     return $this->_ratingFactory->create()->getResourceCollection()->addEntityFilter('product')->setPositionOrder()->addRatingPerStoreName($this->_storeManager->getStore()->getId())->setStoreFilter($this->_storeManager->getStore()->getId())->setActiveFilter(true)->load()->addOptionToItems();
 }