在Magento 2中将产品属性类型从文本更改为下拉列表
默认情况下,在Magento 2中,后台管理没有选项可以更改创建后的产品属性类型。因此,我们必须创建自定义脚本来更改产品属性类型。以下示例代码将帮助您将产品属性类型从文本更改为Magento 2中的下拉列表。
以下是我们在以下示例中遵循的步骤,
- 将我们要更改的属性ID从文本更改为下拉列表
- 获取已存储在数据库中的给定属性Id的产品值
- 对于给定的属性Id,将先前存储的产品值作为选项插入数据库表“eav_attribute_option”和“eav_attribute_option_value”
- 使用选项Id将插入的属性选项分配给适当的产品
- 最后,使用UPDATE查询将属性类型从文本更改为数据库下拉列表
<?php use Magento\Framework\App\Bootstrap; /** * If the external file is in the root folder */ require __DIR__ . '/app/bootstrap.php'; $params = $_SERVER; $bootstrap = Bootstrap::create(BP, $params); $obj = $bootstrap->getObjectManager(); $state = $obj->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); /* * Instance of object manager */ $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); /* * Attribute Id that we want to change from text to dropdown */ $attributeId = 155; $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); /* * Get the product values that are already stored in the database for given attribute */ $catalog_product_entity_varchar = $resource->getTableName('catalog_product_entity_varchar'); $attribute_values = $connection->fetchAll("SELECT DISTINCT attribute_id, value FROM $catalog_product_entity_varchar where attribute_id = $attributeId"); if(!empty($attribute_values)) { foreach ($attribute_values as $_attribute_values) { $attribute_values = $connection->fetchRow("SELECT count(*) as cnt FROM `eav_attribute_option` where attribute_id = $attributeId;"); $count = $attribute_values['cnt'] + 1; /* * Insert previously stored product values as an option on the database for a given attribute */ $eav_attribute_option = $resource->getTableName('eav_attribute_option'); $sql = "insert into $eav_attribute_option(option_id, attribute_id, sort_order) values (null, $attributeId, $count)"; try { $resp = $connection->query($sql); } catch (Exception $e) { echo ''; print_r($e->getMessage()); } $lastInsertId = $connection->lastInsertId(); $eav_attribute_option_value = $resource->getTableName('eav_attribute_option_value'); $sql = "insert into $eav_attribute_option_value(value_id, option_id, store_id, value) values (null, $lastInsertId, 0, '$_attribute_values[value]')"; try { $resp = $connection->query($sql); } catch (Exception $e) { echo ''; print_r($e->getMessage()); } $sql = "insert into $eav_attribute_option_value(value_id, option_id, store_id, value) values (null, $lastInsertId, 1, '$_attribute_values[value]')"; try { $resp = $connection->query($sql); } catch (Exception $e) { echo ''; print_r($e->getMessage()); } } } $catalog_product_entity_varchar = $resource->getTableName('catalog_product_entity_varchar'); $attribute_values = $connection->fetchAll("SELECT * FROM $catalog_product_entity_varchar where attribute_id = $attributeId"); if(!empty($attribute_values)) { foreach ($attribute_values as $_attribute_values) { /* * Get the option id for the specific product */ $option_values = $connection->fetchRow("SELECT * FROM `eav_attribute_option` as eao INNER JOIN `eav_attribute_option_value` as eaov on eao.option_id = eaov.option_id WHERE eao.attribute_id = $attributeId and eaov.store_id = 1 and eaov.value = '$_attribute_values[value]'"); if(!empty($option_values)) { $catalog_product_entity_int = $resource->getTableName('catalog_product_entity_int'); $product_values_exist = $connection->fetchRow("SELECT value_id FROM $catalog_product_entity_int WHERE attribute_id = $attributeId and entity_id = $_attribute_values[entity_id]"); if(empty($product_values_exist)) { $sql = "insert into $catalog_product_entity_int(value_id, attribute_id, store_id, entity_id, value) values (null, $attributeId, 0, $_attribute_values[entity_id], $option_values[option_id])"; try { $resp = $connection->query($sql); } catch (Exception $e) { echo ''; print_r($e->getMessage()); } } else { $sql = "Update $catalog_product_entity_int set value = $option_values[option_id] WHERE attribute_id = $attributeId and entity_id = $_attribute_values[entity_id]"; try { $resp = $connection->query($sql); } catch (Exception $e) { echo ''; print_r($e->getMessage()); } } } } } /* * Change the attribute type from text to dropdown on database */ $eav_attribute = $resource->getTableName('eav_attribute'); $sql = "UPDATE $eav_attribute SET `backend_type` = 'int', `frontend_input` = 'select', `source_model` = 'Magento\\\Eav\\\Model\\\Entity\\\Attribute\\\Source\\\Table' WHERE `attribute_id` = $attributeId"; try { $resp = $connection->query($sql); } catch (Exception $e) { echo ''; print_r($e->getMessage()); } $catalog_eav_attribute = $resource->getTableName('catalog_eav_attribute'); $sql = "UPDATE $catalog_eav_attribute SET `is_filterable` = 1, is_comparable = 1, is_visible_on_front = 1, is_html_allowed_on_front = 1, is_filterable_in_search = 1, used_in_product_listing = 1 WHERE `attribute_id` = $attributeId"; try { $resp = $connection->query($sql); } catch (Exception $e) { echo ''; print_r($e->getMessage()); }希望这可以帮助到你。
版权声明:本站内容源自互联网,如有内容侵犯了你的权益,请联系删除相关内容。