CartItemProcessor.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model\Quote\Item;
  7. use Magento\Quote\Model\Quote\Item\CartItemProcessorInterface;
  8. use Magento\Quote\Api\Data\CartItemInterface;
  9. use Magento\Framework\DataObject\Factory as DataObjectFactory;
  10. class CartItemProcessor implements CartItemProcessorInterface
  11. {
  12. /**
  13. * @var DataObjectFactory
  14. */
  15. private $objectFactory;
  16. /**
  17. * @var \Magento\Framework\Api\DataObjectHelper
  18. */
  19. private $dataObjectHelper;
  20. /**
  21. * @var \Magento\Downloadable\Model\DownloadableOptionFactory
  22. */
  23. private $downloadableOptionFactory;
  24. /**
  25. * @var \Magento\Quote\Model\Quote\ProductOptionFactory
  26. */
  27. private $productOptionFactory;
  28. /**
  29. * @var \Magento\Quote\Api\Data\ProductOptionExtensionFactory
  30. */
  31. private $extensionFactory;
  32. /**
  33. * @param DataObjectFactory $objectFactory
  34. * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
  35. * @param \Magento\Downloadable\Model\DownloadableOptionFactory $downloadableOptionFactory
  36. * @param \Magento\Quote\Model\Quote\ProductOptionFactory $productOptionFactory
  37. * @param \Magento\Quote\Api\Data\ProductOptionExtensionFactory $extensionFactory
  38. */
  39. public function __construct(
  40. DataObjectFactory $objectFactory,
  41. \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
  42. \Magento\Downloadable\Model\DownloadableOptionFactory $downloadableOptionFactory,
  43. \Magento\Quote\Model\Quote\ProductOptionFactory $productOptionFactory,
  44. \Magento\Quote\Api\Data\ProductOptionExtensionFactory $extensionFactory
  45. ) {
  46. $this->objectFactory = $objectFactory;
  47. $this->dataObjectHelper = $dataObjectHelper;
  48. $this->downloadableOptionFactory = $downloadableOptionFactory;
  49. $this->productOptionFactory = $productOptionFactory;
  50. $this->extensionFactory = $extensionFactory;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function convertToBuyRequest(CartItemInterface $cartItem)
  56. {
  57. if ($cartItem->getProductOption()
  58. && $cartItem->getProductOption()->getExtensionAttributes()
  59. && $cartItem->getProductOption()->getExtensionAttributes()->getDownloadableOption()
  60. ) {
  61. $downloadableLinks = $cartItem->getProductOption()->getExtensionAttributes()->getDownloadableOption()
  62. ->getDownloadableLinks();
  63. if (!empty($downloadableLinks)) {
  64. return $this->objectFactory->create(
  65. ['links' => $downloadableLinks]
  66. );
  67. }
  68. }
  69. return null;
  70. }
  71. /**
  72. * Process cart item product options
  73. *
  74. * @param CartItemInterface $cartItem
  75. * @return CartItemInterface
  76. */
  77. public function processOptions(CartItemInterface $cartItem)
  78. {
  79. $downloadableLinkIds = [];
  80. $option = $cartItem->getOptionByCode('downloadable_link_ids');
  81. if (!empty($option)) {
  82. $downloadableLinkIds = explode(',', $option->getValue());
  83. }
  84. $downloadableOption = $this->downloadableOptionFactory->create();
  85. $this->dataObjectHelper->populateWithArray(
  86. $downloadableOption,
  87. [
  88. 'downloadable_links' => $downloadableLinkIds
  89. ],
  90. \Magento\Downloadable\Api\Data\DownloadableOptionInterface::class
  91. );
  92. $productOption = ($cartItem->getProductOption())
  93. ? $cartItem->getProductOption()
  94. : $this->productOptionFactory->create();
  95. $extensibleAttribute = ($productOption->getExtensionAttributes())
  96. ? $productOption->getExtensionAttributes()
  97. : $this->extensionFactory->create();
  98. $extensibleAttribute->setDownloadableOption($downloadableOption);
  99. $productOption->setExtensionAttributes($extensibleAttribute);
  100. $cartItem->setProductOption($productOption);
  101. return $cartItem;
  102. }
  103. }