/** * Get rating summary * * @return string */ public function getRatingSummary() { if (!$this->getRatingSummaryCache()) { $this->setRatingSummaryCache($this->_ratingFactory->create()->getReviewSummary($this->getReviewId())); } return $this->getRatingSummaryCache(); }
/** * @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); } }
/** * 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(); } } }
/** * Get rating summary * * @return array */ public function getRatingSummary() { if (!$this->getRatingSummaryCache()) { $this->setRatingSummaryCache($this->_ratingFactory->create()->getEntitySummary($this->getProductData()->getId())); } return $this->getRatingSummaryCache(); }
/** * @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; }
/** * 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; }
/** * @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(); }
/** * 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); } }
/** * 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(); }