/** * 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); }
/** * @covers \Magento\ConfigurableProduct\Helper\Product\Options\Loader::load */ public function testLoad() { $option = ['value_index' => 23]; $this->product->expects(static::once())->method('getTypeInstance')->willReturn($this->configurable); $attribute = $this->getMockBuilder(Attribute::class)->disableOriginalConstructor()->setMethods(['getOptions', 'setValues'])->getMock(); $attributes = [$attribute]; $iterator = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock(); $iterator->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator($attributes)); $this->configurable->expects(static::once())->method('getConfigurableAttributeCollection')->with($this->product)->willReturn($iterator); $attribute->expects(static::once())->method('getOptions')->willReturn([$option]); $optionValue = $this->getMockForAbstractClass(OptionValueInterface::class); $this->optionValueFactory->expects(static::once())->method('create')->willReturn($optionValue); $optionValue->expects(static::once())->method('setValueIndex')->with($option['value_index']); $attribute->expects(static::once())->method('setValues')->with([$optionValue]); $options = $this->loader->load($this->product); static::assertSame([$attribute], $options); }
/** * @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; }
/** * @covers \Magento\ConfigurableProduct\Helper\Product\Options\Factory::create */ public function testCreate() { $attributeId = 90; $valueIndex = 12; $item = ['attribute_id' => $attributeId, 'values' => [['value_index' => $valueIndex]]]; $data = [$item]; $attribute = $this->getMockBuilder(Attribute::class)->disableOriginalConstructor()->setMethods(['setValues', 'setData', '__wakeup'])->getMock(); $this->attributeFactory->expects(static::once())->method('create')->willReturn($attribute); $eavAttribute = $this->getMockBuilder(EavAttribute::class)->disableOriginalConstructor()->getMock(); $this->productAttributeRepository->expects(static::once())->method('get')->with($attributeId)->willReturn($eavAttribute); $this->configurable->expects(static::once())->method('canUseAttribute')->with($eavAttribute)->willReturn(true); $option = $this->getMock(OptionValueInterface::class); $option->expects(static::once())->method('setValueIndex')->with($valueIndex)->willReturnSelf(); $this->optionValueFactory->expects(static::once())->method('create')->willReturn($option); $attribute->expects(static::once())->method('setData')->with($item); $attribute->expects(static::once())->method('setValues')->with([$option]); $result = $this->factory->create($data); static::assertSame([$attribute], $result); }
/** * {@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; }