Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private function createConfigurable($data) {
- $this->logger->info('create conf');
- $custom=1;
- $ob = ObjectManager::getInstance();
- $dir = $ob->get('MagentoFrameworkAppFilesystemDirectoryList');
- $mediaImportFolder = $dir->getPath(DirectoryList::MEDIA);
- $imagePath = $mediaImportFolder."/import/asgard/"; // path of the image
- $productRepository = $ob->create(ProductRepositoryInterface::class);
- $installer = $ob->create(CategorySetup::class);
- $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
- $eavConfig = $ob->get('MagentoEavModelConfig');
- $associatedProductIds=$data['simple_ids'];
- $attributeValues = [];
- $attribute = $eavConfig->getAttribute('catalog_product', 'color');
- $options = $attribute->getOptions();
- array_shift($options); //remove the first option which is empty
- foreach ($options as $option) {
- $attributeValues[] = [
- 'label' => 'color',
- 'attribute_id' => $attribute->getId(),
- 'value_index' => $option->getValue(),
- ];
- }
- /** @var Factory $optionsFactory */
- $optionsFactory = $ob->create(Factory::class);
- $configurableAttributesData = [
- [
- 'attribute_id' => $attribute->getId(),
- 'code' => $attribute->getAttributeCode(),
- 'label' => $attribute->getStoreLabel(),
- 'position' => '1',
- 'values' => $attributeValues,
- ],
- ];
- $product = $ob->create(Product::class);
- //$product = $ob->get('MagentoCatalogModelProduct')->load($configurableId);
- $configurableOptions = $optionsFactory->create($configurableAttributesData);
- $extensionConfigurableAttributes = $product->getExtensionAttributes();
- $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
- $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
- $product->setExtensionAttributes($extensionConfigurableAttributes);
- $product->setTypeId(Configurable::TYPE_CODE)
- ->setAttributeSetId($attributeSetId)
- ->setWebsiteIds([1])
- ->setName($data['data']['name'].'- configurable')
- ->setSku($data['data']['name'].'c')
- ->setDescription($data['data']['description'])
- ->setPrice($data['data']['price'])
- ->setVisibility(Visibility::VISIBILITY_BOTH)
- ->setStatus(Status::STATUS_ENABLED)
- ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1])
- ->setHasOptions(1)
- ;
- $product=$productRepository->save($product);
- if($custom) {
- $options=array('tet1'=>1,'test3'=>2);
- $this->createCustomOptions($product,'custom option conf',$options);
- }
- // if(isset($data['data']['image'])) {
- //$this->addImage($product,$imagePath.$data['data']['image']);
- // }
- return $product->getId();
- }
- public function addImage (Product $product, $imagePath) {
- $this->_imageContentInterfaceFactory=$this->_objectManager->get('MagentoFrameworkApiDataImageContentInterfaceFactory');
- $this->_productAttributeMediaGalleryEntryInterfaceFactory=$this->_objectManager->get('MagentoCatalogApiDataProductAttributeMediaGalleryEntryInterfaceFactory');
- $this->_productAttributeMediaGalleryManagement=$this->_objectManager->get('MagentoCatalogModelProductGalleryGalleryManagement');
- $imageContent = $this->_imageContentInterfaceFactory->create()
- ->setBase64EncodedData(base64_encode(file_get_contents($imagePath)))
- ->setType($this->getMimeType($imagePath))
- ->setName($product->getSku());
- $newImage = $this->_productAttributeMediaGalleryEntryInterfaceFactory->create()
- ->setMediaType(MagentoCatalogModelProductAttributeBackendMediaImageEntryConverter::MEDIA_TYPE_CODE)
- ->setFile($imagePath)
- ->setDisabled(0)
- ->setContent($imageContent)
- ->setLabel('image')
- ->setTypes(['thumbnail', 'image', 'small_image'])
- ;
- $this->_productAttributeMediaGalleryManagement->create($product->getSku(), $newImage);
- return true;
- }
- public function getMimeType($fileName)
- {
- list($this->_imageSrcWidth, $this->_imageSrcHeight, $this->_fileType, ) = getimagesize($fileName);
- $this->_fileMimeType = image_type_to_mime_type($this->_fileType);
- return $this->_fileMimeType;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement