CartItemProcessor.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Model;
  7. use Magento\Quote\Model\Quote\Item\CartItemProcessorInterface;
  8. use Magento\Quote\Api\Data\CartItemInterface;
  9. use Magento\Bundle\Api\Data\BundleOptionInterfaceFactory;
  10. use Magento\Quote\Api\Data as QuoteApi;
  11. class CartItemProcessor implements CartItemProcessorInterface
  12. {
  13. /**
  14. * @var \Magento\Framework\DataObject\Factory
  15. */
  16. protected $objectFactory;
  17. /**
  18. * @var QuoteApi\ProductOptionExtensionFactory
  19. */
  20. protected $productOptionExtensionFactory;
  21. /**
  22. * @var BundleOptionInterfaceFactory
  23. */
  24. protected $bundleOptionFactory;
  25. /**
  26. * @var QuoteApi\ProductOptionInterfaceFactory
  27. */
  28. protected $productOptionFactory;
  29. /**
  30. * @param \Magento\Framework\DataObject\Factory $objectFactory
  31. * @param QuoteApi\ProductOptionExtensionFactory $productOptionExtensionFactory
  32. * @param BundleOptionInterfaceFactory $bundleOptionFactory
  33. * @param QuoteApi\ProductOptionInterfaceFactory $productOptionFactory
  34. */
  35. public function __construct(
  36. \Magento\Framework\DataObject\Factory $objectFactory,
  37. QuoteApi\ProductOptionExtensionFactory $productOptionExtensionFactory,
  38. BundleOptionInterfaceFactory $bundleOptionFactory,
  39. QuoteApi\ProductOptionInterfaceFactory $productOptionFactory
  40. ) {
  41. $this->objectFactory = $objectFactory;
  42. $this->productOptionExtensionFactory = $productOptionExtensionFactory;
  43. $this->bundleOptionFactory = $bundleOptionFactory;
  44. $this->productOptionFactory = $productOptionFactory;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function convertToBuyRequest(CartItemInterface $cartItem)
  50. {
  51. if ($cartItem->getProductOption() && $cartItem->getProductOption()->getExtensionAttributes()) {
  52. $options = $cartItem->getProductOption()->getExtensionAttributes()->getBundleOptions();
  53. if (is_array($options)) {
  54. $requestData = [];
  55. foreach ($options as $option) {
  56. /** @var \Magento\Bundle\Api\Data\BundleOptionInterface $option */
  57. foreach ($option->getOptionSelections() as $selection) {
  58. $requestData['bundle_option'][$option->getOptionId()][] = $selection;
  59. $requestData['bundle_option_qty'][$option->getOptionId()] = $option->getOptionQty();
  60. }
  61. }
  62. return $this->objectFactory->create($requestData);
  63. }
  64. }
  65. return null;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. * @SuppressWarnings(PHPMD.NPathComplexity)
  70. */
  71. public function processOptions(CartItemInterface $cartItem)
  72. {
  73. if ($cartItem->getProductType() !== \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
  74. return $cartItem;
  75. }
  76. $productOptions = [];
  77. $bundleOptions = $cartItem->getBuyRequest()->getBundleOption();
  78. $bundleOptionsQty = $cartItem->getBuyRequest()->getBundleOptionQty();
  79. if (is_array($bundleOptions)) {
  80. foreach ($bundleOptions as $optionId => $optionSelections) {
  81. if (empty($optionSelections)) {
  82. continue;
  83. }
  84. $optionSelections = is_array($optionSelections) ? $optionSelections : [$optionSelections];
  85. $optionQty = isset($bundleOptionsQty[$optionId]) ? $bundleOptionsQty[$optionId] : 1;
  86. /** @var \Magento\Bundle\Api\Data\BundleOptionInterface $productOption */
  87. $productOption = $this->bundleOptionFactory->create();
  88. $productOption->setOptionId($optionId);
  89. $productOption->setOptionSelections($optionSelections);
  90. $productOption->setOptionQty($optionQty);
  91. $productOptions[] = $productOption;
  92. }
  93. $extension = $this->productOptionExtensionFactory->create()->setBundleOptions($productOptions);
  94. if (!$cartItem->getProductOption()) {
  95. $cartItem->setProductOption($this->productOptionFactory->create());
  96. }
  97. $cartItem->getProductOption()->setExtensionAttributes($extension);
  98. }
  99. return $cartItem;
  100. }
  101. }