AbstractProduct.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Reports\Block\Product;
  7. /**
  8. * Reports Recently Products Abstract Block
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. abstract class AbstractProduct extends \Magento\Catalog\Block\Product\AbstractProduct
  13. {
  14. /**
  15. * Product Index model type
  16. *
  17. * @var string
  18. */
  19. protected $_indexType;
  20. /**
  21. * Product Index Collection
  22. *
  23. * @var \Magento\Reports\Model\ResourceModel\Product\Index\Collection\AbstractCollection
  24. */
  25. protected $_collection;
  26. /**
  27. * @var \Magento\Catalog\Model\Product\Visibility
  28. */
  29. protected $_productVisibility;
  30. /**
  31. * @var \Magento\Reports\Model\Product\Index\Factory
  32. */
  33. protected $_indexFactory;
  34. /**
  35. * @param \Magento\Catalog\Block\Product\Context $context
  36. * @param \Magento\Catalog\Model\Product\Visibility $productVisibility
  37. * @param \Magento\Reports\Model\Product\Index\Factory $indexFactory
  38. * @param array $data
  39. */
  40. public function __construct(
  41. \Magento\Catalog\Block\Product\Context $context,
  42. \Magento\Catalog\Model\Product\Visibility $productVisibility,
  43. \Magento\Reports\Model\Product\Index\Factory $indexFactory,
  44. array $data = []
  45. ) {
  46. parent::__construct(
  47. $context,
  48. $data
  49. );
  50. $this->_productVisibility = $productVisibility;
  51. $this->_indexFactory = $indexFactory;
  52. $this->_isScopePrivate = true;
  53. }
  54. /**
  55. * Retrieve page size
  56. *
  57. * @return int
  58. */
  59. public function getPageSize()
  60. {
  61. if ($this->hasData('page_size')) {
  62. return $this->getData('page_size');
  63. }
  64. return 5;
  65. }
  66. /**
  67. * Retrieve product ids, that must not be included in collection
  68. *
  69. * @return array
  70. */
  71. protected function _getProductsToSkip()
  72. {
  73. return [];
  74. }
  75. /**
  76. * Public method for retrieve Product Index model
  77. * @throws \Magento\Framework\Exception\LocalizedException
  78. * @return \Magento\Reports\Model\Product\Index\AbstractIndex
  79. */
  80. public function getModel()
  81. {
  82. try {
  83. $model = $this->_indexFactory->get($this->_indexType);
  84. } catch (\InvalidArgumentException $e) {
  85. throw new \Magento\Framework\Exception\LocalizedException(__('Index type is not valid'));
  86. }
  87. return $model;
  88. }
  89. /**
  90. * Retrieve Index Product Collection
  91. *
  92. * @return \Magento\Reports\Model\ResourceModel\Product\Index\Collection\AbstractCollection
  93. */
  94. public function getItemsCollection()
  95. {
  96. if ($this->_collection === null) {
  97. $attributes = $this->_catalogConfig->getProductAttributes();
  98. $this->_collection = $this->getModel()->getCollection()->addAttributeToSelect($attributes);
  99. if ($this->getCustomerId()) {
  100. $this->_collection->setCustomerId($this->getCustomerId());
  101. }
  102. $this->_collection->excludeProductIds(
  103. $this->getModel()->getExcludeProductIds()
  104. )->addUrlRewrite()->setPageSize(
  105. $this->getPageSize()
  106. )->setCurPage(
  107. 1
  108. );
  109. /* Price data is added to consider item stock status using price index */
  110. $this->_collection->addPriceData();
  111. $ids = $this->getProductIds();
  112. if (empty($ids)) {
  113. $this->_collection->addIndexFilter();
  114. } else {
  115. $this->_collection->addFilterByIds($ids);
  116. }
  117. $this->_collection->setAddedAtOrder()->setVisibility($this->_productVisibility->getVisibleInSiteIds());
  118. }
  119. return $this->_collection;
  120. }
  121. /**
  122. * Retrieve count of product index items
  123. *
  124. * @return int
  125. */
  126. public function getCount()
  127. {
  128. if (!$this->getModel()->getCount()) {
  129. return 0;
  130. }
  131. return $this->getItemsCollection()->count();
  132. }
  133. }