Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace PackageModuleSetup;
- use MagentoFrameworkSetupUpgradeDataInterface;
- use MagentoFrameworkSetupModuleContextInterface;
- use MagentoFrameworkSetupModuleDataSetupInterface;
- class UpgradeData implements UpgradeDataInterface
- {
- const PRODUCT_GROUP = 'Product Details';
- /** @var MagentoFrameworkAppState */
- protected $state;
- /** @var MagentoCatalogModelProductAttributeRepository $attributeRepository */
- protected $attributeRepository;
- /** @var MagentoFrameworkFilesystem */
- protected $filesystem;
- /** @var MagentoSwatchesHelperMedia */
- protected $swatchHelper;
- /** @var MagentoCatalogModelProductMediaConfig */
- protected $productMediaConfig;
- /** @var MagentoFrameworkFilesystemDriverFile */
- protected $driverFile;
- /** @var MagentoEavSetupEavSetupFactory */
- protected $eavSetupFactory;
- public function __construct(
- MagentoEavSetupEavSetupFactory $eavSetupFactory,
- MagentoFrameworkAppState $state,
- MagentoCatalogModelProductAttributeRepository $attributeRepository,
- MagentoFrameworkFilesystem $filesystem,
- MagentoSwatchesHelperMedia $swatchHelper,
- MagentoCatalogModelProductMediaConfig $productMediaConfig,
- MagentoFrameworkFilesystemDriverFile $driverFile
- ) {
- $this->eavSetupFactory = $eavSetupFactory;
- $this->state = $state;
- $this->attributeRepository = $attributeRepository;
- $this->filesystem = $filesystem;
- $this->swatchHelper = $swatchHelper;
- $this->productMediaConfig = $productMediaConfig;
- $this->driverFile = $driverFile;
- }
- /**
- * {@inheritdoc}
- */
- public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
- {
- $setup->startSetup();
- if (version_compare($context->getVersion(), '1.0.0', '<')) {
- $attributesData = [
- // to add in step 2
- ];
- $attributesOptionsData = [
- // to add in step 2
- ];
- $this->addProductAttributes($attributesData, $attributesOptionsData, $setup);
- }
- $setup->endSetup();
- }
- /**
- * Add product attributes.
- *
- * @param $attributesData
- * @param $attributesOptionsData
- * @param ModuleDataSetupInterface $setup
- *
- * @throws Exception
- */
- public function addProductAttributes($attributesData, $attributesOptionsData, ModuleDataSetupInterface $setup)
- {
- // to implement in step 3
- }
- }
- $attributesData = [
- 'fit' => [
- 'type' => 'int',
- 'label' => 'Fit',
- 'input' => 'select',
- 'backend' => 'MagentoEavModelEntityAttributeBackendArrayBackend',
- 'source' => 'MagentoEavModelEntityAttributeSourceTable',
- 'required' => false,
- 'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
- 'group' => self::PRODUCT_GROUP,
- 'used_in_product_listing' => true,
- 'visible_on_front' => true,
- 'user_defined' => true,
- 'filterable' => 2,
- 'filterable_in_search' => true,
- 'used_for_promo_rules' => true,
- 'is_html_allowed_on_front' => true,
- 'used_for_sort_by' => true,
- ],
- ];
- $attributesOptionsData = [
- 'fit' => [
- MagentoSwatchesModelSwatch::SWATCH_INPUT_TYPE_KEY => MagentoSwatchesModelSwatch::SWATCH_INPUT_TYPE_VISUAL,
- 'optionvisual' => [
- 'value' => [
- 'option_0' => [
- 0 => 'FITTED'
- ],
- 'option_1' => [
- 0 => 'RELAXED'
- ],
- 'option_2' => [
- 0 => 'REGULAR'
- ],
- ],
- ],
- 'swatchvisual' => [
- 'value' => [
- 'option_0' => 'Fitted.png',
- 'option_1' => 'Relaxed.png',
- 'option_2' => 'Regular.png',
- ],
- ],
- ]
- ];
- /**
- * Add product attributes.
- *
- * @param $attributesData
- * @param $attributesOptionsData
- * @param ModuleDataSetupInterface $setup
- *
- * @throws Exception
- */
- public function addProductAttributes($attributesData, $attributesOptionsData, ModuleDataSetupInterface $setup)
- {
- try {
- // Make sure we don't get error "Area code is not set".
- $this->state->setAreaCode('admin');
- } catch (Exception $ignored) {}
- try {
- /** @var MagentoEavSetupEavSetup $eavSetup */
- $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
- // Add attributes.
- foreach ($attributesData as $code => $attributeData) {
- $eavSetup->addAttribute(MagentoCatalogModelProduct::ENTITY, $code, $attributeData);
- }
- // Add order if it doesn't exist. This is an important step to make sure everything will be created correctly.
- foreach ($attributesOptionsData as &$attributeOptionsData) {
- $order = 0;
- $swatchVisualFiles = isset($attributeOptionsData['optionvisual']['value'])
- ? $attributeOptionsData['optionvisual']['value']
- : [];
- foreach ($swatchVisualFiles as $index => $swatchVisualFile) {
- if (!isset($attributeOptionsData['optionvisual']['order'][$index])) {
- $attributeOptionsData['optionvisual']['order'][$index] = ++$order;
- }
- }
- }
- // Prepare visual swatches files.
- $mediaDirectory = $this->filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
- $tmpMediaPath = $this->productMediaConfig->getBaseTmpMediaPath();
- $fullTmpMediaPath = $mediaDirectory->getAbsolutePath($tmpMediaPath);
- $this->driverFile->createDirectory($fullTmpMediaPath);
- foreach ($attributesOptionsData as &$attributeOptionsData) {
- $swatchVisualFiles = isset($attributeOptionsData['swatchvisual']['value'])
- ? $attributeOptionsData['swatchvisual']['value']
- : [];
- foreach ($swatchVisualFiles as $index => $swatchVisualFile) {
- $this->driverFile->copy(
- __DIR__ . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $swatchVisualFile,
- $fullTmpMediaPath . DIRECTORY_SEPARATOR . $swatchVisualFile
- );
- $newFile = $this->swatchHelper->moveImageFromTmp($swatchVisualFile);
- if (substr($newFile, 0, 1) == '.') {
- $newFile = substr($newFile, 1); // Fix generating swatch variations for files beginning with ".".
- }
- $this->swatchHelper->generateSwatchVariations($newFile);
- $attributeOptionsData['swatchvisual']['value'][$index] = $newFile;
- }
- }
- // Add attribute options.
- foreach ($attributesOptionsData as $code => $attributeOptionsData) {
- /* @var MagentoCatalogModelResourceModelEavAttribute $attribute */
- $attribute = $this->attributeRepository->get($code);
- $attribute->addData($attributeOptionsData);
- $attribute->save();
- }
- } catch (Exception $ex) {
- throw new Exception(__('There was an error adding product attributes.'), 0, $ex);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement