如果条件如何切换列

时间:2013-05-17 13:28:52

标签: magento

我需要用sku,数量,价格和特价更新数据库。

我拥有的是:

CSV文件:

sku,qty,price
123,1,150
124,1,160
125,1,160
126,1,100

在表格产品内容中我有:

sku  qty  price  special_price
123   1   150     120
124   1   160     110
125   1   160     110
126   1   100     null

很好,在我的情况下我需要比较

if special_price is >= than price,

if true update price 

else update special_price.

我的PHP脚本是下一个:

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

// Update Special Price
function _getPrice($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));
}

// Update Price
function _getSpecialPrice($attribute_code = 'special_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 Entity TypeID
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));
}
// Get ID from SKU
function _getIdFromSku($sku){
    $connection = _getConnection('core_read');
    $sql        = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
    return $connection->fetchOne($sql, array($sku));

}
// Check if SKU exist in DB
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;
    }
}

// Switch price method if special_price is = or > price.
function _checkPrice($sku) {
    $connection = _getConnection('core_read');
    $sql        = "SELECT COUNT(*) AS count_no FROM " . _getTableName('eav_attribute') . " WHERE special_price >=" . $data[2];
    $count      = $connection->fetchOne($sql, array(_getIdFromSku($sku)));
    if($count > 0){
        return true;
    }else{
        return false;
    }
}

function _updatePrice($data){
    $connection     = _getConnection('core_write');
    $sku            = $data[0];
    $qty       = $data[1];
    $price       = $data[2];
    $productId      = _getIdFromSku($sku);
    $attributeId    = _getPrice();

    $sql = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped
                SET  cped.value = ?
            WHERE  cped.attribute_id = ?
            AND cped.entity_id = ?";
    $connection->query($sql, array($qty, $price, $attributeId, $productId));
}

function _updateSpecialPrice($data){
    $connection     = _getConnection('core_write');
    $sku            = $data[0];
    $qty       = $data[1];
    $price       = $data[2];
    $productId      = _getIdFromSku($sku);
    $attributeId    = _getSpecialPrice();

    $sql = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped
                SET  cped.value = ?
            WHERE  cped.attribute_id = ?
            AND cped.entity_id = ?";
    $connection->query($sql, array($qty, $price, $attributeId, $productId));
}
/***************** UTILITY FUNCTIONS ********************/

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

$message = '';
$count   = 1;
foreach($data as $_data){
    if(_checkIfSkuExists($_data[0]) and _checkPrice == TRUE){
        try{
            _updatePrice($_data);
            $message .= $count . '  Success:: Updated:  ' . $_data[1] . 'Pret:  ' . $_data[2] . '   Sku:    ' . $_data[0] . '<br />';

        }catch(Exception $e){
            $message .=  $count .'> Error:: ' . $_data[1] . '   AND ' . $_data[2] . '   of Sku  ' . $_data[0] . '   => '.$e->getMessage().'<br />';
        }
    }elseif(_checkIfSkuExists($_data[0]) and _checkPrice == FALSE){
        try{
            _updateSpecialPrice($_data);
            $message .= $count . '  Updated:    ' . $_data[1] . 'Pret:  ' . $_data[2] . '   Sku:    ' . $_data[0] . '<br />';

        }catch(Exception $e){
            $message .=  $count .'> Error:: ' . $_data[1] . '   AND ' . $_data[2] . '   of Sku  ' . $_data[0] . '   => '.$e->getMessage().'<br />';
        }else{
        $message .=  $count .'  Error:: Product with sku    ' . $_data[0] . '   doesnt exist<br />';
    }
    }
    $count++;
}
echo $message;

1 个答案:

答案 0 :(得分:0)

为什么不直接使用magento的导入功能?如果您需要比较旧价格/特价与新价格,只需提取产品清单并制作一个小脚本即可创建更新的导入csv。