Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. private function createConfigurable($data) {
  2. $this->logger->info('create conf');
  3. $custom=1;
  4.  
  5. $ob = ObjectManager::getInstance();
  6. $dir = $ob->get('MagentoFrameworkAppFilesystemDirectoryList');
  7. $mediaImportFolder = $dir->getPath(DirectoryList::MEDIA);
  8. $imagePath = $mediaImportFolder."/import/asgard/"; // path of the image
  9.  
  10.  
  11. $productRepository = $ob->create(ProductRepositoryInterface::class);
  12. $installer = $ob->create(CategorySetup::class);
  13. $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
  14.  
  15. $eavConfig = $ob->get('MagentoEavModelConfig');
  16.  
  17. $associatedProductIds=$data['simple_ids'];
  18.  
  19. $attributeValues = [];
  20. $attribute = $eavConfig->getAttribute('catalog_product', 'color');
  21. $options = $attribute->getOptions();
  22. array_shift($options); //remove the first option which is empty
  23.  
  24. foreach ($options as $option) {
  25. $attributeValues[] = [
  26. 'label' => 'color',
  27. 'attribute_id' => $attribute->getId(),
  28. 'value_index' => $option->getValue(),
  29. ];
  30. }
  31.  
  32. /** @var Factory $optionsFactory */
  33. $optionsFactory = $ob->create(Factory::class);
  34. $configurableAttributesData = [
  35. [
  36. 'attribute_id' => $attribute->getId(),
  37. 'code' => $attribute->getAttributeCode(),
  38. 'label' => $attribute->getStoreLabel(),
  39. 'position' => '1',
  40. 'values' => $attributeValues,
  41. ],
  42. ];
  43.  
  44.  
  45. $product = $ob->create(Product::class);
  46.  
  47. //$product = $ob->get('MagentoCatalogModelProduct')->load($configurableId);
  48.  
  49. $configurableOptions = $optionsFactory->create($configurableAttributesData);
  50.  
  51. $extensionConfigurableAttributes = $product->getExtensionAttributes();
  52. $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
  53. $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
  54. $product->setExtensionAttributes($extensionConfigurableAttributes);
  55.  
  56.  
  57. $product->setTypeId(Configurable::TYPE_CODE)
  58. ->setAttributeSetId($attributeSetId)
  59. ->setWebsiteIds([1])
  60. ->setName($data['data']['name'].'- configurable')
  61. ->setSku($data['data']['name'].'c')
  62. ->setDescription($data['data']['description'])
  63. ->setPrice($data['data']['price'])
  64. ->setVisibility(Visibility::VISIBILITY_BOTH)
  65. ->setStatus(Status::STATUS_ENABLED)
  66. ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1])
  67. ->setHasOptions(1)
  68. ;
  69.  
  70. $product=$productRepository->save($product);
  71.  
  72. if($custom) {
  73. $options=array('tet1'=>1,'test3'=>2);
  74. $this->createCustomOptions($product,'custom option conf',$options);
  75. }
  76.  
  77. // if(isset($data['data']['image'])) {
  78. //$this->addImage($product,$imagePath.$data['data']['image']);
  79. // }
  80.  
  81. return $product->getId();
  82.  
  83. }
  84.  
  85.  
  86.  
  87. public function addImage (Product $product, $imagePath) {
  88.  
  89. $this->_imageContentInterfaceFactory=$this->_objectManager->get('MagentoFrameworkApiDataImageContentInterfaceFactory');
  90. $this->_productAttributeMediaGalleryEntryInterfaceFactory=$this->_objectManager->get('MagentoCatalogApiDataProductAttributeMediaGalleryEntryInterfaceFactory');
  91. $this->_productAttributeMediaGalleryManagement=$this->_objectManager->get('MagentoCatalogModelProductGalleryGalleryManagement');
  92.  
  93. $imageContent = $this->_imageContentInterfaceFactory->create()
  94. ->setBase64EncodedData(base64_encode(file_get_contents($imagePath)))
  95. ->setType($this->getMimeType($imagePath))
  96. ->setName($product->getSku());
  97.  
  98. $newImage = $this->_productAttributeMediaGalleryEntryInterfaceFactory->create()
  99. ->setMediaType(MagentoCatalogModelProductAttributeBackendMediaImageEntryConverter::MEDIA_TYPE_CODE)
  100. ->setFile($imagePath)
  101. ->setDisabled(0)
  102. ->setContent($imageContent)
  103. ->setLabel('image')
  104. ->setTypes(['thumbnail', 'image', 'small_image'])
  105. ;
  106.  
  107. $this->_productAttributeMediaGalleryManagement->create($product->getSku(), $newImage);
  108.  
  109. return true;
  110. }
  111.  
  112.  
  113. public function getMimeType($fileName)
  114. {
  115. list($this->_imageSrcWidth, $this->_imageSrcHeight, $this->_fileType, ) = getimagesize($fileName);
  116. $this->_fileMimeType = image_type_to_mime_type($this->_fileType);
  117. return $this->_fileMimeType;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement