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
7 
11 use Magento\ConfigurableProduct\Model\Quote\Item\ConfigurableItemOptionValueFactory;
13 use Magento\Framework\DataObject\Factory as DataObjectFactory;
14 
16 {
20  protected $objectFactory;
21 
26 
31  public function __construct(
32  DataObjectFactory $objectFactory,
33  ConfigurableItemOptionValueFactory $itemOptionValueFactory
34  ) {
35  $this->objectFactory = $objectFactory;
36  $this->itemOptionValueFactory = $itemOptionValueFactory;
37  }
38 
43  {
45  $request = $this->objectFactory->create();
46 
47  $options = $this->getConfigurableItemOptions($productOption);
48  if (!empty($options)) {
49  $requestData = [];
50  foreach ($options as $option) {
52  $requestData['super_attribute'][$option->getOptionId()] = $option->getOptionValue();
53  }
54  $request->addData($requestData);
55  }
56 
57  return $request;
58  }
59 
67  {
68  if ($productOption
69  && $productOption->getExtensionAttributes()
70  && $productOption->getExtensionAttributes()->getConfigurableItemOptions()
71  ) {
72  return $productOption->getExtensionAttributes()
73  ->getConfigurableItemOptions();
74  }
75  return [];
76  }
77 
82  {
83  $superAttribute = $request->getSuperAttribute();
84  if (!empty($superAttribute) && is_array($superAttribute)) {
85  $data = [];
86  foreach ($superAttribute as $optionId => $optionValue) {
88  $option = $this->itemOptionValueFactory->create();
89  $option->setOptionId($optionId);
90  $option->setOptionValue($optionValue);
91  $data[] = $option;
92  }
93 
94  return ['configurable_item_options' => $data];
95  }
96 
97  return [];
98  }
99 }
__construct(DataObjectFactory $objectFactory, ConfigurableItemOptionValueFactory $itemOptionValueFactory)
getConfigurableItemOptions(ProductOptionInterface $productOption)
convertToBuyRequest(ProductOptionInterface $productOption)