Downloadable.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Pricing\ConfiguredPrice;
  7. use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
  8. use Magento\Catalog\Pricing\Price\ConfiguredPriceInterface;
  9. use Magento\Catalog\Pricing\Price\FinalPrice;
  10. class Downloadable extends FinalPrice implements ConfiguredPriceInterface
  11. {
  12. /**
  13. * Price type configured
  14. */
  15. const PRICE_CODE = 'configured_price';
  16. /**
  17. * @var ItemInterface
  18. */
  19. private $item;
  20. /**
  21. * @inheritdoc
  22. */
  23. public function getValue()
  24. {
  25. return max(0, parent::getValue() + $this->getLinkPrice());
  26. }
  27. /**
  28. * Retrieve calculated links price
  29. *
  30. * @return int
  31. */
  32. private function getLinkPrice()
  33. {
  34. $result = 0;
  35. if ($this->getProduct()->getLinksPurchasedSeparately()) {
  36. /** @var \Magento\Wishlist\Model\Item\Option $customOption */
  37. $customOption = $this->getProduct()->getCustomOption('downloadable_link_ids');
  38. if ($customOption) {
  39. $links = $this->getLinks();
  40. $linkIds = explode(',', $customOption->getValue());
  41. foreach ($linkIds as $linkId) {
  42. if (isset($links[$linkId])) {
  43. $result += $links[$linkId]->getPrice();
  44. }
  45. }
  46. }
  47. }
  48. return $result;
  49. }
  50. /**
  51. * @return \Magento\Downloadable\Model\Link[]
  52. */
  53. private function getLinks()
  54. {
  55. /** @var \Magento\Downloadable\Model\Product\Type $productType */
  56. $productType = $this->getProduct()->getTypeInstance();
  57. $links = $productType->getLinks($this->getProduct());
  58. return $links;
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function setItem(ItemInterface $item)
  64. {
  65. $this->item = $item;
  66. return $this;
  67. }
  68. }