/** * 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(); } } }
/** * Aggregate review summary * * @param \Magento\Framework\Model\AbstractModel $object * @param \Magento\Review\Model\Rating $ratingSummaryObject * @return void */ protected function aggregateReviewSummary($object, $ratingSummaryObject) { $readAdapter = $this->_getReadAdapter(); 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()); $this->writeReviewSummary($oldData, $data); }