Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractProduct.php
Go to the documentation of this file.
1 <?php
7 
14 {
20  protected $_indexType;
21 
27  protected $_collection;
28 
33 
37  protected $_indexFactory;
38 
45  public function __construct(
46  \Magento\Catalog\Block\Product\Context $context,
47  \Magento\Catalog\Model\Product\Visibility $productVisibility,
48  \Magento\Reports\Model\Product\Index\Factory $indexFactory,
49  array $data = []
50  ) {
51  parent::__construct(
52  $context,
53  $data
54  );
55  $this->_productVisibility = $productVisibility;
56  $this->_indexFactory = $indexFactory;
57  $this->_isScopePrivate = true;
58  }
59 
65  public function getPageSize()
66  {
67  if ($this->hasData('page_size')) {
68  return $this->getData('page_size');
69  }
70  return 5;
71  }
72 
78  protected function _getProductsToSkip()
79  {
80  return [];
81  }
82 
88  public function getModel()
89  {
90  try {
91  $model = $this->_indexFactory->get($this->_indexType);
92  } catch (\InvalidArgumentException $e) {
93  throw new \Magento\Framework\Exception\LocalizedException(__('Index type is not valid'));
94  }
95 
96  return $model;
97  }
98 
104  public function getItemsCollection()
105  {
106  if ($this->_collection === null) {
107  $attributes = $this->_catalogConfig->getProductAttributes();
108 
109  $this->_collection = $this->getModel()->getCollection()->addAttributeToSelect($attributes);
110 
111  if ($this->getCustomerId()) {
112  $this->_collection->setCustomerId($this->getCustomerId());
113  }
114 
115  $this->_collection->excludeProductIds(
116  $this->getModel()->getExcludeProductIds()
117  )->addUrlRewrite()->setPageSize(
118  $this->getPageSize()
119  )->setCurPage(
120  1
121  );
122 
123  /* Price data is added to consider item stock status using price index */
124  $this->_collection->addPriceData();
125 
126  $ids = $this->getProductIds();
127  if (empty($ids)) {
128  $this->_collection->addIndexFilter();
129  } else {
130  $this->_collection->addFilterByIds($ids);
131  }
132  $this->_collection->setAddedAtOrder()->setVisibility($this->_productVisibility->getVisibleInSiteIds());
133  }
134 
135  return $this->_collection;
136  }
137 
143  public function getCount()
144  {
145  if (!$this->getModel()->getCount()) {
146  return 0;
147  }
148  return $this->getItemsCollection()->count();
149  }
150 }
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
$this _collection
Definition: coupons.php:7
$attributes
Definition: matrix.phtml:13
__construct(\Magento\Catalog\Block\Product\Context $context, \Magento\Catalog\Model\Product\Visibility $productVisibility, \Magento\Reports\Model\Product\Index\Factory $indexFactory, array $data=[])