PageRenderTime 321ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-bundle/Block/Catalog/Product/View/Type/Bundle.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 354 lines | 215 code | 39 blank | 100 comment | 15 complexity | c8a6b270127aacb602fe89299cd1ba24 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Block\Catalog\Product\View\Type;
  7. use Magento\Bundle\Model\Option;
  8. use Magento\Catalog\Model\Product;
  9. /**
  10. * Catalog bundle product info block
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class Bundle extends \Magento\Catalog\Block\Product\View\AbstractView
  15. {
  16. /**
  17. * @var array
  18. */
  19. protected $options;
  20. /**
  21. * Catalog product
  22. *
  23. * @var \Magento\Catalog\Helper\Product
  24. */
  25. protected $catalogProduct;
  26. /**
  27. * @var \Magento\Bundle\Model\Product\PriceFactory
  28. */
  29. protected $productPriceFactory;
  30. /**
  31. * @var \Magento\Framework\Json\EncoderInterface
  32. */
  33. protected $jsonEncoder;
  34. /**
  35. * @var \Magento\Framework\Locale\FormatInterface
  36. */
  37. protected $localeFormat;
  38. /**
  39. * @var array
  40. */
  41. private $selectedOptions = [];
  42. /**
  43. * @param \Magento\Catalog\Block\Product\Context $context
  44. * @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
  45. * @param \Magento\Catalog\Helper\Product $catalogProduct
  46. * @param \Magento\Bundle\Model\Product\PriceFactory $productPrice
  47. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  48. * @param \Magento\Framework\Locale\FormatInterface $localeFormat
  49. * @param array $data
  50. */
  51. public function __construct(
  52. \Magento\Catalog\Block\Product\Context $context,
  53. \Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
  54. \Magento\Catalog\Helper\Product $catalogProduct,
  55. \Magento\Bundle\Model\Product\PriceFactory $productPrice,
  56. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  57. \Magento\Framework\Locale\FormatInterface $localeFormat,
  58. array $data = []
  59. ) {
  60. $this->catalogProduct = $catalogProduct;
  61. $this->productPriceFactory = $productPrice;
  62. $this->jsonEncoder = $jsonEncoder;
  63. $this->localeFormat = $localeFormat;
  64. parent::__construct(
  65. $context,
  66. $arrayUtils,
  67. $data
  68. );
  69. }
  70. /**
  71. * @return array
  72. */
  73. public function getOptions()
  74. {
  75. if (!$this->options) {
  76. $product = $this->getProduct();
  77. $typeInstance = $product->getTypeInstance();
  78. $typeInstance->setStoreFilter($product->getStoreId(), $product);
  79. $optionCollection = $typeInstance->getOptionsCollection($product);
  80. $selectionCollection = $typeInstance->getSelectionsCollection(
  81. $typeInstance->getOptionsIds($product),
  82. $product
  83. );
  84. $this->options = $optionCollection->appendSelections(
  85. $selectionCollection,
  86. false,
  87. $this->catalogProduct->getSkipSaleableCheck()
  88. );
  89. }
  90. return $this->options;
  91. }
  92. /**
  93. * @return bool
  94. */
  95. public function hasOptions()
  96. {
  97. $this->getOptions();
  98. if (empty($this->options) || !$this->getProduct()->isSalable()) {
  99. return false;
  100. }
  101. return true;
  102. }
  103. /**
  104. * Returns JSON encoded config to be used in JS scripts
  105. *
  106. * @return string
  107. *
  108. */
  109. public function getJsonConfig()
  110. {
  111. /** @var Option[] $optionsArray */
  112. $optionsArray = $this->getOptions();
  113. $options = [];
  114. $currentProduct = $this->getProduct();
  115. $defaultValues = [];
  116. $preConfiguredFlag = $currentProduct->hasPreconfiguredValues();
  117. /** @var \Magento\Framework\DataObject|null $preConfiguredValues */
  118. $preConfiguredValues = $preConfiguredFlag ? $currentProduct->getPreconfiguredValues() : null;
  119. $position = 0;
  120. foreach ($optionsArray as $optionItem) {
  121. /* @var $optionItem Option */
  122. if (!$optionItem->getSelections()) {
  123. continue;
  124. }
  125. $optionId = $optionItem->getId();
  126. $options[$optionId] = $this->getOptionItemData($optionItem, $currentProduct, $position);
  127. // Add attribute default value (if set)
  128. if ($preConfiguredFlag) {
  129. $configValue = $preConfiguredValues->getData('bundle_option/' . $optionId);
  130. if ($configValue) {
  131. $defaultValues[$optionId] = $configValue;
  132. }
  133. }
  134. $position++;
  135. }
  136. $config = $this->getConfigData($currentProduct, $options);
  137. $configObj = new \Magento\Framework\DataObject(
  138. [
  139. 'config' => $config,
  140. ]
  141. );
  142. //pass the return array encapsulated in an object for the other modules to be able to alter it eg: weee
  143. $this->_eventManager->dispatch('catalog_product_option_price_configuration_after', ['configObj' => $configObj]);
  144. $config=$configObj->getConfig();
  145. if ($preConfiguredFlag && !empty($defaultValues)) {
  146. $config['defaultValues'] = $defaultValues;
  147. }
  148. return $this->jsonEncoder->encode($config);
  149. }
  150. /**
  151. * Get html for option
  152. *
  153. * @param Option $option
  154. * @return string
  155. */
  156. public function getOptionHtml(Option $option)
  157. {
  158. $optionBlock = $this->getChildBlock($option->getType());
  159. if (!$optionBlock) {
  160. return __('There is no defined renderer for "%1" option type.', $option->getType());
  161. }
  162. return $optionBlock->setOption($option)->toHtml();
  163. }
  164. /**
  165. * Get formed data from option selection item
  166. *
  167. * @param Product $product
  168. * @param Product $selection
  169. * @return array
  170. */
  171. private function getSelectionItemData(Product $product, Product $selection)
  172. {
  173. $qty = ($selection->getSelectionQty() * 1) ?: '1';
  174. $optionPriceAmount = $product->getPriceInfo()
  175. ->getPrice('bundle_option')
  176. ->getOptionSelectionAmount($selection);
  177. $finalPrice = $optionPriceAmount->getValue();
  178. $basePrice = $optionPriceAmount->getBaseAmount();
  179. $selection = [
  180. 'qty' => $qty,
  181. 'customQty' => $selection->getSelectionCanChangeQty(),
  182. 'optionId' => $selection->getId(),
  183. 'prices' => [
  184. 'oldPrice' => [
  185. 'amount' => $basePrice
  186. ],
  187. 'basePrice' => [
  188. 'amount' => $basePrice
  189. ],
  190. 'finalPrice' => [
  191. 'amount' => $finalPrice
  192. ]
  193. ],
  194. 'priceType' => $selection->getSelectionPriceType(),
  195. 'tierPrice' => $this->getTierPrices($product, $selection),
  196. 'name' => $selection->getName(),
  197. 'canApplyMsrp' => false
  198. ];
  199. return $selection;
  200. }
  201. /**
  202. * Get tier prices from option selection item
  203. *
  204. * @param Product $product
  205. * @param Product $selection
  206. * @return array
  207. */
  208. private function getTierPrices(Product $product, Product $selection)
  209. {
  210. // recalculate currency
  211. $tierPrices = $selection->getPriceInfo()
  212. ->getPrice(\Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE)
  213. ->getTierPriceList();
  214. foreach ($tierPrices as &$tierPriceInfo) {
  215. /** @var \Magento\Framework\Pricing\Amount\Base $price */
  216. $price = $tierPriceInfo['price'];
  217. $priceBaseAmount = $price->getBaseAmount();
  218. $priceValue = $price->getValue();
  219. $bundleProductPrice = $this->productPriceFactory->create();
  220. $priceBaseAmount = $bundleProductPrice->getLowestPrice($product, $priceBaseAmount);
  221. $priceValue = $bundleProductPrice->getLowestPrice($product, $priceValue);
  222. $tierPriceInfo['prices'] = [
  223. 'oldPrice' => [
  224. 'amount' => $priceBaseAmount
  225. ],
  226. 'basePrice' => [
  227. 'amount' => $priceBaseAmount
  228. ],
  229. 'finalPrice' => [
  230. 'amount' => $priceValue
  231. ]
  232. ];
  233. }
  234. return $tierPrices;
  235. }
  236. /**
  237. * Get formed data from selections of option
  238. *
  239. * @param Option $option
  240. * @param Product $product
  241. * @return array
  242. */
  243. private function getSelections(Option $option, Product $product)
  244. {
  245. $selections = [];
  246. $selectionCount = count($option->getSelections());
  247. foreach ($option->getSelections() as $selectionItem) {
  248. /* @var $selectionItem Product */
  249. $selectionId = $selectionItem->getSelectionId();
  250. $selections[$selectionId] = $this->getSelectionItemData($product, $selectionItem);
  251. if (($selectionItem->getIsDefault() || $selectionCount == 1 && $option->getRequired())
  252. && $selectionItem->isSalable()
  253. ) {
  254. $this->selectedOptions[$option->getId()][] = $selectionId;
  255. }
  256. }
  257. return $selections;
  258. }
  259. /**
  260. * Get formed data from option
  261. *
  262. * @param Option $option
  263. * @param Product $product
  264. * @param int $position
  265. * @return array
  266. */
  267. private function getOptionItemData(Option $option, Product $product, $position)
  268. {
  269. return [
  270. 'selections' => $this->getSelections($option, $product),
  271. 'title' => $option->getTitle(),
  272. 'isMulti' => in_array($option->getType(), ['multi', 'checkbox']),
  273. 'position' => $position
  274. ];
  275. }
  276. /**
  277. * Get formed config data from calculated options data
  278. *
  279. * @param Product $product
  280. * @param array $options
  281. * @return array
  282. */
  283. private function getConfigData(Product $product, array $options)
  284. {
  285. $isFixedPrice = $this->getProduct()->getPriceType() == \Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED;
  286. $productAmount = $product
  287. ->getPriceInfo()
  288. ->getPrice(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE)
  289. ->getPriceWithoutOption();
  290. $baseProductAmount = $product
  291. ->getPriceInfo()
  292. ->getPrice(\Magento\Catalog\Pricing\Price\RegularPrice::PRICE_CODE)
  293. ->getAmount();
  294. $config = [
  295. 'options' => $options,
  296. 'selected' => $this->selectedOptions,
  297. 'bundleId' => $product->getId(),
  298. 'priceFormat' => $this->localeFormat->getPriceFormat(),
  299. 'prices' => [
  300. 'oldPrice' => [
  301. 'amount' => $isFixedPrice ? $baseProductAmount->getValue() : 0
  302. ],
  303. 'basePrice' => [
  304. 'amount' => $isFixedPrice ? $productAmount->getBaseAmount() : 0
  305. ],
  306. 'finalPrice' => [
  307. 'amount' => $isFixedPrice ? $productAmount->getValue() : 0
  308. ]
  309. ],
  310. 'priceType' => $product->getPriceType(),
  311. 'isFixedPrice' => $isFixedPrice,
  312. ];
  313. return $config;
  314. }
  315. }