/** * Update attribute data * * @param OptionInterface $attribute * @param array $item * @return void */ private function updateAttributeData(OptionInterface $attribute, array $item) { $values = []; foreach ($item['values'] as $value) { $option = $this->optionValueFactory->create(); $option->setValueIndex($value['value_index']); $values[] = $option; } $attribute->setData(array_replace_recursive((array) $attribute->getData(), $item)); $attribute->setValues($values); }
/** * @param \Magento\Catalog\Model\Product $product * @return \Magento\ConfigurableProduct\Api\Data\OptionInterface[] */ protected function getOptions(\Magento\Catalog\Model\Product $product) { $options = []; /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $typeInstance */ $typeInstance = $product->getTypeInstance(); $attributeCollection = $typeInstance->getConfigurableAttributes($product); /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $option */ foreach ($attributeCollection as $attribute) { $values = []; $attributeOptions = $attribute->getOptions(); if (is_array($attributeOptions)) { foreach ($attributeOptions as $option) { /** @var \Magento\ConfigurableProduct\Api\Data\OptionValueInterface $value */ $value = $this->optionValueFactory->create(); $value->setValueIndex($option['value_index']); $values[] = $value; } } $attribute->setValues($values); $options[] = $attribute; } return $options; }
/** * @param ProductInterface $product * @return OptionInterface[] */ public function load(ProductInterface $product) { $options = []; /** @var Configurable $typeInstance */ $typeInstance = $product->getTypeInstance(); $attributeCollection = $typeInstance->getConfigurableAttributeCollection($product); $this->extensionAttributesJoinProcessor->process($attributeCollection); foreach ($attributeCollection as $attribute) { $values = []; $attributeOptions = $attribute->getOptions(); if (is_array($attributeOptions)) { foreach ($attributeOptions as $option) { /** @var \Magento\ConfigurableProduct\Api\Data\OptionValueInterface $value */ $value = $this->optionValueFactory->create(); $value->setValueIndex($option['value_index']); $values[] = $value; } } $attribute->setValues($values); $options[] = $attribute; } return $options; }
/** * {@inheritdoc} */ public function getList($sku) { $options = []; $product = $this->getProduct($sku); foreach ($this->getConfigurableAttributesCollection($product) as $option) { $values = []; $prices = $option->getPrices(); if (is_array($prices)) { foreach ($prices as $price) { /** @var \Magento\ConfigurableProduct\Api\Data\OptionValueInterface $value */ $value = $this->optionValueFactory->create(); $value->setValueIndex($price['value_index'])->setPricingValue($price['pricing_value'])->setIsPercent($price['is_percent']); $values[] = $value; } } $option->setValues($values); $options[] = $option; } return $options; }