Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ListCompare.php
Go to the documentation of this file.
1 <?php
8 
12 
20 {
26  protected $_items;
27 
33  protected $_attributes;
34 
40  protected $_useLinkForAsLowAs = false;
41 
47  protected $_customerId = null;
48 
52  protected $httpContext;
53 
59  protected $_customerVisitor;
60 
67 
74 
78  protected $urlEncoder;
79 
83  protected $currentCustomer;
84 
95  public function __construct(
96  \Magento\Catalog\Block\Product\Context $context,
97  \Magento\Framework\Url\EncoderInterface $urlEncoder,
98  \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory $itemCollectionFactory,
99  \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
100  \Magento\Customer\Model\Visitor $customerVisitor,
101  \Magento\Framework\App\Http\Context $httpContext,
102  \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
103  array $data = []
104  ) {
105  $this->urlEncoder = $urlEncoder;
106  $this->_itemCollectionFactory = $itemCollectionFactory;
107  $this->_catalogProductVisibility = $catalogProductVisibility;
108  $this->_customerVisitor = $customerVisitor;
109  $this->httpContext = $httpContext;
110  $this->currentCustomer = $currentCustomer;
111  parent::__construct(
112  $context,
113  $data
114  );
115  }
116 
124  {
125  $continueUrl = $this->urlEncoder->encode($this->getUrl('customer/account'));
126  $urlParamName = Action::PARAM_NAME_URL_ENCODED;
127 
128  $continueUrlParams = [$urlParamName => $continueUrl];
129 
130  return $this->_wishlistHelper->getAddParams($product, $continueUrlParams);
131  }
132 
138  protected function _prepareLayout()
139  {
140  $this->pageConfig->getTitle()->set(
141  __('Products Comparison List') . ' - ' . $this->pageConfig->getTitle()->getDefault()
142  );
143  return parent::_prepareLayout();
144  }
145 
151  public function getItems()
152  {
153  if ($this->_items === null) {
154  $this->_compareProduct->setAllowUsedFlat(false);
155 
156  $this->_items = $this->_itemCollectionFactory->create();
157  $this->_items->useProductItem(true)->setStoreId($this->_storeManager->getStore()->getId());
158 
159  if ($this->httpContext->getValue(Context::CONTEXT_AUTH)) {
160  $this->_items->setCustomerId($this->currentCustomer->getCustomerId());
161  } elseif ($this->_customerId) {
162  $this->_items->setCustomerId($this->_customerId);
163  } else {
164  $this->_items->setVisitorId($this->_customerVisitor->getId());
165  }
166 
167  $this->_items->addAttributeToSelect(
168  $this->_catalogConfig->getProductAttributes()
169  )->loadComparableAttributes()->addMinimalPrice()->addTaxPercents()->setVisibility(
170  $this->_catalogProductVisibility->getVisibleInSiteIds()
171  );
172  }
173 
174  return $this->_items;
175  }
176 
182  public function getAttributes()
183  {
184  if ($this->_attributes === null) {
185  $this->_attributes = $this->getItems()->getComparableAttributes();
186  }
187 
188  return $this->_attributes;
189  }
190 
199  {
200  if (!$product->hasData($attribute->getAttributeCode())) {
201  return __('N/A');
202  }
203 
204  if ($attribute->getSourceModel() || in_array(
205  $attribute->getFrontendInput(),
206  ['select', 'boolean', 'multiselect']
207  )
208  ) {
209  $value = $attribute->getFrontend()->getValue($product);
210  } else {
211  $value = $product->getData($attribute->getAttributeCode());
212  }
213  return (string)$value == '' ? __('No') : $value;
214  }
215 
224  {
225  foreach ($this->getItems() as $item) {
226  if ($item->hasData($attribute->getAttributeCode())) {
227  return true;
228  }
229  }
230  return false;
231  }
232 
238  public function getPrintUrl()
239  {
240  return $this->getUrl('*/*/*', ['_current' => true, 'print' => 1]);
241  }
242 
249  public function setCustomerId($id)
250  {
251  $this->_customerId = $id;
252  return $this;
253  }
254 
262  public function getProductPrice(\Magento\Catalog\Model\Product $product, $idSuffix = '')
263  {
265  $priceRender = $this->getLayout()->getBlock('product.price.render.default');
266 
267  $price = '';
268  if ($priceRender) {
269  $price = $priceRender->render(
270  \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
271  $product,
272  [
273  'price_id' => 'product-price-' . $product->getId() . $idSuffix,
274  'display_minimal_price' => true,
276  ]
277  );
278  }
279  return $price;
280  }
281 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$price
$value
Definition: gender.phtml:16
getProductPrice(\Magento\Catalog\Model\Product $product)
$idSuffix
__construct(\Magento\Catalog\Block\Product\Context $context, \Magento\Framework\Url\EncoderInterface $urlEncoder, \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory $itemCollectionFactory, \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility, \Magento\Customer\Model\Visitor $customerVisitor, \Magento\Framework\App\Http\Context $httpContext, \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer, array $data=[])
Definition: ListCompare.php:95