Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Pviewed.php
Go to the documentation of this file.
1 <?php
7 
9 
17 class Pviewed extends \Magento\Sales\Block\Adminhtml\Order\Create\Sidebar\AbstractSidebar
18 {
24  protected $_productFactory;
25 
31  protected $_eventFactory;
32 
43  public function __construct(
44  \Magento\Backend\Block\Template\Context $context,
45  \Magento\Backend\Model\Session\Quote $sessionQuote,
46  \Magento\Sales\Model\AdminOrder\Create $orderCreate,
48  \Magento\Sales\Model\Config $salesConfig,
49  \Magento\Reports\Model\EventFactory $eventFactory,
50  \Magento\Catalog\Model\ProductFactory $productFactory,
51  array $data = []
52  ) {
53  $this->_eventFactory = $eventFactory;
54  $this->_productFactory = $productFactory;
55  parent::__construct($context, $sessionQuote, $orderCreate, $priceCurrency, $salesConfig, $data);
56  }
57 
63  protected function _construct()
64  {
65  parent::_construct();
66  $this->setId('sales_order_create_sidebar_pviewed');
67  $this->setDataId('pviewed');
68  }
69 
75  public function getHeaderText()
76  {
77  return __('Recently Viewed Products');
78  }
79 
85  public function getItemCollection()
86  {
87  $productCollection = $this->getData('item_collection');
88  if ($productCollection === null) {
89  $stores = [];
90  $website = $this->_storeManager->getStore($this->getStoreId())->getWebsite();
91  foreach ($website->getStores() as $store) {
92  $stores[] = $store->getId();
93  }
94 
95  $collection = $this->_eventFactory->create()->getCollection()->addStoreFilter(
96  $stores
97  )->addRecentlyFiler(
98  \Magento\Reports\Model\Event::EVENT_PRODUCT_VIEW,
99  $this->getCustomerId(),
100  0
101  );
102  $productIds = [];
103  foreach ($collection as $event) {
104  $productIds[] = $event->getObjectId();
105  }
106 
107  $productCollection = null;
108  if ($productIds) {
109  $productCollection = $this->_productFactory->create()->getCollection()->setStoreId(
110  $this->getQuote()->getStoreId()
111  )->addStoreFilter(
112  $this->getQuote()->getStoreId()
113  )->addAttributeToSelect(
114  'name'
115  )->addAttributeToSelect(
116  'price'
117  )->addAttributeToSelect(
118  'small_image'
119  )->addIdFilter(
121  )->load();
122  }
123  $this->setData('item_collection', $productCollection);
124  }
125  return $productCollection;
126  }
127 
133  public function canRemoveItems()
134  {
135  return false;
136  }
137 
144  public function getIdentifierId($item)
145  {
146  return $item->getId();
147  }
148 }
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
setData($key, $value=null)
Definition: DataObject.php:72
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Backend\Model\Session\Quote $sessionQuote, \Magento\Sales\Model\AdminOrder\Create $orderCreate, PriceCurrencyInterface $priceCurrency, \Magento\Sales\Model\Config $salesConfig, \Magento\Reports\Model\EventFactory $eventFactory, \Magento\Catalog\Model\ProductFactory $productFactory, array $data=[])
Definition: Pviewed.php:43