PageRenderTime 95ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-catalog/Block/Product/View/Options/AbstractOptions.php

https://gitlab.com/daigiangaitu91/magento
PHP | 184 lines | 88 code | 20 blank | 76 comment | 9 complexity | e3791de216558b83a9f63a300bfe5bab MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2015 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Product options abstract type block
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Catalog\Block\Product\View\Options;
  12. use Magento\Catalog\Pricing\Price\CustomOptionPriceInterface;
  13. abstract class AbstractOptions extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * Product object
  17. *
  18. * @var \Magento\Catalog\Model\Product
  19. */
  20. protected $_product;
  21. /**
  22. * Product option object
  23. *
  24. * @var \Magento\Catalog\Model\Product\Option
  25. */
  26. protected $_option;
  27. /**
  28. * @var \Magento\Framework\Pricing\Helper\Data
  29. */
  30. protected $pricingHelper;
  31. /**
  32. * @var \Magento\Catalog\Helper\Data
  33. */
  34. protected $_catalogHelper;
  35. /**
  36. * @param \Magento\Framework\View\Element\Template\Context $context
  37. * @param \Magento\Framework\Pricing\Helper\Data $pricingHelper
  38. * @param \Magento\Catalog\Helper\Data $catalogData,
  39. * @param array $data
  40. */
  41. public function __construct(
  42. \Magento\Framework\View\Element\Template\Context $context,
  43. \Magento\Framework\Pricing\Helper\Data $pricingHelper,
  44. \Magento\Catalog\Helper\Data $catalogData,
  45. array $data = []
  46. ) {
  47. $this->pricingHelper = $pricingHelper;
  48. $this->_catalogHelper = $catalogData;
  49. parent::__construct($context, $data);
  50. }
  51. /**
  52. * Set Product object
  53. *
  54. * @param \Magento\Catalog\Model\Product $product
  55. * @return \Magento\Catalog\Block\Product\View\Options\AbstractOptions
  56. */
  57. public function setProduct(\Magento\Catalog\Model\Product $product = null)
  58. {
  59. $this->_product = $product;
  60. return $this;
  61. }
  62. /**
  63. * Retrieve Product object
  64. *
  65. * @return \Magento\Catalog\Model\Product
  66. */
  67. public function getProduct()
  68. {
  69. return $this->_product;
  70. }
  71. /**
  72. * Set option
  73. *
  74. * @param \Magento\Catalog\Model\Product\Option $option
  75. * @return \Magento\Catalog\Block\Product\View\Options\AbstractOptions
  76. */
  77. public function setOption(\Magento\Catalog\Model\Product\Option $option)
  78. {
  79. $this->_option = $option;
  80. return $this;
  81. }
  82. /**
  83. * Get option
  84. *
  85. * @return \Magento\Catalog\Model\Product\Option
  86. */
  87. public function getOption()
  88. {
  89. return $this->_option;
  90. }
  91. /**
  92. * @return string
  93. */
  94. public function getFormatedPrice()
  95. {
  96. if ($option = $this->getOption()) {
  97. return $this->_formatPrice(
  98. [
  99. 'is_percent' => $option->getPriceType() == 'percent',
  100. 'pricing_value' => $option->getPrice($option->getPriceType() == 'percent'),
  101. ]
  102. );
  103. }
  104. return '';
  105. }
  106. /**
  107. * Return formated price
  108. *
  109. * @param array $value
  110. * @param bool $flag
  111. * @return string
  112. */
  113. protected function _formatPrice($value, $flag = true)
  114. {
  115. if ($value['pricing_value'] == 0) {
  116. return '';
  117. }
  118. $sign = '+';
  119. if ($value['pricing_value'] < 0) {
  120. $sign = '-';
  121. $value['pricing_value'] = 0 - $value['pricing_value'];
  122. }
  123. $priceStr = $sign;
  124. $customOptionPrice = $this->getProduct()->getPriceInfo()->getPrice('custom_option_price');
  125. $context = [CustomOptionPriceInterface::CONFIGURATION_OPTION_FLAG => true];
  126. $optionAmount = $customOptionPrice->getCustomAmount($value['pricing_value'], null, $context);
  127. $priceStr .= $this->getLayout()->getBlock('product.price.render.default')->renderAmount(
  128. $optionAmount,
  129. $customOptionPrice,
  130. $this->getProduct()
  131. );
  132. if ($flag) {
  133. $priceStr = '<span class="price-notice">' . $priceStr . '</span>';
  134. }
  135. return $priceStr;
  136. }
  137. /**
  138. * Get price with including/excluding tax
  139. *
  140. * @param float $price
  141. * @param bool $includingTax
  142. * @return float
  143. */
  144. public function getPrice($price, $includingTax = null)
  145. {
  146. if ($includingTax !== null) {
  147. $price = $this->_catalogHelper->getTaxPrice($this->getProduct(), $price, true);
  148. } else {
  149. $price = $this->_catalogHelper->getTaxPrice($this->getProduct(), $price);
  150. }
  151. return $price;
  152. }
  153. /**
  154. * Returns price converted to current currency rate
  155. *
  156. * @param float $price
  157. * @return float
  158. */
  159. public function getCurrencyPrice($price)
  160. {
  161. $store = $this->getProduct()->getStore();
  162. return $this->pricingHelper->currencyByStore($price, $store, false);
  163. }
  164. }