Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductOptionProcessor.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Bundle\Model;
7 
9 use Magento\Bundle\Api\Data\BundleOptionInterfaceFactory;
14 use Magento\Framework\DataObject\Factory as DataObjectFactory;
15 
17 {
21  protected $objectFactory;
22 
27 
32  public function __construct(
33  DataObjectFactory $objectFactory,
34  BundleOptionInterfaceFactory $bundleOptionFactory
35  ) {
36  $this->objectFactory = $objectFactory;
37  $this->bundleOptionFactory = $bundleOptionFactory;
38  }
39 
44  {
46  $request = $this->objectFactory->create();
47 
48  $bundleOptions = $this->getBundleOptions($productOption);
49  if (!empty($bundleOptions) && is_array($bundleOptions)) {
50  $requestData = [];
51  foreach ($bundleOptions as $option) {
53  foreach ($option->getOptionSelections() as $selection) {
54  $requestData['bundle_option'][$option->getOptionId()][] = $selection;
55  $requestData['bundle_option_qty'][$option->getOptionId()] = $option->getOptionQty();
56  }
57  }
58  $request->addData($requestData);
59  }
60 
61  return $request;
62  }
63 
71  {
72  if ($productOption
73  && $productOption->getExtensionAttributes()
74  && $productOption->getExtensionAttributes()->getBundleOptions()
75  ) {
76  return $productOption->getExtensionAttributes()
77  ->getBundleOptions();
78  }
79  return [];
80  }
81 
86  {
87  $bundleOptions = $request->getBundleOption();
88  $bundleOptionsQty = $request->getBundleOptionQty();
89 
90  if (!empty($bundleOptions) && is_array($bundleOptions)) {
91  $data = [];
92  foreach ($bundleOptions as $optionId => $optionSelections) {
93  if (empty($optionSelections)) {
94  continue;
95  }
96  $optionSelections = is_array($optionSelections) ? $optionSelections : [$optionSelections];
97  $optionQty = isset($bundleOptionsQty[$optionId]) ? $bundleOptionsQty[$optionId] : 1;
98 
100  $productOption = $this->bundleOptionFactory->create();
101  $productOption->setOptionId($optionId);
102  $productOption->setOptionSelections($optionSelections);
103  $productOption->setOptionQty($optionQty);
104  $data[] = $productOption;
105  }
106 
107  return ['bundle_options' => $data];
108  }
109 
110  return [];
111  }
112 }
getBundleOptions(ProductOptionInterface $productOption)
__construct(DataObjectFactory $objectFactory, BundleOptionInterfaceFactory $bundleOptionFactory)
convertToBuyRequest(ProductOptionInterface $productOption)