Skip to content

Instantly share code, notes, and snippets.

@michelve
Created December 22, 2018 15:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michelve/95b9a3045982ef02da758d61d8b30750 to your computer and use it in GitHub Desktop.
Save michelve/95b9a3045982ef02da758d61d8b30750 to your computer and use it in GitHub Desktop.
Magento 2 - Update/Merge/Change Product Attributes
<?php
error_reporting(1);
ini_set('max_execution_time', 0);
use \Magento\Framework\App\Bootstrap;
require_once '/var/www/html/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$url = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
$mediaurl= $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
// Attribute Id that we need to change
$attributeId = 139;
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
// Get the product values that are already stored in the database for given attribute
$rgd_catalog_product_entity_varchar = $resource->getTableName('rgd_catalog_product_entity_varchar');
$attribute_values = $connection->fetchAll("SELECT DISTINCT attribute_id, value FROM $rgd_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 `rgd_eav_attribute_option` where attribute_id = $attributeId;");
$count = $attribute_values['cnt'] + 1;
// Insert the product values as an option for a given attribute
$rgd_eav_attribute_option = $resource->getTableName('rgd_eav_attribute_option');
$sql = "insert into $rgd_eav_attribute_option(option_id, attribute_id, sort_order) values (null, $attributeId, $count)";
try {
$resp = $connection->query($sql);
} catch (Exception $e) {
echo '<pre>'; print_r($e->getMessage());
}
$lastInsertId = $connection->lastInsertId();
$rgd_eav_attribute_option_value = $resource->getTableName('rgd_eav_attribute_option_value');
$sql = "insert into $rgd_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 '<pre>'; print_r($e->getMessage());
}
$sql = "insert into $rgd_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 '<pre>'; print_r($e->getMessage());
}
}
}
$rgd_catalog_product_entity_varchar = $resource->getTableName('rgd_catalog_product_entity_varchar');
$attribute_values = $connection->fetchAll("SELECT * FROM $rgd_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 `rgd_eav_attribute_option` as eao INNER JOIN `rgd_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 '<pre>'; 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 '<pre>'; print_r($e->getMessage());
}
}
}
}
}
// Change the attribute type to dropdown
$eav_attribute = $resource->getTableName('rgd_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 '<pre>'; print_r($e->getMessage());
}
$rgd_catalog_eav_attribute = $resource->getTableName('rgd_catalog_eav_attribute');
$sql = "UPDATE $rgd_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 '<pre>'; print_r($e->getMessage());
}
<?php
error_reporting(1);
ini_set('max_execution_time', 0);
use \Magento\Framework\App\Bootstrap;
require_once '/var/www/html/app/bootstrap.php';
$installer = $this;
$installer->startSetup();
$installer->updateAttribute('catalog_product', 'old_attribute_code', array('attribute_code' => 'new_attribute_code'));
$installer->endSetup();
<?php
// error_reporting(E_ALL);
// ini_set('display_errors', 1);
ini_set('max_execution_time', 0);
use \Magento\Framework\App\Bootstrap;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Eav\Setup\EavSetup;
require_once '/var/www/html/app/bootstrap.php';
function unique_code($limit){
return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $limit);
}
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()->addAttributeToSelect('asin')->load();
$csv_path = 'asins.csv';
$lines = explode( "\n", file_get_contents( $csv_path ) );
$headers = str_getcsv( array_shift( $lines ) );
$data = array();
$asins = array();
$identification_label = 'identification_label';
$identification_label_value = 797; //asin
foreach ( $lines as $line ) {
$csv = str_getcsv( $line );
if (count($csv) > 1) {
$sku = $csv[0];
$asin = $csv[1];
$asins[$sku] = $asin;
}
}
foreach ($collection as $product){
$name = $product->getName();
$sku = $product->getSku();
$mg_data = $product->getData();
$asin = $product->getResource()->getAttribute('asin')->getFrontend()->getValue($product);
//stripos($haystack, 'a') !== false;
if($asin == 'NO ASIN RETURNED'){
//B07KB6CXD7
// B1 +8
// $prefix = 'B9';
// $code_id = unique_code(8);
// $unique_asin = $prefix . strtoupper($code_id);
// $product->addAttributeUpdate('identification_value', $unique_asin, 1);
// $product->addAttributeUpdate('asin', $unique_asin, 1);
// echo $asin . '<br>';
}
if(strpos($asin, '-') !== false){
// //B07KB6CXD7
// // B1 +8
// $prefix = 'B8';
// $code_id = unique_code(8);
// $unique_asin = $prefix . strtoupper($code_id);
// $product->addAttributeUpdate('identification_value', $unique_asin, 1);
// $product->addAttributeUpdate('asin', $unique_asin, 1);
// echo $asin . '<br>';
}
// $product->addAttributeUpdate($identification_label, $identification_label_value, 1);
// var_dump($asins);
// if (empty($asin) && isset($asins[$sku])) {
// // echo "-- Asin missing for sku: $sku\n".'<br>';
// // echo "-- found asin: $asins[$sku]\n\n".'<br>';
// if (isset($asins[$sku])) {
// //echo $asins[$sku] . ' -- ' . $sku . '<br>';
// $product->addAttributeUpdate('identification_value', $asins[$sku], 1);
// $product->addAttributeUpdate('asin', $asins[$sku], 1);
// $product->addAttributeUpdate($identification_label, $identification_label_value, $store_id);
// }
// }
}
@michelve
Copy link
Author

Magento 2 - Update/Merge/Change Product Attributes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment