Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Links.php
Go to the documentation of this file.
1 <?php
7 
12 
20 {
24  protected $pricingHelper;
25 
29  protected $encoder;
30 
37  public function __construct(
38  \Magento\Catalog\Block\Product\Context $context,
39  \Magento\Framework\Pricing\Helper\Data $pricingHelper,
41  array $data = []
42  ) {
43  $this->pricingHelper = $pricingHelper;
44  $this->encoder = $encoder;
45  parent::__construct($context, $data);
46  }
47 
54  public function getLinksPurchasedSeparately()
55  {
56  return $this->getProduct()->getLinksPurchasedSeparately();
57  }
58 
63  public function getLinkSelectionRequired()
64  {
65  return $this->getProduct()->getTypeInstance()->getLinkSelectionRequired($this->getProduct());
66  }
67 
71  public function hasLinks()
72  {
73  return $this->getProduct()->getTypeInstance()->hasLinks($this->getProduct());
74  }
75 
79  public function getLinks()
80  {
81  return $this->getProduct()->getTypeInstance()->getLinks($this->getProduct());
82  }
83 
90  public function getCurrencyPrice($price)
91  {
92  $store = $this->getProduct()->getStore();
93  return $this->pricingHelper->currencyByStore($price, $store, false);
94  }
95 
99  public function getJsonConfig()
100  {
101  $finalPrice = $this->getProduct()->getPriceInfo()
102  ->getPrice(FinalPrice::PRICE_CODE);
103 
104  $linksConfig = [];
105  foreach ($this->getLinks() as $link) {
106  $amount = $finalPrice->getCustomAmount($link->getPrice());
107  $linksConfig[$link->getId()] = [
108  'finalPrice' => $amount->getValue(),
109  'basePrice' => $amount->getBaseAmount()
110  ];
111  }
112 
113  return $this->encoder->encode(['links' => $linksConfig]);
114  }
115 
120  public function getLinkSampleUrl($link)
121  {
122  $store = $this->getProduct()->getStore();
123  return $store->getUrl('downloadable/download/linkSample', ['link_id' => $link->getId()]);
124  }
125 
131  public function getLinksTitle()
132  {
133  if ($this->getProduct()->getLinksTitle()) {
134  return $this->getProduct()->getLinksTitle();
135  }
136  return $this->_scopeConfig->getValue(
137  \Magento\Downloadable\Model\Link::XML_PATH_LINKS_TITLE,
138  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
139  );
140  }
141 
148  public function getIsOpenInNewWindow()
149  {
150  return $this->_scopeConfig->isSetFlag(
151  \Magento\Downloadable\Model\Link::XML_PATH_TARGET_NEW_WINDOW,
152  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
153  );
154  }
155 
163  public function getIsLinkChecked($link)
164  {
165  $configValue = $this->getProduct()->getPreconfiguredValues()->getLinks();
166  if (!$configValue || !is_array($configValue)) {
167  return false;
168  }
169 
170  return $configValue && in_array($link->getId(), $configValue);
171  }
172 
179  public function getLinkCheckedValue($link)
180  {
181  return $this->getIsLinkChecked($link) ? 'checked' : '';
182  }
183 
188  protected function getLinkAmount($link)
189  {
190  return $this->getPriceType()->getLinkAmount($link);
191  }
192 
197  public function getLinkPrice(Link $link)
198  {
199  return $this->getLayout()->getBlock('product.price.render.default')->renderAmount(
200  $this->getLinkAmount($link),
201  $this->getPriceType(),
202  $this->getProduct()
203  );
204  }
205 
211  protected function getPriceType()
212  {
213  return $this->getProduct()->getPriceInfo()->getPrice(LinkPrice::PRICE_CODE);
214  }
215 }
$price
$amount
Definition: order.php:14