/** * Get category collection array * * @param null|string|bool|int|Store $storeId * @return array|bool */ public function getCollection($storeId) { $products = []; /* @var $store Store */ $store = $this->_storeManager->getStore($storeId); if (!$store) { return false; } $connection = $this->getConnection(); $this->_select = $connection->select()->from(['e' => $this->getMainTable()], [$this->getIdFieldName(), $this->_productResource->getLinkField(), 'updated_at'])->joinInner(['w' => $this->getTable('catalog_product_website')], 'e.entity_id = w.product_id', [])->joinLeft(['url_rewrite' => $this->getTable('url_rewrite')], 'e.entity_id = url_rewrite.entity_id AND url_rewrite.is_autogenerated = 1' . $connection->quoteInto(' AND url_rewrite.store_id = ?', $store->getId()) . $connection->quoteInto(' AND url_rewrite.entity_type = ?', ProductUrlRewriteGenerator::ENTITY_TYPE), ['url' => 'request_path'])->where('w.website_id = ?', $store->getWebsiteId()); $this->_addFilter($store->getId(), 'visibility', $this->_productVisibility->getVisibleInSiteIds(), 'in'); $this->_addFilter($store->getId(), 'status', $this->_productStatus->getVisibleStatusIds(), 'in'); // Join product images required attributes $imageIncludePolicy = $this->_sitemapData->getProductImageIncludePolicy($store->getId()); if (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_NONE != $imageIncludePolicy) { $this->_joinAttribute($store->getId(), 'name'); $this->_select->columns(['name' => $this->getConnection()->getIfNullSql('t2_name.value', 't1_name.value')]); if (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_ALL == $imageIncludePolicy) { $this->_joinAttribute($store->getId(), 'thumbnail'); $this->_select->columns(['thumbnail' => $this->getConnection()->getIfNullSql('t2_thumbnail.value', 't1_thumbnail.value')]); } elseif (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_BASE == $imageIncludePolicy) { $this->_joinAttribute($store->getId(), 'image'); $this->_select->columns(['image' => $this->getConnection()->getIfNullSql('t2_image.value', 't1_image.value')]); } } $query = $connection->query($this->_select); while ($row = $query->fetch()) { $product = $this->_prepareProduct($row, $store->getId()); $products[$product->getId()] = $product; } return $products; }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false) { $tierPrices = $product->getData('tier_price'); try { $existingProduct = $this->get($product->getSku()); $product->setData($this->resourceModel->getLinkField(), $existingProduct->getData($this->resourceModel->getLinkField())); } catch (NoSuchEntityException $e) { $existingProduct = null; } $productDataArray = $this->extensibleDataObjectConverter->toNestedArray($product, [], 'Magento\\Catalog\\Api\\Data\\ProductInterface'); $productDataArray = array_replace($productDataArray, $product->getData()); unset($productDataArray['media_gallery']); $ignoreLinksFlag = $product->getData('ignore_links_flag'); $productLinks = null; if (!$ignoreLinksFlag && $ignoreLinksFlag !== null) { $productLinks = $product->getProductLinks(); } $productDataArray['store_id'] = (int) $this->storeManager->getStore()->getId(); $product = $this->initializeProductData($productDataArray, empty($existingProduct)); $this->processLinks($product, $productLinks); if (isset($productDataArray['media_gallery_entries'])) { $this->processMediaGallery($product, $productDataArray['media_gallery_entries']); } if (!$product->getOptionsReadonly()) { $product->setCanSaveCustomOptions(true); } $validationResult = $this->resourceModel->validate($product); if (true !== $validationResult) { throw new \Magento\Framework\Exception\CouldNotSaveException(__('Invalid product data: %1', implode(',', $validationResult))); } try { if ($tierPrices !== null) { $product->setData('tier_price', $tierPrices); } unset($this->instances[$product->getSku()]); unset($this->instancesById[$product->getId()]); $this->resourceModel->save($product); } catch (\Magento\Eav\Model\Entity\Attribute\Exception $exception) { throw \Magento\Framework\Exception\InputException::invalidFieldValue($exception->getAttributeCode(), $product->getData($exception->getAttributeCode()), $exception); } catch (ValidatorException $e) { throw new CouldNotSaveException(__($e->getMessage())); } catch (LocalizedException $e) { throw $e; } catch (\Exception $e) { throw new \Magento\Framework\Exception\CouldNotSaveException(__('Unable to save product')); } unset($this->instances[$product->getSku()]); unset($this->instancesById[$product->getId()]); return $this->get($product->getSku()); }
/** * 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 getLinkField() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getLinkField'); if (!$pluginInfo) { return parent::getLinkField(); } else { return $this->___callPlugins('getLinkField', func_get_args(), $pluginInfo); } }