Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomOptionProcessor.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Quote\Api\Data\ProductOptionExtensionFactory;
12 use Magento\Quote\Model\Quote\ProductOptionFactory;
13 
15 {
19  protected $objectFactory;
20 
25 
29  protected $extensionFactory;
30 
35 
39  private $urlBuilder;
40 
46  private $serializer;
47 
55  public function __construct(
57  \Magento\Quote\Model\Quote\ProductOptionFactory $productOptionFactory,
58  \Magento\Quote\Api\Data\ProductOptionExtensionFactory $extensionFactory,
59  \Magento\Catalog\Model\CustomOptions\CustomOptionFactory $customOptionFactory,
60  \Magento\Framework\Serialize\Serializer\Json $serializer = null
61  ) {
62  $this->objectFactory = $objectFactory;
63  $this->productOptionFactory = $productOptionFactory;
64  $this->extensionFactory = $extensionFactory;
65  $this->customOptionFactory = $customOptionFactory;
66  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
67  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
68  }
69 
74  {
75  if ($cartItem->getProductOption()
76  && $cartItem->getProductOption()->getExtensionAttributes()
77  && $cartItem->getProductOption()->getExtensionAttributes()->getCustomOptions()) {
78  $customOptions = $cartItem->getProductOption()->getExtensionAttributes()->getCustomOptions();
79  if (!empty($customOptions) && is_array($customOptions)) {
80  $requestData = [];
81  foreach ($customOptions as $option) {
82  $requestData['options'][$option->getOptionId()] = $option->getOptionValue();
83  }
84  return $this->objectFactory->create($requestData);
85  }
86  }
87  return null;
88  }
89 
94  {
95  $options = $this->getOptions($cartItem);
96  if (!empty($options) && is_array($options)) {
97  $this->updateOptionsValues($options);
98  $productOption = $cartItem->getProductOption()
99  ? $cartItem->getProductOption()
100  : $this->productOptionFactory->create();
101 
103  $extensibleAttribute = $productOption->getExtensionAttributes()
104  ? $productOption->getExtensionAttributes()
105  : $this->extensionFactory->create();
106 
107  $extensibleAttribute->setCustomOptions($options);
108  $productOption->setExtensionAttributes($extensibleAttribute);
109  $cartItem->setProductOption($productOption);
110  }
111  return $cartItem;
112  }
113 
121  {
122  $buyRequest = !empty($cartItem->getOptionByCode('info_buyRequest'))
123  ? $this->serializer->unserialize($cartItem->getOptionByCode('info_buyRequest')->getValue())
124  : null;
125  return is_array($buyRequest) && isset($buyRequest['options'])
126  ? $buyRequest['options']
127  : [];
128  }
129 
136  protected function updateOptionsValues(array &$options)
137  {
138  foreach ($options as $optionId => &$optionValue) {
140  $option = $this->customOptionFactory->create();
141  $option->setOptionId($optionId);
142  if (is_array($optionValue)) {
143  $optionValue = $this->processFileOptionValue($optionValue);
144  $optionValue = $this->processDateOptionValue($optionValue);
145  $optionValue = implode(',', $optionValue);
146  }
147  $option->setOptionValue($optionValue);
149  }
150  }
151 
158  private function processFileOptionValue(array $optionValue)
159  {
160  if (array_key_exists('url', $optionValue) &&
161  array_key_exists('route', $optionValue['url']) &&
162  array_key_exists('params', $optionValue['url'])
163  ) {
164  $optionValue['url'] = $this->getUrlBuilder()->getUrl(
165  $optionValue['url']['route'],
166  $optionValue['url']['params']
167  );
168  }
169  return $optionValue;
170  }
171 
178  private function processDateOptionValue(array $optionValue)
179  {
180  if (array_key_exists('date_internal', $optionValue)
181  ) {
182  $closure = function ($key) {
183  return $key === 'date_internal';
184  };
185  $optionValue = array_filter($optionValue, $closure, ARRAY_FILTER_USE_KEY);
186  }
187  return $optionValue;
188  }
189 
195  private function getUrlBuilder()
196  {
197  if ($this->urlBuilder === null) {
199  ->get(\Magento\Catalog\Model\Product\Option\UrlBuilder::class);
200  }
201  return $this->urlBuilder;
202  }
203 }
__construct(\Magento\Framework\DataObject\Factory $objectFactory, \Magento\Quote\Model\Quote\ProductOptionFactory $productOptionFactory, \Magento\Quote\Api\Data\ProductOptionExtensionFactory $extensionFactory, \Magento\Catalog\Model\CustomOptions\CustomOptionFactory $customOptionFactory, \Magento\Framework\Serialize\Serializer\Json $serializer=null)
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest