Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartItemProcessor.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Bundle\Model;
7 
10 use Magento\Bundle\Api\Data\BundleOptionInterfaceFactory;
12 
14 {
18  protected $objectFactory;
19 
24 
29 
34 
41  public function __construct(
43  QuoteApi\ProductOptionExtensionFactory $productOptionExtensionFactory,
44  BundleOptionInterfaceFactory $bundleOptionFactory,
45  QuoteApi\ProductOptionInterfaceFactory $productOptionFactory
46  ) {
47  $this->objectFactory = $objectFactory;
48  $this->productOptionExtensionFactory = $productOptionExtensionFactory;
49  $this->bundleOptionFactory = $bundleOptionFactory;
50  $this->productOptionFactory = $productOptionFactory;
51  }
52 
57  {
58  if ($cartItem->getProductOption() && $cartItem->getProductOption()->getExtensionAttributes()) {
59  $options = $cartItem->getProductOption()->getExtensionAttributes()->getBundleOptions();
60  if (is_array($options)) {
61  $requestData = [];
62  foreach ($options as $option) {
64  foreach ($option->getOptionSelections() as $selection) {
65  $requestData['bundle_option'][$option->getOptionId()][] = $selection;
66  $requestData['bundle_option_qty'][$option->getOptionId()] = $option->getOptionQty();
67  }
68  }
69  return $this->objectFactory->create($requestData);
70  }
71  }
72  return null;
73  }
74 
79  public function processOptions(CartItemInterface $cartItem)
80  {
81  if ($cartItem->getProductType() !== \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
82  return $cartItem;
83  }
84  $productOptions = [];
85  $bundleOptions = $cartItem->getBuyRequest()->getBundleOption();
86  $bundleOptionsQty = $cartItem->getBuyRequest()->getBundleOptionQty();
87  if (is_array($bundleOptions)) {
88  foreach ($bundleOptions as $optionId => $optionSelections) {
89  if (empty($optionSelections)) {
90  continue;
91  }
92  $optionSelections = is_array($optionSelections) ? $optionSelections : [$optionSelections];
93  $optionQty = isset($bundleOptionsQty[$optionId]) ? $bundleOptionsQty[$optionId] : 1;
94 
96  $productOption = $this->bundleOptionFactory->create();
97  $productOption->setOptionId($optionId);
98  $productOption->setOptionSelections($optionSelections);
99  $productOption->setOptionQty($optionQty);
101  }
102 
103  $extension = $this->productOptionExtensionFactory->create()->setBundleOptions($productOptions);
104  if (!$cartItem->getProductOption()) {
105  $cartItem->setProductOption($this->productOptionFactory->create());
106  }
107  $cartItem->getProductOption()->setExtensionAttributes($extension);
108  }
109  return $cartItem;
110  }
111 }
__construct(\Magento\Framework\DataObject\Factory $objectFactory, QuoteApi\ProductOptionExtensionFactory $productOptionExtensionFactory, BundleOptionInterfaceFactory $bundleOptionFactory, QuoteApi\ProductOptionInterfaceFactory $productOptionFactory)