/** * @return \Magento\Catalog\Api\Data\ProductAttributeInterface */ public function getAttribute() { if (!$this->attribute) { $this->attribute = $this->attributeRepository->get('media_gallery'); } return $this->attribute; }
/** * @param ProductRepositoryInterface $subject * @param callable $proceed * @param ProductInterface $product * @param bool $saveOptions * @return ProductInterface * @throws CouldNotSaveException * @throws InputException * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(ProductRepositoryInterface $subject, \Closure $proceed, ProductInterface $product, $saveOptions = false) { /** @var ProductInterface $result */ $result = $proceed($product, $saveOptions); if ($product->getTypeId() !== Configurable::TYPE_CODE) { return $result; } $extensionAttributes = $result->getExtensionAttributes(); if ($extensionAttributes === null) { return $result; } $configurableLinks = (array) $extensionAttributes->getConfigurableProductLinks(); $configurableOptions = (array) $extensionAttributes->getConfigurableProductOptions(); if (empty($configurableLinks) && empty($configurableOptions)) { return $result; } $attributeCodes = []; /** @var OptionInterface $configurableOption */ foreach ($configurableOptions as $configurableOption) { $eavAttribute = $this->productAttributeRepository->get($configurableOption->getAttributeId()); $attributeCode = $eavAttribute->getAttributeCode(); $attributeCodes[] = $attributeCode; } $this->validateProductLinks($attributeCodes, $configurableLinks); return $subject->get($result->getSku(), false, $result->getStoreId(), true); }
/** * Save attributes for configurable product * * @param ProductInterface $product * @param array $attributes * @return array */ private function saveConfigurableProductAttributes(ProductInterface $product, array $attributes) { $ids = []; /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $attribute */ foreach ($attributes as $attribute) { $eavAttribute = $this->productAttributeRepository->get($attribute->getAttributeId()); $data = $attribute->getData(); $attribute->loadByProductAndAttribute($product, $eavAttribute); $attribute->setData(array_replace_recursive($attribute->getData(), $data)); $ids[] = $this->optionRepository->save($product->getSku(), $attribute); } return $ids; }
/** * Create configurable product options * * @param array $attributesData * @return OptionInterface[] * @throws \InvalidArgumentException */ public function create(array $attributesData) { $options = []; foreach ($attributesData as $item) { $attribute = $this->attributeFactory->create(); $eavAttribute = $this->productAttributeRepository->get($item[Attribute::KEY_ATTRIBUTE_ID]); if (!$this->productType->canUseAttribute($eavAttribute)) { throw new \InvalidArgumentException('Provided attribute can not be used with configurable product.'); } $this->updateAttributeData($attribute, $item); $options[] = $attribute; } return $options; }
/** * * Creates a configurable product with child simple products * * @throws \Exception */ public function createConfigurableProduct() { if ($this->processedProducts >= $this->getCount()) { return; } $configurableAttribute = $this->attributeRepository->get(self::CONFIGURABLE_ATTRIBUTE); if (null == $configurableAttribute) { throw new \Exception("Selected configurable attribute {self::CONFIGURABLE_ATTRIBUTE} is not available"); } $childProductsData = $this->createConfigurableChildren($configurableAttribute); /** @var \Magento\Catalog\Model\Product $configurableProduct */ $configurableProduct = $this->objectManager->create('Magento\\Catalog\\Model\\Product'); $configurableProduct->setStoreId(self::DEFAULT_STORE_ID)->setTypeId('configurable')->setAttributeSetId(self::ATTRIBUTE_SET)->setName(self::NAMES_PREFIX . $this->titlesGenerator->generateProductTitle() . ' configurable')->setPrice(self::DEFAULT_PRODUCT_PRICE)->setWeight(self::DEFAULT_PRODUCT_WEIGHT)->setSku(uniqid())->setCategoriesIds([$this->getProductCategory()]); $configurableOption = $this->configurableOption; $configurableOption->setAttributeId($configurableAttribute->getAttributeId())->setLabel('Color')->setValues($childProductsData['configurable_options_values']); $extensionAttributes = $configurableProduct->getExtensionAttributes(); if (!$extensionAttributes) { $extensionAttributes = $this->productExtensionFactory->create(); } $extensionAttributes->setConfigurableProductLinks($childProductsData['child_products_ids']); $extensionAttributes->setConfigurableProductOptions([$configurableOption]); $configurableProduct->setExtensionAttributes($extensionAttributes); $this->productRepository->save($configurableProduct); $this->processedProducts++; }
/** * Prepare attribute info for variation matrix generation * * @param \Magento\ConfigurableProduct\Api\Data\OptionInterface[] $options * @return array */ private function getAttributesForMatrix($options) { $attributes = []; /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $option */ foreach ($options as $option) { $configurable = $this->objectToArray($option); /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */ $attribute = $this->attributeRepository->get($option->getAttributeId()); $attributeOptions = $attribute->getOptions() !== null ? $attribute->getOptions() : []; foreach ($attributeOptions as $attributeOption) { $configurable['options'][] = $this->objectToArray($attributeOption); } $configurable['attribute_code'] = $attribute->getAttributeCode(); $attributes[$option->getAttributeId()] = $configurable; } return $attributes; }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function save($sku, OptionInterface $option) { /** @var $configurableAttribute \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute */ $configurableAttribute = $this->configurableAttributeFactory->create(); if ($option->getId()) { /** @var Product $product */ $product = $this->getProduct($sku); $configurableAttribute->load($option->getId()); if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) { throw new NoSuchEntityException(__('Option with id "%1" not found', $option->getId())); } $configurableAttribute->addData($option->getData()); $configurableAttribute->setValues($option->getValues() !== null ? $option->getValues() : $configurableAttribute->getPrices()); try { $configurableAttribute->save(); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not update option with id "%1"', $option->getId())); } } else { $this->validateNewOptionData($option); /** @var Product $product */ $product = $this->productRepository->get($sku); $allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE]; if (!in_array($product->getTypeId(), $allowedTypes)) { throw new \InvalidArgumentException('Incompatible product type'); } $eavAttribute = $this->productAttributeRepository->get($option->getAttributeId()); $configurableAttribute->loadByProductAndAttribute($product, $eavAttribute); if ($configurableAttribute->getId()) { throw new CouldNotSaveException(__('Product already has this option')); } $configurableAttributesData = ['attribute_id' => $option->getAttributeId(), 'position' => $option->getPosition(), 'use_default' => $option->getIsUseDefault(), 'label' => $option->getLabel(), 'values' => $option->getValues()]; try { $product->setTypeId(ConfigurableType::TYPE_CODE); $product->setConfigurableAttributesData([$configurableAttributesData]); $product->setStoreId($this->storeManager->getStore(Store::ADMIN_CODE)->getId()); $product->save(); } catch (\Exception $e) { throw new CouldNotSaveException(__('An error occurred while saving option')); } $configurableAttribute = $this->configurableAttributeFactory->create(); $configurableAttribute->loadByProductAndAttribute($product, $eavAttribute); } if (!$configurableAttribute->getId()) { throw new CouldNotSaveException(__('An error occurred while saving option')); } return $configurableAttribute->getId(); }
/** * {@inheritdoc} */ public function getList($sku) { $result = []; /** @var \Magento\Catalog\Model\Product $product */ $product = $this->productRepository->get($sku); /** @var \Magento\Catalog\Api\Data\ProductAttributeInterface $galleryAttribute */ $galleryAttribute = $this->attributeRepository->get('media_gallery'); $container = new \Magento\Framework\Object(['attribute' => $galleryAttribute]); $gallery = $this->mediaGallery->loadGallery($product, $container); $productImages = $this->getMediaAttributeValues($product); foreach ($gallery as $image) { /** @var \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry */ $entry = $this->entryFactory->create(); $entry->setId($image['value_id'])->setLabel($image['label_default'])->setTypes(array_keys($productImages, $image['file']))->setDisabled($image['disabled_default'])->setPosition($image['position_default'])->setFile($image['file']); $result[] = $entry; } return $result; }