/** * Retrieve product attribute * * @param string $attributeCode * @param int|array $productIds * @param string $storeId * @return array */ public function _getProductAttribute($attributeCode, $productIds, $storeId) { $connection = $this->getConnection(); if (!isset($this->_productAttributes[$attributeCode])) { $attribute = $this->productResource->getAttribute($attributeCode); $this->_productAttributes[$attributeCode] = ['entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getId(), 'table' => $attribute->getBackend()->getTable(), 'is_global' => $attribute->getIsGlobal()]; unset($attribute); } if (!is_array($productIds)) { $productIds = [$productIds]; } $bind = ['attribute_id' => $this->_productAttributes[$attributeCode]['attribute_id']]; $select = $connection->select(); $attributeTable = $this->_productAttributes[$attributeCode]['table']; if ($this->_productAttributes[$attributeCode]['is_global'] || $storeId == 0) { $select->from($attributeTable, ['entity_id', 'value'])->where('attribute_id = :attribute_id')->where('store_id = ?', 0)->where('entity_id IN(?)', $productIds); } else { $valueExpr = $connection->getCheckSql('t2.value_id > 0', 't2.value', 't1.value'); $select->from(['t1' => $attributeTable], ['entity_id', 'value' => $valueExpr])->joinLeft(['t2' => $attributeTable], 't1.entity_id = t2.entity_id AND t1.attribute_id = t2.attribute_id AND t2.store_id=:store_id', [])->where('t1.store_id = ?', 0)->where('t1.attribute_id = :attribute_id')->where('t1.entity_id IN(?)', $productIds); $bind['store_id'] = $storeId; } $rowSet = $connection->fetchAll($select, $bind); $attributes = []; foreach ($rowSet as $row) { $attributes[$row['entity_id']] = $row['value']; } unset($rowSet); foreach ($productIds as $productId) { if (!isset($attributes[$productId])) { $attributes[$productId] = null; } } return $attributes; }
/** * Get attribute data by attribute code * * @param string $attributeCode * @return array */ protected function _getAttribute($attributeCode) { if (!isset($this->_attributesCache[$attributeCode])) { $attribute = $this->_productResource->getAttribute($attributeCode); $this->_attributesCache[$attributeCode] = ['entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getId(), 'table' => $attribute->getBackend()->getTable(), 'is_global' => $attribute->getIsGlobal() == \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 'backend_type' => $attribute->getBackendType()]; } return $this->_attributesCache[$attributeCode]; }
/** * Aggregate products view data * * @param null|mixed $from * @param null|mixed $to * @return $this * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function aggregate($from = null, $to = null) { $mainTable = $this->getMainTable(); $connection = $this->getConnection(); if ($from !== null || $to !== null) { $subSelect = $this->_getTableDateRangeSelect($this->getTable('report_event'), 'logged_at', 'logged_at', $from, $to); } else { $subSelect = null; } $this->_clearTableByDateRange($mainTable, $from, $to, $subSelect); // convert dates to current admin timezone $periodExpr = $connection->getDatePartSql($this->getStoreTZOffsetQuery(['source_table' => $this->getTable('report_event')], 'source_table.logged_at', $from, $to)); $select = $connection->select(); $select->group([$periodExpr, 'source_table.store_id', 'source_table.object_id']); $viewsNumExpr = new \Zend_Db_Expr('COUNT(source_table.event_id)'); $columns = ['period' => $periodExpr, 'store_id' => 'source_table.store_id', 'product_id' => 'source_table.object_id', 'product_name' => new \Zend_Db_Expr(sprintf('MIN(%s)', $connection->getIfNullSql('product_name.value', 'product_default_name.value'))), 'product_price' => new \Zend_Db_Expr(sprintf('MIN(%s)', $connection->getIfNullSql($connection->getIfNullSql('product_price.value', 'product_default_price.value'), 0))), 'views_num' => $viewsNumExpr]; $select->from(['source_table' => $this->getTable('report_event')], $columns)->where('source_table.event_type_id = ?', \Magento\Reports\Model\Event::EVENT_PRODUCT_VIEW); $productLinkField = $this->_productResource->getLinkField(); $select->joinInner(['product' => $this->getTable('catalog_product_entity')], "product.{$productLinkField} = source_table.object_id", []); // join product attributes Name & Price $nameAttribute = $this->_productResource->getAttribute('name'); $joinExprProductName = ["product_name.{$productLinkField} = product.{$productLinkField}", 'product_name.store_id = source_table.store_id', $connection->quoteInto('product_name.attribute_id = ?', $nameAttribute->getAttributeId())]; $joinExprProductName = implode(' AND ', $joinExprProductName); $joinProductName = ["product_default_name.{$productLinkField} = product.{$productLinkField}", 'product_default_name.store_id = 0', $connection->quoteInto('product_default_name.attribute_id = ?', $nameAttribute->getAttributeId())]; $joinProductName = implode(' AND ', $joinProductName); $select->joinLeft(['product_name' => $nameAttribute->getBackend()->getTable()], $joinExprProductName, [])->joinLeft(['product_default_name' => $nameAttribute->getBackend()->getTable()], $joinProductName, []); $priceAttribute = $this->_productResource->getAttribute('price'); $joinExprProductPrice = ["product_price.{$productLinkField} = product.{$productLinkField}", 'product_price.store_id = source_table.store_id', $connection->quoteInto('product_price.attribute_id = ?', $priceAttribute->getAttributeId())]; $joinExprProductPrice = implode(' AND ', $joinExprProductPrice); $joinProductPrice = ["product_default_price.{$productLinkField} = product.{$productLinkField}", 'product_default_price.store_id = 0', $connection->quoteInto('product_default_price.attribute_id = ?', $priceAttribute->getAttributeId())]; $joinProductPrice = implode(' AND ', $joinProductPrice); $select->joinLeft(['product_price' => $priceAttribute->getBackend()->getTable()], $joinExprProductPrice, [])->joinLeft(['product_default_price' => $priceAttribute->getBackend()->getTable()], $joinProductPrice, []); $havingPart = [$connection->prepareSqlCondition($viewsNumExpr, ['gt' => 0])]; if (null !== $subSelect) { $subSelectHavingPart = $this->_makeConditionFromDateRangeSelect($subSelect, 'period'); if ($subSelectHavingPart) { $havingPart[] = '(' . $subSelectHavingPart . ')'; } } $select->having(implode(' AND ', $havingPart)); $select->useStraightJoin(); $insertQuery = $select->insertFromSelect($this->getMainTable(), array_keys($columns)); $connection->query($insertQuery); $this->_resourceHelper->updateReportRatingPos($connection, 'day', 'views_num', $mainTable, $this->getTable(self::AGGREGATION_DAILY)); $this->_resourceHelper->updateReportRatingPos($connection, 'month', 'views_num', $mainTable, $this->getTable(self::AGGREGATION_MONTHLY)); $this->_resourceHelper->updateReportRatingPos($connection, 'year', 'views_num', $mainTable, $this->getTable(self::AGGREGATION_YEARLY)); $this->_setFlagData(\Magento\Reports\Model\Flag::REPORT_PRODUCT_VIEWED_FLAG_CODE); return $this; }
/** * {@inheritdoc} */ public function getAttribute($attribute) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getAttribute'); if (!$pluginInfo) { return parent::getAttribute($attribute); } else { return $this->___callPlugins('getAttribute', func_get_args(), $pluginInfo); } }