Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ListProducts.php
Go to the documentation of this file.
1 <?php
8 
10 
18 {
22  protected $currentCustomer;
23 
27  protected $_linksFactory;
28 
32  protected $_itemsFactory;
33 
41  public function __construct(
42  \Magento\Framework\View\Element\Template\Context $context,
43  \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
44  \Magento\Downloadable\Model\ResourceModel\Link\Purchased\CollectionFactory $linksFactory,
45  \Magento\Downloadable\Model\ResourceModel\Link\Purchased\Item\CollectionFactory $itemsFactory,
46  array $data = []
47  ) {
48  $this->currentCustomer = $currentCustomer;
49  $this->_linksFactory = $linksFactory;
50  $this->_itemsFactory = $itemsFactory;
51  parent::__construct($context, $data);
52  }
53 
59  protected function _construct()
60  {
61  parent::_construct();
62  $purchased = $this->_linksFactory->create()
63  ->addFieldToFilter('customer_id', $this->currentCustomer->getCustomerId())
64  ->addOrder('created_at', 'desc');
65  $this->setPurchased($purchased);
66  $purchasedIds = [];
67  foreach ($purchased as $_item) {
68  $purchasedIds[] = $_item->getId();
69  }
70  if (empty($purchasedIds)) {
71  $purchasedIds = [null];
72  }
73  $purchasedItems = $this->_itemsFactory->create()->addFieldToFilter(
74  'purchased_id',
75  ['in' => $purchasedIds]
76  )->addFieldToFilter(
77  'status',
79  )->setOrder(
80  'item_id',
81  'desc'
82  );
83  $this->setItems($purchasedItems);
84  }
85 
91  protected function _prepareLayout()
92  {
93  parent::_prepareLayout();
94 
95  $pager = $this->getLayout()->createBlock(
96  \Magento\Theme\Block\Html\Pager::class,
97  'downloadable.customer.products.pager'
98  )->setCollection(
99  $this->getItems()
100  )->setPath('downloadable/customer/products');
101  $this->setChild('pager', $pager);
102  $this->getItems()->load();
103  foreach ($this->getItems() as $item) {
104  $item->setPurchased($this->getPurchased()->getItemById($item->getPurchasedId()));
105  }
106  return $this;
107  }
108 
115  public function getOrderViewUrl($orderId)
116  {
117  return $this->getUrl('sales/order/view', ['order_id' => $orderId]);
118  }
119 
125  public function getBackUrl()
126  {
127  if ($this->getRefererUrl()) {
128  return $this->getRefererUrl();
129  }
130  return $this->getUrl('customer/account/');
131  }
132 
139  public function getRemainingDownloads($item)
140  {
141  if ($item->getNumberOfDownloadsBought()) {
142  $downloads = $item->getNumberOfDownloadsBought() - $item->getNumberOfDownloadsUsed();
143  return $downloads;
144  }
145  return __('Unlimited');
146  }
147 
154  public function getDownloadUrl($item)
155  {
156  return $this->getUrl('downloadable/download/link', ['id' => $item->getLinkHash(), '_secure' => true]);
157  }
158 
165  public function getIsOpenInNewWindow()
166  {
167  return $this->_scopeConfig->isSetFlag(
168  \Magento\Downloadable\Model\Link::XML_PATH_TARGET_NEW_WINDOW,
169  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
170  );
171  }
172 }
__()
Definition: __.php:13
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer, \Magento\Downloadable\Model\ResourceModel\Link\Purchased\CollectionFactory $linksFactory, \Magento\Downloadable\Model\ResourceModel\Link\Purchased\Item\CollectionFactory $itemsFactory, array $data=[])
$_item
Definition: default.phtml:11