Skip to content

Instantly share code, notes, and snippets.

@MagePsycho
Created January 14, 2020 13:45
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 MagePsycho/c6726dd47ffe250d690ca76526362467 to your computer and use it in GitHub Desktop.
Save MagePsycho/c6726dd47ffe250d690ca76526362467 to your computer and use it in GitHub Desktop.
How to convert varchar type attribute to decimal/price type in Magento? - https://blog.magepsycho.com/how-to-convert-attribute-type-of-varchar-to-price-in-magento/
<?php
/**
* @author Raj KB <magepsycho@gmail.com>
* @website https://www.magepsycho.com
*
*/
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
# EDIT HERE...Note the single quotes inside the double quotes. This is necessary unless you modify the function yourself
# Note that these attribute codes are those attributes whose type is to be changed.
$_attributeCodes = array("'shipping_cost_with_tax'", "'shipping_cost_without_tax'");
function copyVarcharToDecimal(){
$connection = _getConnection('core_write');
$attributeIds = (string) implode(',', _getAttributeIds());
$entityTypeId = (int) _getEntityTypeId();
$sql = 'SELECT * FROM ' . _getTableName('catalog_product_entity_varchar') . ' WHERE attribute_id IN ('.$attributeIds.') AND entity_type_id = '.$entityTypeId;
$rows = $connection->fetchAll($sql);
$insertCount = 1;
$deleteCount = 1;
$insertOutput = '';
$deleteOutput = '';
foreach($rows as $row){
$checkIfDecimalValueExists = _checkIfDecimalValueExists($row);
if(!$checkIfDecimalValueExists){
$sql = 'INSERT INTO ' . _getTableName('catalog_product_entity_decimal') . ' (`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`)
VALUES (?,?,?,?,?)';
$price = $row['value'];
$price = trim(str_replace(',', '.', $price));
$connection->query($sql, array($row['entity_type_id'], $row['attribute_id'], $row['store_id'], $row['entity_id'], $price));
$insertOutput .= $insertCount . '> INSERTED::' . $connection->lastInsertId() . ' :: ' .$row['value'] . ' => ' . $price . '';
$insertCount++;
}
$sql = 'DELETE FROM ' . _getTableName('catalog_product_entity_varchar') . ' WHERE value_id = ?';
$connection->query($sql, $row['value_id']);
$deleteOutput .= $deleteCount . '> DELETED::'.$row['value_id'].'';
$deleteCount++;
}
echo '=================================================';
echo 'INSERTED';
echo $insertOutput;
echo '=================================================';
echo '=================================================';
echo 'DELETED';
echo $deleteOutput;
echo '=================================================';
}
function _getTableName($tableName){
return Mage::getSingleton('core/resource')->getTableName($tableName);
}
function _getConnection($type = 'core_read'){
return Mage::getSingleton('core/resource')->getConnection($type);
}
function _getAttributeIds(){
global $_attributeCodes;
$attributeCodes = (string) implode(',', $_attributeCodes);
$connection = _getConnection('core_read');
$sql = "SELECT attribute_id
FROM " . _getTableName('eav_attribute') . "
WHERE attribute_code
IN (
" . $attributeCodes . "
)";
return $connection->fetchCol($sql);
}
function _getEntityTypeId(){
$connection = _getConnection('core_read');
$sql = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = 'catalog_product'";
return $connection->fetchOne($sql);
}
function _checkIfDecimalValueExists($row){
$connection = _getConnection('core_write');
$sql = 'SELECT COUNT(*) FROM ' . _getTableName('catalog_product_entity_decimal') . ' WHERE attribute_id = ? AND entity_type_id = ? AND store_id = ? AND entity_id = ?';
$result = $connection->fetchOne($sql, array($row['attribute_id'], $row['entity_type_id'], $row['store_id'], $row['entity_id']));
return $result > 0 ? true : false;
}
#simply call as:
copyVarcharToDecimal();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment