如果条件为真,则需要在 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;
任何帮助表示赞赏。