I am programatically updating some attributes of products through CSV import in "Advanced Dataflow Profiles". Of which, i have found the code to update quantity i.e. inventory information of a product.
Now there are some custom attributes which i need to change similarly. All these are string kind of attributes. But in dont get that in which database tables their values are stored. Can anyone modify the _updateAttribute() function so that i can update custom product attributes similarly??
I am using the following code:
<?php
class Mage_Catalog_Model_Convert_Adapter_Updateqty extends Mage_Catalog_Model_Convert_Adapter_Product {
public function saveRow( array $importData )
{
function _getConnection($type = 'core_read'){
return Mage::getSingleton('core/resource')->getConnection($type);
}
function _getTableName($tableName)
{
return Mage::getSingleton('core/resource')->getTableName($tableName);
}
function _getAttributeId($attribute_code = 'price')
{
$connection = _getConnection('core_read');
$sql = "SELECT attribute_id FROM " . _getTableName('eav_attribute') . " WHERE entity_type_id = ? AND attribute_code = ?";
$entity_type_id = _getEntityTypeId();
return $connection->fetchOne($sql, array($entity_type_id, $attribute_code));
}
function _getEntityTypeId($entity_type_code = 'catalog_product')
{
$connection = _getConnection('core_read');
$sql = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?";
return $connection->fetchOne($sql, array($entity_type_code));
}
function _checkIfSkuExists($sku)
{
$connection = _getConnection('core_read');
$sql = "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
$count = $connection->fetchOne($sql, array($sku));
if($count > 0)
{
return true;
}
else
{
return false;
}
}
function _getIdFromSku($sku)
{
$connection = _getConnection('core_read');
$sql = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
return $connection->fetchOne($sql, array($sku));
}
function _updateStocks($sku,$newQty)
{
$connection = _getConnection('core_write');
$productId = _getIdFromSku($sku);
$attributeId = _getAttributeId();
$sql = "UPDATE " . _getTableName('cataloginventory_stock_item') . " csi,
" . _getTableName('cataloginventory_stock_status') . " css
SET
csi.qty = ?,
csi.is_in_stock = ?,
css.qty = ?,
css.stock_status = ?
WHERE
csi.product_id = ?
AND csi.product_id = css.product_id";
$isInStock = $newQty > 0 ? 1 : 0;
$stockStatus = $newQty > 0 ? 1 : 0;
$connection->query($sql, array($newQty, $isInStock, $newQty, $stockStatus, $productId));
}
function _updateAttribute($sku,$newattri)
{
$connection = _getConnection('core_write');
$productId = _getIdFromSku($sku);
$attributeId = _getAttributeId();
$sql = "UPDATE `catalog_product_flat_1` tbl
SET
tbl.materials_value = ?
tbl."
}
set_time_limit(0);
ini_set('memory_limit','1024M');
$message = '';
$count = 1;
if(_checkIfSkuExists($importData['sku']))
{
try
{
_updateStocks($importData['sku'],$importData['qty']);
$message .= $count . '> Success: Qty of Sku "' . $importData['sku'] . '" has been updated.';
}
catch(Exception $e)
{
$message .= $count .'> Error: while Upating Qty "' .$importData['qty'] . '" of Sku (' . $importData['sku'] . ') => '.$e->getMessage().'';
}
}
else
{
$message .= $count .'> Error: Product with Sku "' .$importData['sku'] . '" does\'t exist.';
}
$count++;
echo $message;
}
}