Pviewed.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Block\Adminhtml\Order\Create\Sidebar;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. /**
  9. * Adminhtml sales order create sidebar recently view block
  10. *
  11. * @api
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. * @since 100.0.2
  14. */
  15. class Pviewed extends \Magento\Sales\Block\Adminhtml\Order\Create\Sidebar\AbstractSidebar
  16. {
  17. /**
  18. * Product factory
  19. *
  20. * @var \Magento\Catalog\Model\ProductFactory
  21. */
  22. protected $_productFactory;
  23. /**
  24. * Event factory
  25. *
  26. * @var \Magento\Reports\Model\EventFactory
  27. */
  28. protected $_eventFactory;
  29. /**
  30. * @param \Magento\Backend\Block\Template\Context $context
  31. * @param \Magento\Backend\Model\Session\Quote $sessionQuote
  32. * @param \Magento\Sales\Model\AdminOrder\Create $orderCreate
  33. * @param PriceCurrencyInterface $priceCurrency
  34. * @param \Magento\Sales\Model\Config $salesConfig
  35. * @param \Magento\Reports\Model\EventFactory $eventFactory
  36. * @param \Magento\Catalog\Model\ProductFactory $productFactory
  37. * @param array $data
  38. */
  39. public function __construct(
  40. \Magento\Backend\Block\Template\Context $context,
  41. \Magento\Backend\Model\Session\Quote $sessionQuote,
  42. \Magento\Sales\Model\AdminOrder\Create $orderCreate,
  43. PriceCurrencyInterface $priceCurrency,
  44. \Magento\Sales\Model\Config $salesConfig,
  45. \Magento\Reports\Model\EventFactory $eventFactory,
  46. \Magento\Catalog\Model\ProductFactory $productFactory,
  47. array $data = []
  48. ) {
  49. $this->_eventFactory = $eventFactory;
  50. $this->_productFactory = $productFactory;
  51. parent::__construct($context, $sessionQuote, $orderCreate, $priceCurrency, $salesConfig, $data);
  52. }
  53. /**
  54. * Constructor
  55. *
  56. * @return void
  57. */
  58. protected function _construct()
  59. {
  60. parent::_construct();
  61. $this->setId('sales_order_create_sidebar_pviewed');
  62. $this->setDataId('pviewed');
  63. }
  64. /**
  65. * Get header text
  66. *
  67. * @return \Magento\Framework\Phrase
  68. */
  69. public function getHeaderText()
  70. {
  71. return __('Recently Viewed Products');
  72. }
  73. /**
  74. * Retrieve item collection
  75. *
  76. * @return mixed
  77. */
  78. public function getItemCollection()
  79. {
  80. $productCollection = $this->getData('item_collection');
  81. if ($productCollection === null) {
  82. $stores = [];
  83. $website = $this->_storeManager->getStore($this->getStoreId())->getWebsite();
  84. foreach ($website->getStores() as $store) {
  85. $stores[] = $store->getId();
  86. }
  87. $collection = $this->_eventFactory->create()->getCollection()->addStoreFilter(
  88. $stores
  89. )->addRecentlyFiler(
  90. \Magento\Reports\Model\Event::EVENT_PRODUCT_VIEW,
  91. $this->getCustomerId(),
  92. 0
  93. );
  94. $productIds = [];
  95. foreach ($collection as $event) {
  96. $productIds[] = $event->getObjectId();
  97. }
  98. $productCollection = null;
  99. if ($productIds) {
  100. $productCollection = $this->_productFactory->create()->getCollection()->setStoreId(
  101. $this->getQuote()->getStoreId()
  102. )->addStoreFilter(
  103. $this->getQuote()->getStoreId()
  104. )->addAttributeToSelect(
  105. 'name'
  106. )->addAttributeToSelect(
  107. 'price'
  108. )->addAttributeToSelect(
  109. 'small_image'
  110. )->addIdFilter(
  111. $productIds
  112. )->load();
  113. }
  114. $this->setData('item_collection', $productCollection);
  115. }
  116. return $productCollection;
  117. }
  118. /**
  119. * Retrieve availability removing items in block
  120. *
  121. * @return false
  122. */
  123. public function canRemoveItems()
  124. {
  125. return false;
  126. }
  127. /**
  128. * Retrieve identifier of block item
  129. *
  130. * @param \Magento\Framework\DataObject $item
  131. * @return int
  132. */
  133. public function getIdentifierId($item)
  134. {
  135. return $item->getId();
  136. }
  137. }