Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
View.php
Go to the documentation of this file.
1 <?php
7 
10 
17 class View extends AbstractProduct implements \Magento\Framework\DataObject\IdentityInterface
18 {
24  protected $string;
25 
29  protected $_jsonEncoder;
30 
35  protected $priceCurrency;
36 
40  protected $urlEncoder;
41 
45  protected $_productHelper;
46 
50  protected $productTypeConfig;
51 
55  protected $_localeFormat;
56 
60  protected $customerSession;
61 
65  protected $productRepository;
66 
82  public function __construct(
83  \Magento\Catalog\Block\Product\Context $context,
84  \Magento\Framework\Url\EncoderInterface $urlEncoder,
85  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
86  \Magento\Framework\Stdlib\StringUtils $string,
87  \Magento\Catalog\Helper\Product $productHelper,
89  \Magento\Framework\Locale\FormatInterface $localeFormat,
90  \Magento\Customer\Model\Session $customerSession,
93  array $data = []
94  ) {
95  $this->_productHelper = $productHelper;
96  $this->urlEncoder = $urlEncoder;
97  $this->_jsonEncoder = $jsonEncoder;
98  $this->productTypeConfig = $productTypeConfig;
99  $this->string = $string;
100  $this->_localeFormat = $localeFormat;
101  $this->customerSession = $customerSession;
102  $this->productRepository = $productRepository;
103  $this->priceCurrency = $priceCurrency;
104  parent::__construct(
105  $context,
106  $data
107  );
108  }
109 
110  // @codingStandardsIgnoreEnd
111 
118  public function getWishlistOptions()
119  {
120  return ['productType' => $this->getProduct()->getTypeId()];
121  }
122 
128  public function getProduct()
129  {
130  if (!$this->_coreRegistry->registry('product') && $this->getProductId()) {
131  $product = $this->productRepository->getById($this->getProductId());
132  $this->_coreRegistry->register('product', $product);
133  }
134  return $this->_coreRegistry->registry('product');
135  }
136 
142  public function canEmailToFriend()
143  {
144  return false;
145  }
146 
154  public function getAddToCartUrl($product, $additional = [])
155  {
156  if ($this->hasCustomAddToCartUrl()) {
157  return $this->getCustomAddToCartUrl();
158  }
159 
160  if ($this->getRequest()->getParam('wishlist_next')) {
161  $additional['wishlist_next'] = 1;
162  }
163 
165  $addUrlValue = $this->_urlBuilder->getUrl('*/*/*', ['_use_rewrite' => true, '_current' => true]);
166  $additional[$addUrlKey] = $this->urlEncoder->encode($addUrlValue);
167 
168  return $this->_cartHelper->getAddUrl($product, $additional);
169  }
170 
177  public function getJsonConfig()
178  {
179  /* @var $product \Magento\Catalog\Model\Product */
180  $product = $this->getProduct();
181 
182  if (!$this->hasOptions()) {
183  $config = [
184  'productId' => $product->getId(),
185  'priceFormat' => $this->_localeFormat->getPriceFormat()
186  ];
187  return $this->_jsonEncoder->encode($config);
188  }
189 
190  $tierPrices = [];
191  $tierPricesList = $product->getPriceInfo()->getPrice('tier_price')->getTierPriceList();
192  foreach ($tierPricesList as $tierPrice) {
193  $tierPrices[] = $tierPrice['price']->getValue();
194  }
195  $config = [
196  'productId' => $product->getId(),
197  'priceFormat' => $this->_localeFormat->getPriceFormat(),
198  'prices' => [
199  'oldPrice' => [
200  'amount' => $product->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue(),
201  'adjustments' => []
202  ],
203  'basePrice' => [
204  'amount' => $product->getPriceInfo()->getPrice('final_price')->getAmount()->getBaseAmount(),
205  'adjustments' => []
206  ],
207  'finalPrice' => [
208  'amount' => $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue(),
209  'adjustments' => []
210  ]
211  ],
212  'idSuffix' => '_clone',
213  'tierPrices' => $tierPrices
214  ];
215 
216  $responseObject = new \Magento\Framework\DataObject();
217  $this->_eventManager->dispatch('catalog_product_view_config', ['response_object' => $responseObject]);
218  if (is_array($responseObject->getAdditionalOptions())) {
219  foreach ($responseObject->getAdditionalOptions() as $option => $value) {
221  }
222  }
223 
224  return $this->_jsonEncoder->encode($config);
225  }
226 
232  public function hasOptions()
233  {
234  if ($this->getProduct()->getTypeInstance()->hasOptions($this->getProduct())) {
235  return true;
236  }
237  return false;
238  }
239 
245  public function hasRequiredOptions()
246  {
247  return $this->getProduct()->getTypeInstance()->hasRequiredOptions($this->getProduct());
248  }
249 
258  public function isStartCustomization()
259  {
260  return $this->getProduct()->getConfigureMode() || $this->_request->getParam('startcustomization');
261  }
262 
270  public function getProductDefaultQty($product = null)
271  {
272  if (!$product) {
273  $product = $this->getProduct();
274  }
275 
276  $qty = $this->getMinimalQty($product);
277  $config = $product->getPreconfiguredValues();
278  $configQty = $config->getQty();
279  if ($configQty > $qty) {
280  $qty = $configQty;
281  }
282 
283  return $qty;
284  }
285 
291  public function getOptionsContainer()
292  {
293  return $this->getProduct()->getOptionsContainer() == 'container1' ? 'container1' : 'container2';
294  }
295 
301  public function shouldRenderQuantity()
302  {
303  return !$this->productTypeConfig->isProductSet($this->getProduct()->getTypeId());
304  }
305 
311  public function getQuantityValidators()
312  {
313  $validators = [];
314  $validators['required-number'] = true;
315  return $validators;
316  }
317 
323  public function getIdentities()
324  {
325  $identities = $this->getProduct()->getIdentities();
326  $category = $this->_coreRegistry->registry('current_category');
327  if ($category) {
328  $identities[] = Category::CACHE_TAG . '_' . $category->getId();
329  }
330  return $identities;
331  }
332 
338  protected function getCustomerId()
339  {
340  return $this->customerSession->getCustomerId();
341  }
342 }
$config
Definition: fraud_order.php:17
getAddToCartUrl($product, $additional=[])
Definition: View.php:154
getProductDefaultQty($product=null)
Definition: View.php:270
$value
Definition: gender.phtml:16
__construct(\Magento\Catalog\Block\Product\Context $context, \Magento\Framework\Url\EncoderInterface $urlEncoder, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Framework\Stdlib\StringUtils $string, \Magento\Catalog\Helper\Product $productHelper, \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig, \Magento\Framework\Locale\FormatInterface $localeFormat, \Magento\Customer\Model\Session $customerSession, ProductRepositoryInterface $productRepository, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, array $data=[])
Definition: View.php:82