Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. <?php
  2. namespace PackageModuleSetup;
  3. use MagentoFrameworkSetupUpgradeDataInterface;
  4. use MagentoFrameworkSetupModuleContextInterface;
  5. use MagentoFrameworkSetupModuleDataSetupInterface;
  6.  
  7. class UpgradeData implements UpgradeDataInterface
  8. {
  9. const PRODUCT_GROUP = 'Product Details';
  10.  
  11. /** @var MagentoFrameworkAppState */
  12. protected $state;
  13.  
  14. /** @var MagentoCatalogModelProductAttributeRepository $attributeRepository */
  15. protected $attributeRepository;
  16.  
  17. /** @var MagentoFrameworkFilesystem */
  18. protected $filesystem;
  19.  
  20. /** @var MagentoSwatchesHelperMedia */
  21. protected $swatchHelper;
  22.  
  23. /** @var MagentoCatalogModelProductMediaConfig */
  24. protected $productMediaConfig;
  25.  
  26. /** @var MagentoFrameworkFilesystemDriverFile */
  27. protected $driverFile;
  28.  
  29. /** @var MagentoEavSetupEavSetupFactory */
  30. protected $eavSetupFactory;
  31.  
  32. public function __construct(
  33. MagentoEavSetupEavSetupFactory $eavSetupFactory,
  34. MagentoFrameworkAppState $state,
  35. MagentoCatalogModelProductAttributeRepository $attributeRepository,
  36. MagentoFrameworkFilesystem $filesystem,
  37. MagentoSwatchesHelperMedia $swatchHelper,
  38. MagentoCatalogModelProductMediaConfig $productMediaConfig,
  39. MagentoFrameworkFilesystemDriverFile $driverFile
  40. ) {
  41. $this->eavSetupFactory = $eavSetupFactory;
  42. $this->state = $state;
  43. $this->attributeRepository = $attributeRepository;
  44. $this->filesystem = $filesystem;
  45. $this->swatchHelper = $swatchHelper;
  46. $this->productMediaConfig = $productMediaConfig;
  47. $this->driverFile = $driverFile;
  48. }
  49.  
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  54. {
  55. $setup->startSetup();
  56.  
  57. if (version_compare($context->getVersion(), '1.0.0', '<')) {
  58. $attributesData = [
  59. // to add in step 2
  60. ];
  61.  
  62. $attributesOptionsData = [
  63. // to add in step 2
  64. ];
  65.  
  66. $this->addProductAttributes($attributesData, $attributesOptionsData, $setup);
  67. }
  68.  
  69. $setup->endSetup();
  70. }
  71.  
  72. /**
  73. * Add product attributes.
  74. *
  75. * @param $attributesData
  76. * @param $attributesOptionsData
  77. * @param ModuleDataSetupInterface $setup
  78. *
  79. * @throws Exception
  80. */
  81. public function addProductAttributes($attributesData, $attributesOptionsData, ModuleDataSetupInterface $setup)
  82. {
  83. // to implement in step 3
  84. }
  85. }
  86.  
  87. $attributesData = [
  88. 'fit' => [
  89. 'type' => 'int',
  90. 'label' => 'Fit',
  91. 'input' => 'select',
  92. 'backend' => 'MagentoEavModelEntityAttributeBackendArrayBackend',
  93. 'source' => 'MagentoEavModelEntityAttributeSourceTable',
  94. 'required' => false,
  95. 'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
  96. 'group' => self::PRODUCT_GROUP,
  97. 'used_in_product_listing' => true,
  98. 'visible_on_front' => true,
  99. 'user_defined' => true,
  100. 'filterable' => 2,
  101. 'filterable_in_search' => true,
  102. 'used_for_promo_rules' => true,
  103. 'is_html_allowed_on_front' => true,
  104. 'used_for_sort_by' => true,
  105. ],
  106. ];
  107. $attributesOptionsData = [
  108. 'fit' => [
  109. MagentoSwatchesModelSwatch::SWATCH_INPUT_TYPE_KEY => MagentoSwatchesModelSwatch::SWATCH_INPUT_TYPE_VISUAL,
  110. 'optionvisual' => [
  111. 'value' => [
  112. 'option_0' => [
  113. 0 => 'FITTED'
  114. ],
  115. 'option_1' => [
  116. 0 => 'RELAXED'
  117. ],
  118. 'option_2' => [
  119. 0 => 'REGULAR'
  120. ],
  121. ],
  122. ],
  123. 'swatchvisual' => [
  124. 'value' => [
  125. 'option_0' => 'Fitted.png',
  126. 'option_1' => 'Relaxed.png',
  127. 'option_2' => 'Regular.png',
  128. ],
  129. ],
  130. ]
  131. ];
  132.  
  133. /**
  134. * Add product attributes.
  135. *
  136. * @param $attributesData
  137. * @param $attributesOptionsData
  138. * @param ModuleDataSetupInterface $setup
  139. *
  140. * @throws Exception
  141. */
  142. public function addProductAttributes($attributesData, $attributesOptionsData, ModuleDataSetupInterface $setup)
  143. {
  144. try {
  145. // Make sure we don't get error "Area code is not set".
  146. $this->state->setAreaCode('admin');
  147. } catch (Exception $ignored) {}
  148. try {
  149. /** @var MagentoEavSetupEavSetup $eavSetup */
  150. $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
  151.  
  152. // Add attributes.
  153. foreach ($attributesData as $code => $attributeData) {
  154. $eavSetup->addAttribute(MagentoCatalogModelProduct::ENTITY, $code, $attributeData);
  155. }
  156.  
  157. // Add order if it doesn't exist. This is an important step to make sure everything will be created correctly.
  158. foreach ($attributesOptionsData as &$attributeOptionsData) {
  159. $order = 0;
  160. $swatchVisualFiles = isset($attributeOptionsData['optionvisual']['value'])
  161. ? $attributeOptionsData['optionvisual']['value']
  162. : [];
  163. foreach ($swatchVisualFiles as $index => $swatchVisualFile) {
  164. if (!isset($attributeOptionsData['optionvisual']['order'][$index])) {
  165. $attributeOptionsData['optionvisual']['order'][$index] = ++$order;
  166. }
  167. }
  168. }
  169.  
  170. // Prepare visual swatches files.
  171. $mediaDirectory = $this->filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
  172. $tmpMediaPath = $this->productMediaConfig->getBaseTmpMediaPath();
  173. $fullTmpMediaPath = $mediaDirectory->getAbsolutePath($tmpMediaPath);
  174. $this->driverFile->createDirectory($fullTmpMediaPath);
  175. foreach ($attributesOptionsData as &$attributeOptionsData) {
  176. $swatchVisualFiles = isset($attributeOptionsData['swatchvisual']['value'])
  177. ? $attributeOptionsData['swatchvisual']['value']
  178. : [];
  179. foreach ($swatchVisualFiles as $index => $swatchVisualFile) {
  180. $this->driverFile->copy(
  181. __DIR__ . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $swatchVisualFile,
  182. $fullTmpMediaPath . DIRECTORY_SEPARATOR . $swatchVisualFile
  183. );
  184. $newFile = $this->swatchHelper->moveImageFromTmp($swatchVisualFile);
  185. if (substr($newFile, 0, 1) == '.') {
  186. $newFile = substr($newFile, 1); // Fix generating swatch variations for files beginning with ".".
  187. }
  188. $this->swatchHelper->generateSwatchVariations($newFile);
  189. $attributeOptionsData['swatchvisual']['value'][$index] = $newFile;
  190. }
  191. }
  192.  
  193. // Add attribute options.
  194. foreach ($attributesOptionsData as $code => $attributeOptionsData) {
  195. /* @var MagentoCatalogModelResourceModelEavAttribute $attribute */
  196. $attribute = $this->attributeRepository->get($code);
  197. $attribute->addData($attributeOptionsData);
  198. $attribute->save();
  199. }
  200. } catch (Exception $ex) {
  201. throw new Exception(__('There was an error adding product attributes.'), 0, $ex);
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement