How to update inventory, Price using custome attribute in magento


Step 1: Create a test.csv file as you  see in above  picture in magento root directory.
Step 1: Create a test.php  file  in magento root directory.
and copy following code in this file:-
<?php
/**
 * @author     Sunil & Arun  
 * @website     smartwinzsolutions.com
 * @category    Export / Import
 */
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

set_time_limit(0);
ini_set('memory_limit','1024M');

/***************** UTILITY FUNCTIONS ********************/
function _getConnection($type = 'core_read'){
    return Mage::getSingleton('core/resource')->getConnection($type);
}

function _getTableName($tableName){
    return Mage::getSingleton('core/resource')->getTableName($tableName);
}

 /*  Get attribute of price */

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));
}

  /*  Get attribute of cost */
  
function _getAttributeIdcost($attribute_code = 'cost'){
    $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));
}

  /*  Get attribute of msrp */
  
function _getAttributeIdmsrp($attribute_code = 'msrp'){
    $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_varchar') . " WHERE value = ?";
    $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_varchar') . " WHERE value = ?";
    return $connection->fetchOne($sql, array($sku));
}

function _updateStocks($data){
    $connection     = _getConnection('core_write');
    $sku            = $data[0];
    $newQty         = $data[1];
$newcost = $data[3];
$newPrice       = $data[4];
$newmsrp = $data[5];
    $productId      = _getIdFromSku($sku);
    $attributeId    = _getAttributeId();
    $attributeIdcost    = _getAttributeIdcost();
    $attributeIdmsrp    = _getAttributeIdmsrp();

    $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));
/* Query to update Price of Products */
$sql1 = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped
                SET  cped.value = ?
            WHERE  cped.attribute_id = ?
            AND cped.entity_id = ?";
    $connection->query($sql1, array($newPrice, $attributeId, $productId));
/* Query to update Cost of Products */
$sql2 = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cpd
                SET  cpd.value = ?
            WHERE  cpd.attribute_id = ?
            AND cpd.entity_id = ?";
    $connection->query($sql2, array($newcost, $attributeIdcost, $productId));
/* Query to update msrp of Products */
$sql3 = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cpdr
                SET  cpdr.value = ?
            WHERE  cpdr.attribute_id = ?
            AND cpdr.entity_id = ?";
    $connection->query($sql3, array($newmsrp, $attributeIdmsrp, $productId));
}
/***************** UTILITY FUNCTIONS ********************/

$csv                = new Varien_File_Csv();
$data               = $csv->getData('Book12.csv'); //path to csv
array_shift($data);

$message = '';
$count   = 1;
foreach($data as $_data){
    if(_checkIfSkuExists($_data[0])){
        try{
            _updateStocks($_data);
            $message .= $count . '> Success:: Qty (' . $_data[1] . '), Cost (' . $_data[3] . '), Price (' . $_data[4] . ') and MSRP (' . $_data[5] . ') of ALT_SKU (' . $_data[0] . ') has been updated. <br />';

        }catch(Exception $e){
            $message .=  $count .'> Error:: while Upating  Qty (' . $_data[1] . '), Cost (' . $_data[3] . '), Price (' . $_data[4] . ') and MSRP (' . $_data[5] . ') of ALT_SKU (' . $_data[0] . ') => '.$e->getMessage().'<br />';
        }
    }else{
        $message .=  $count .'> Error:: Product with ALT_SKU (' . $_data[0] . ') does\'t exist.<br />';
    }
    $count++;
}
echo $message;

Note :- Now go to your browser and type in url www.magento/test.php
Enjoy coding 



No comments:

Post a Comment