0
我需要SKU,數量,價格和特殊的價格來更新數據庫。如何切換欄,如果條件
我有的是:
CSV文件:
sku,qty,price
123,1,150
124,1,160
125,1,160
126,1,100
在表produdct內容我有:
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;
是的,我知道,但問題是如果我收到數據量,我可以將其分成股票和價格,但仍然需要比較價格並更新正確的價格。 – Dario
這是非常基本的,將你的導出和導入列表讀入數組,寫一些簡單的代碼來比較你需要比較和更新導入數組,然後將其打印到csv。閱讀http://www.php.net/manual/en/上的php函數 –