Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NewWidget.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  const DISPLAY_TYPE_ALL_PRODUCTS = 'all_products';
17 
21  const DISPLAY_TYPE_NEW_PRODUCTS = 'new_products';
22 
26  const DEFAULT_SHOW_PAGER = false;
27 
32 
38  const PAGE_VAR_NAME = 'np';
39 
45  protected $_pager;
46 
50  private $serializer;
51 
62  public function __construct(
63  \Magento\Catalog\Block\Product\Context $context,
64  \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
65  \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
66  \Magento\Framework\App\Http\Context $httpContext,
67  array $data = [],
68  \Magento\Framework\Serialize\Serializer\Json $serializer = null
69  ) {
70  parent::__construct(
71  $context,
72  $productCollectionFactory,
73  $catalogProductVisibility,
75  $data
76  );
77  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
78  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
79  }
80 
86  protected function _getProductCollection()
87  {
88  switch ($this->getDisplayType()) {
90  $collection = parent::_getProductCollection()
91  ->setPageSize($this->getPageSize())
92  ->setCurPage($this->getCurrentPage());
93  break;
94  default:
95  $collection = $this->_getRecentlyAddedProductsCollection();
96  break;
97  }
98  return $collection;
99  }
100 
106  protected function _getRecentlyAddedProductsCollection()
107  {
109  $collection = $this->_productCollectionFactory->create();
110  $collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
111 
113  ->addStoreFilter()
114  ->addAttributeToSort('created_at', 'desc')
115  ->setPageSize($this->getPageSize())
116  ->setCurPage($this->getCurrentPage());
117  return $collection;
118  }
119 
125  public function getCurrentPage()
126  {
127  return abs((int)$this->getRequest()->getParam($this->getData('page_var_name')));
128  }
129 
135  public function getCacheKeyInfo()
136  {
137  return array_merge(
138  parent::getCacheKeyInfo(),
139  [
140  $this->getDisplayType(),
141  $this->getProductsPerPage(),
142  intval($this->getRequest()->getParam($this->getData('page_var_name'), 1)),
143  $this->serializer->serialize($this->getRequest()->getParams())
144  ]
145  );
146  }
147 
153  public function getDisplayType()
154  {
155  if (!$this->hasData('display_type')) {
156  $this->setData('display_type', self::DISPLAY_TYPE_ALL_PRODUCTS);
157  }
158  return $this->getData('display_type');
159  }
160 
166  public function getProductsCount()
167  {
168  if (!$this->hasData('products_count')) {
169  return parent::getProductsCount();
170  }
171  return $this->getData('products_count');
172  }
173 
179  public function getProductsPerPage()
180  {
181  if (!$this->hasData('products_per_page')) {
182  $this->setData('products_per_page', self::DEFAULT_PRODUCTS_PER_PAGE);
183  }
184  return $this->getData('products_per_page');
185  }
186 
192  public function showPager()
193  {
194  if (!$this->hasData('show_pager')) {
195  $this->setData('show_pager', self::DEFAULT_SHOW_PAGER);
196  }
197  return (bool)$this->getData('show_pager');
198  }
199 
205  protected function getPageSize()
206  {
207  return $this->showPager() ? $this->getProductsPerPage() : $this->getProductsCount();
208  }
209 
215  public function getPagerHtml()
216  {
217  if ($this->showPager()) {
218  if (!$this->_pager) {
219  $this->_pager = $this->getLayout()->createBlock(
220  \Magento\Catalog\Block\Product\Widget\Html\Pager::class,
221  'widget.new.product.list.pager'
222  );
223 
224  $this->_pager->setUseContainer(true)
225  ->setShowAmounts(true)
226  ->setShowPerPage(false)
227  ->setPageVarName($this->getData('page_var_name'))
228  ->setLimit($this->getProductsPerPage())
229  ->setTotalLimit($this->getProductsCount())
230  ->setCollection($this->getProductCollection());
231  }
232  if ($this->_pager instanceof \Magento\Framework\View\Element\AbstractBlock) {
233  return $this->_pager->toHtml();
234  }
235  }
236  return '';
237  }
238 
249  public function getProductPriceHtml(
250  \Magento\Catalog\Model\Product $product,
251  $priceType = null,
252  $renderZone = \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
253  array $arguments = []
254  ) {
255  if (!isset($arguments['zone'])) {
256  $arguments['zone'] = $renderZone;
257  }
258  $arguments['zone'] = isset($arguments['zone'])
259  ? $arguments['zone']
260  : $renderZone;
261  $arguments['price_id'] = isset($arguments['price_id'])
262  ? $arguments['price_id']
263  : 'old-price-' . $product->getId() . '-' . $priceType;
264  $arguments['include_container'] = isset($arguments['include_container'])
265  ? $arguments['include_container']
266  : true;
267  $arguments['display_minimal_price'] = isset($arguments['display_minimal_price'])
268  ? $arguments['display_minimal_price']
269  : true;
270 
272  $priceRender = $this->getLayout()->getBlock('product.price.render.default');
273 
274  $price = '';
275  if ($priceRender) {
276  $price = $priceRender->render(
277  \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
278  $product,
279  $arguments
280  );
281  }
282  return $price;
283  }
284 }
getData($key='', $index=null)
Definition: DataObject.php:119
$price
$priceType
Definition: msrp.phtml:18
$arguments
__construct(\Magento\Catalog\Block\Product\Context $context, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility, \Magento\Framework\App\Http\Context $httpContext, array $data=[], \Magento\Framework\Serialize\Serializer\Json $serializer=null)
Definition: NewWidget.php:62
_addProductAttributesAndPrices(\Magento\Catalog\Model\ResourceModel\Product\Collection $collection)