スクリプトを使用して価格を更新しています。これまでのところ、CSV からデータを取得し、これを使用してデータベースを検索し、その行の CSV からの情報で製品を更新します。
私が抱えている問題は、CSV の SKU が Magento のものと正確に一致しないことです。たとえば、CSV1234
では、magento では、この行の情報で更新する必要がある製品が存在する可能性が001234_01
あります。001234_02
001234_03
LIKE
そのため、CSV のその行の SKU のような Magento からすべての結果を取得するための検索を追加しました。
問題は、1 つだけが更新され、次の行に移動することです。したがって、更新されますが、次の行に移動する前に001234_01
更新する必要があり001234_02
ます。001234_03
_updatePrices を微調整して foreach のようなものにする必要があると思いますが、うまくいかないようです。
$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);
}
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));
}
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 LIKE '%$sku%'";
return $connection->fetchOne($sql, array($sku));
}
function _checkIfSkuExists($sku){
$connection = _getConnection('core_read');
$sql = "SELECT * FROM " . _getTableName('catalog_product_entity') . " WHERE sku LIKE '%$sku%'";
// I think you can try $conn->fetchAll();
return $connection->fetchAll($sql, array($sku));
// print_r this - it will be an array (empty or not)
print_r($connection);
}
function _updatePrices($data){
$connection = _getConnection('core_write');
$sku = $data[0];
$newPrice = $data[4];
$specialPrice = $data[6];
$status = $data[10];
$productId = $row['entity_id'];
$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($newPrice, $attributeId, $productId));
$sql2 = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped
SET cped.value = ?
WHERE cped.attribute_id = ?
AND cped.entity_id = ?";
$connection->query($sql2, array($specialPrice, '567', $productId));
if ($status == "A") {
$sql3 = "UPDATE " . _getTableName('catalog_product_entity_int') . " cped
SET cped.value = ?
WHERE cped.attribute_id = ?
AND cped.entity_id = ?";
$connection->query($sql3, array('1', '273', $productId));
} else {
$sql4 = "UPDATE " . _getTableName('catalog_product_entity_int') . " cped
SET cped.value = ?
WHERE cped.attribute_id = ?
AND cped.entity_id = ?";
$connection->query($sql4, array('2', '273', $productId));
}
}
/***************** UTILITY FUNCTIONS ********************/
// Here comes your code after UTILITY FUNCTIONS:
$csv = new Varien_File_Csv();
$data = $csv->getData('price-update/PRDAH014.csv'); //path to csv
array_shift($data);
$message = '';
$count = 1;
foreach($data as $_data){
// insted of checking if there exists nuber of rows
// if(_checkIfSkuExists($_data[0])){
// you get rows that match your LIKE query
$skuRows = _checkIfSkuExists($_data[0]);
if(0 < sizeof($skuRows)){
// okay you have records, let's iterate through them
foreach ($skuRows as $row) {
try{
// modify your _updatePrices so as to use record ID from $row and CSV data from $data both
_updatePrices($_data, $row);
} catch(Exception $e){
// exception warning
}
}
}else{
// no records warning
}
} // foreach($data as $_data) ends