ConfiguredPrice.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GroupedProduct\Pricing\Price;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
  9. use Magento\Catalog\Pricing\Price\ConfiguredPriceInterface;
  10. use Magento\Catalog\Pricing\Price\FinalPrice as CatalogFinalPrice;
  11. use Magento\Framework\Pricing\Amount\AmountInterface;
  12. class ConfiguredPrice extends CatalogFinalPrice implements ConfiguredPriceInterface
  13. {
  14. /**
  15. * Price type configured
  16. */
  17. const PRICE_CODE = self::CONFIGURED_PRICE_CODE;
  18. /**
  19. * @var null|ItemInterface
  20. */
  21. protected $item;
  22. /**
  23. * @param ItemInterface $item
  24. * @return $this
  25. */
  26. public function setItem(ItemInterface $item)
  27. {
  28. $this->item = $item;
  29. return $this;
  30. }
  31. /**
  32. * Calculate configured price
  33. *
  34. * @return float
  35. */
  36. protected function calculatePrice()
  37. {
  38. $value = 0.;
  39. /** @var \Magento\GroupedProduct\Model\Product\Type\Grouped $typeInstance */
  40. $typeInstance = $this->getProduct()->getTypeInstance();
  41. $associatedProducts = $typeInstance
  42. ->setStoreFilter($this->getProduct()->getStore(), $this->getProduct())
  43. ->getAssociatedProducts($this->getProduct());
  44. foreach ($associatedProducts as $product) {
  45. /** @var Product $product */
  46. /** @var \Magento\Wishlist\Model\Item\Option $customOption */
  47. $customOption = $this->getProduct()
  48. ->getCustomOption('associated_product_' . $product->getId());
  49. if (!$customOption) {
  50. continue;
  51. }
  52. $finalPrice = $product->getPriceInfo()
  53. ->getPrice(FinalPrice::PRICE_CODE)
  54. ->getValue();
  55. $value += $finalPrice * ($customOption->getValue() ? $customOption->getValue() : 1);
  56. }
  57. return $value;
  58. }
  59. /**
  60. * Price value of product with configured options
  61. *
  62. * @return bool|float
  63. */
  64. public function getValue()
  65. {
  66. return $this->item ? $this->calculatePrice() : parent::getValue();
  67. }
  68. }