Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractOptions.php
Go to the documentation of this file.
1 <?php
13 
15 
21 {
27  protected $_product;
28 
34  protected $_option;
35 
39  protected $pricingHelper;
40 
44  protected $_catalogHelper;
45 
52  public function __construct(
53  \Magento\Framework\View\Element\Template\Context $context,
54  \Magento\Framework\Pricing\Helper\Data $pricingHelper,
55  \Magento\Catalog\Helper\Data $catalogData,
56  array $data = []
57  ) {
58  $this->pricingHelper = $pricingHelper;
59  $this->_catalogHelper = $catalogData;
60  parent::__construct($context, $data);
61  }
62 
69  public function setProduct(\Magento\Catalog\Model\Product $product = null)
70  {
71  $this->_product = $product;
72  return $this;
73  }
74 
80  public function getProduct()
81  {
82  return $this->_product;
83  }
84 
91  public function setOption(\Magento\Catalog\Model\Product\Option $option)
92  {
93  $this->_option = $option;
94  return $this;
95  }
96 
102  public function getOption()
103  {
104  return $this->_option;
105  }
106 
113  public function getFormattedPrice()
114  {
115  if ($option = $this->getOption()) {
116  return $this->_formatPrice(
117  [
118  'is_percent' => $option->getPriceType() == 'percent',
119  'pricing_value' => $option->getPrice($option->getPriceType() == 'percent'),
120  ]
121  );
122  }
123  return '';
124  }
125 
132  public function getFormatedPrice()
133  {
134  return $this->getFormattedPrice();
135  }
136 
144  protected function _formatPrice($value, $flag = true)
145  {
146  if ($value['pricing_value'] == 0) {
147  return '';
148  }
149 
150  $sign = '+';
151  if ($value['pricing_value'] < 0) {
152  $sign = '-';
153  $value['pricing_value'] = 0 - $value['pricing_value'];
154  }
155 
156  $priceStr = $sign;
157 
158  $customOptionPrice = $this->getProduct()->getPriceInfo()->getPrice('custom_option_price');
159  $context = [CustomOptionPriceInterface::CONFIGURATION_OPTION_FLAG => true];
160  $optionAmount = $customOptionPrice->getCustomAmount($value['pricing_value'], null, $context);
161  $priceStr .= $this->getLayout()->getBlock('product.price.render.default')->renderAmount(
162  $optionAmount,
163  $customOptionPrice,
164  $this->getProduct()
165  );
166 
167  if ($flag) {
168  $priceStr = '<span class="price-notice">' . $priceStr . '</span>';
169  }
170 
171  return $priceStr;
172  }
173 
181  public function getPrice($price, $includingTax = null)
182  {
183  if ($includingTax !== null) {
184  $price = $this->_catalogHelper->getTaxPrice($this->getProduct(), $price, true);
185  } else {
186  $price = $this->_catalogHelper->getTaxPrice($this->getProduct(), $price);
187  }
188  return $price;
189  }
190 
197  public function getCurrencyPrice($price)
198  {
199  $store = $this->getProduct()->getStore();
200  return $this->pricingHelper->currencyByStore($price, $store, false);
201  }
202 }
$price
setOption(\Magento\Catalog\Model\Product\Option $option)
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Pricing\Helper\Data $pricingHelper, \Magento\Catalog\Helper\Data $catalogData, array $data=[])
$value
Definition: gender.phtml:16
setProduct(\Magento\Catalog\Model\Product $product=null)