检查情况,然后更新Magento


Check if condition and then update Magento

需要在magento中更新价格表,如果条件为真,如果为假,则跳过。

问题是不知道为什么函数_checkIfSkuInStock返回1。

我设置一种产品有库存,另一种产品无库存。

下面的代码是在sku存在的情况下更新价格表,我还想添加如果产品有货,否则跳过的条件。

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 = '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));
}
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 _getIdFromSku($sku){
    $connection = _getConnection('core_read');
    $sql        = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
    return $connection->fetchOne($sql, array($sku));
}
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 _checkIfSkuInStock($sku){
    $connection = _getConnection('core_read');
    $sql        = "SELECT COUNT(*) AS count_no FROM " . _getTableName('cataloginventory_stock_item') . " WHERE is_in_stock = 1";
    $count      = $connection->fetchOne($sql, array($sku));
    if($count > 0){
        return true;
    }else{
        return false;
    }
}
function _updatePrices($data){
    $connection     = _getConnection('core_write');
    $sku            = $data[0];
    $newspecial_price       = $data[1];
    $productId      = _getIdFromSku($sku);
    $attributeId    = _getAttributeId();
    $sql = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped
                SET  cped.value = ?

               WHERE  cped.attribute_id = ?
                AND cped.entity_id = ?";
        $connection->query($sql, array($newspecial_price, $attributeId, $productId));
    }
$csv                = new Varien_File_Csv();
$data               = $csv->getData('special_price.csv'); //path to csv
array_shift($data);    
    $message = '';
$count   = 1;
foreach($data as $_data){
    if(_checkIfSkuExists($_data[0]) and _checkIfSkuInStock($_data[0]) == 0){
        try{
            _updatePrices($_data);
            $message .= $count . '> Success:: (' . $_data[1] . ') product code  (' . $_data[0] . '). <br />';
        }catch(Exception $e){
            $message .=  $count .'> Error:: While Upating  Price (' . $_data[1] . ') of Sku (' . $_data[0] . ') => '.$e->getMessage().'<br />';
        }
    }else{
        $message .=  $count .'> Error:: Product code   (' . $_data[0] . ')  not in database<br />';
    }
    $count++;
}
echo $message;

在此代码中:

function _checkIfSkuInStock($sku){
$connection = _getConnection('core_read');
$sql        = "SELECT COUNT(*) AS count_no FROM " . _getTableName('cataloginventory_stock_item') . " WHERE is_in_stock = 1";
$count      = $connection->fetchOne($sql, array($sku));
if($count > 0){
    return true;
}else{
    return false;
}

}

$connection->fetchOne($sql, array($sku));但是您的请求中没有任何准备好的字段(?)

添加如下内容:

"SELECT COUNT(*) AS count_no FROM " . _getTableName('cataloginventory_stock_item') . " WHERE is_in_stock = 1 AND product_id=?"

然后使用

$connection->fetchOne($sql, array($this->_getIdFromSku($sku)));