├── Controller
└── Adminhtml
│ └── Product
│ └── MassChangeattributeset.php
├── README.md
├── Ui
└── Component
│ └── MassAction
│ └── Group
│ └── Options.php
├── composer.json
├── etc
├── acl.xml
├── adminhtml
│ ├── routes.xml
│ └── system.xml
├── config.xml
└── module.xml
├── registration.php
└── view
└── adminhtml
├── layout
└── catalog_product_index.xml
└── ui_component
└── product_listing.xml
/Controller/Adminhtml/Product/MassChangeattributeset.php:
--------------------------------------------------------------------------------
1 | filter = $filter;
64 | $this->collectionFactory = $collectionFactory;
65 | $this->attributeSetFactory = $attributeSetFactory;
66 | $this->scopeConfig = $scopeConfig;
67 | $this->configurableProduct = $configurableProduct;
68 | $this->entityFactory = $entityFactory;
69 | parent::__construct($context, $productBuilder);
70 |
71 | }
72 |
73 |
74 | /**
75 | * Update product(s) status action
76 | *
77 | * @return \Magento\Backend\Model\View\Result\Redirect
78 | */
79 | public function execute()
80 | {
81 | $collection = $this->filter->getCollection($this->collectionFactory->create());
82 | $productIds = $collection->getAllIds();
83 | $storeId = (int) $this->getRequest()->getParam('store', 0);
84 | $attributeSetId = (int) $this->getRequest()->getParam('changeattributeset');
85 | try {
86 | foreach ($collection->getItems() as $product) {
87 | //echo get_class($product->getTypeInstance());die();
88 | if($this->validateConfigurable($product,$attributeSetId,$storeId)==false)
89 | {
90 | $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
91 | return $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
92 | break;
93 | }
94 | $product->setAttributeSetId($attributeSetId)->setStoreId($storeId);
95 |
96 | }
97 | $collection->save();
98 | //$product->getTypeInstance();
99 | $this->messageManager->addSuccess(__('A total of %1 record(s) have been updated.', count($productIds)));
100 | } catch (\Magento\Framework\Exception\LocalizedException $e) {
101 | $this->messageManager->addError($e->getMessage());
102 | } catch (\Exception $e) {
103 | $this->_getSession()->addException($e, __('Something went wrong while updating the product(s) atrribute set.'));
104 | }
105 |
106 | /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
107 | $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
108 | return $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
109 | }
110 | private function validateConfigurable($product,$attributeSetId,$storeId)
111 | {
112 | $type = $product->getTypeInstance();
113 | //var_dump(\Magento\Store\Model\ScopeInterface::SCOPE_STORE);die();
114 | $auto = $this->scopeConfig->getValue('catalog/configurable_value/auto_create', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
115 | if(!$type instanceof Configurable)
116 | {
117 | return true;
118 | }
119 | $attributeSet = $this->attributeSetFactory->create()->load($attributeSetId);
120 | $attributes = $this->configurableProduct->getUsedProductAttributes($product);
121 | $attributeSet->addSetInfo(
122 | $this->entityFactory->create()->setType(\Magento\Catalog\Model\Product::ENTITY)->getTypeId(),
123 | $attributes
124 | );
125 | foreach ($type->getConfigurableAttributes($product) as $configAattribute) {
126 | $attribute = $configAattribute->getProductAttribute();
127 | if (!is_null($attribute)) {
128 | //var_dump($attribute->isInSet($attributeSetId));die();
129 | if (!$attribute->isInSet($attributeSetId)) {
130 | if($auto)
131 | {
132 | $attribute->setAttributeSetId(
133 | $attributeSetId
134 | )->setAttributeGroupId(
135 | $attributeSet->getDefaultGroupId($attributeSetId)
136 | )->save();
137 | return true;
138 | }
139 | else
140 | {
141 |
142 | $this->messageManager->addError(__(
143 | 'The configurable attribute '.$attribute->getAttributeCode().' is not available in the targeted attribute set. Please create it first! Or allow create it from global config Catalog->Change Attribte Set'
144 | ));
145 | return false;
146 |
147 | }
148 |
149 | }
150 | else
151 | {
152 | return true;
153 | }
154 |
155 |
156 | }
157 | }
158 | }
159 | }
160 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #Ibnab Magento 2 Extension Change Product Attribute Set
2 | * Download
3 | * past in your folder app/code
4 | * enable extension in app/etc/config.php by adding 'Ibnab_ChangeAttributeSet' => 1,
5 | * and execute the command php bin/magento setup:upgrade
6 |
7 | with composer :
8 | =======
9 | * composer require ibnab/magento2-delete-orders:1.0.0
10 | * enable extension in app/etc/config.php by adding 'Ibnab_ChangeAttributeSet' => 1,
11 |
12 | More Free Magento 2 Extensions :
13 | =======
14 | http://www.ibnab.com/en/resource or http://store.ibnab.com
15 |
--------------------------------------------------------------------------------
/Ui/Component/MassAction/Group/Options.php:
--------------------------------------------------------------------------------
1 | collectionFactory = $collectionFactory;
77 | $this->data = $data;
78 | $this->urlBuilder = $urlBuilder;
79 | $this->product = $product;
80 | }
81 |
82 | /**
83 | * Get action options
84 | *
85 | * @return array
86 | */
87 | public function jsonSerialize()
88 | {
89 | if ($this->options === null) {
90 | $options = $this->collectionFactory->create()->setEntityTypeFilter($this->product->getEntityType()->getEntityTypeId())->toOptionArray();
91 | $this->prepareData();
92 | foreach ($options as $optionCode) {
93 | $this->options[$optionCode['value']] = [
94 | 'type' => 'customer_group_' . $optionCode['value'],
95 | 'label' => $optionCode['label'],
96 | ];
97 |
98 | if ($this->urlPath && $this->paramName) {
99 | $this->options[$optionCode['value']]['url'] = $this->urlBuilder->getUrl(
100 | $this->urlPath,
101 | [$this->paramName => $optionCode['value']]
102 | );
103 | }
104 |
105 | $this->options[$optionCode['value']] = array_merge_recursive(
106 | $this->options[$optionCode['value']],
107 | $this->additionalData
108 | );
109 | }
110 |
111 | $this->options = array_values($this->options);
112 | }
113 |
114 | return $this->options;
115 | }
116 |
117 | /**
118 | * Prepare addition data for subactions
119 | *
120 | * @return void
121 | */
122 | protected function prepareData()
123 | {
124 | foreach ($this->data as $key => $value) {
125 | switch ($key) {
126 | case 'urlPath':
127 | $this->urlPath = $value;
128 | break;
129 | case 'paramName':
130 | $this->paramName = $value;
131 | break;
132 | default:
133 | $this->additionalData[$key] = $value;
134 | break;
135 | }
136 | }
137 | }
138 | }
139 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ibnab/magento2-extension-change-attribute-set",
3 | "description": "extension for change product attribute set in magento 2",
4 | "type": "magento2-module",
5 | "version": "1.0.0",
6 | "license": [
7 | "OSL-3.0",
8 | "AFL-3.0"
9 | ],
10 | "require": {
11 | "php": "~5.5.0|~5.6.0",
12 | "magento/magento-composer-installer": "*"
13 | },
14 | "extra": {
15 | "map": [
16 | [
17 | "*",
18 | "Ibnab/ChangeAttributeSet"
19 | ]
20 | ]
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/etc/acl.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |