快速更新MAGENT产品价格



快速更新MAGENT产品价格

用后台的magento上传功能, 只是要更新价格而已, 是不是只有几百条或几千条数据要更新几百年

google后, 找到一个更新价格的方面比较快的方法是, 直接操作sql

 

  1. <?php
  2. /**
  3.  * @author      MagePsycho <info@magepsycho.com>
  4.  * @website     http://www.magepsycho.com
  5.  * @category    Export / Import
  6.  */
  7. $mageFilename = ’app/Mage.php’;
  8. require_once $mageFilename;
  9. Mage::setIsDeveloperMode(true);
  10. ini_set(‘display_errors’, 1);
  11. umask(0);
  12. Mage::app(‘admin’);
  13. Mage::register(‘isSecureArea’, 1);
  14. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  15. set_time_limit(0);
  16. ini_set(‘memory_limit’,’1024M’);
  17. /***************** UTILITY FUNCTIONS ********************/
  18. function _getConnection($type = ’core_read’){
  19.     return Mage::getSingleton(‘core/resource’)->getConnection($type);
  20. }
  21. function _getTableName($tableName){
  22.     return Mage::getSingleton(‘core/resource’)->getTableName($tableName);
  23. }
  24. function _getAttributeId($attribute_code = ’price’){
  25.     $connection = _getConnection(‘core_read’);
  26.     $sql = ”SELECT attribute_id
  27.                 FROM ” . _getTableName(‘eav_attribute’) . ”
  28.             WHERE
  29.                 entity_type_id = ?
  30.                 AND attribute_code = ?”;
  31.     $entity_type_id = _getEntityTypeId();
  32.     return $connection->fetchOne($sql, array($entity_type_id, $attribute_code));
  33. }
  34. function _getEntityTypeId($entity_type_code = ’catalog_product’){
  35.     $connection = _getConnection(‘core_read’);
  36.     $sql        = ”SELECT entity_type_id FROM ” . _getTableName(‘eav_entity_type’) . ” WHERE entity_type_code = ?”;
  37.     return $connection->fetchOne($sql, array($entity_type_code));
  38. }
  39. function _getIdFromSku($sku){
  40.     $connection = _getConnection(‘core_read’);
  41.     $sql        = ”SELECT entity_id FROM ” . _getTableName(‘catalog_product_entity’) . ” WHERE sku = ?”;
  42.     return $connection->fetchOne($sql, array($sku));
  43. }
  44. function _checkIfSkuExists($sku){
  45.     $connection = _getConnection(‘core_read’);
  46.     $sql        = ”SELECT COUNT(*) AS count_no FROM ” . _getTableName(‘catalog_product_entity’) . ” WHERE sku = ?”;
  47.     $count      = $connection->fetchOne($sql, array($sku));
  48.     if($count > 0){
  49.         return true;
  50.     }else{
  51.         return false;
  52.     }
  53. }
  54. function _updatePrices($data){
  55.     $connection     = _getConnection(‘core_write’);
  56.     $sku            = $data[0];
  57.     $newPrice       = $data[1];
  58.     $productId      = _getIdFromSku($sku);
  59.     $attributeId    = _getAttributeId();
  60.     $sql = ”UPDATE ” . _getTableName(‘catalog_product_entity_decimal’) . ” cped
  61.                 SET  cped.value = ?
  62.             WHERE  cped.attribute_id = ?
  63.             AND cped.entity_id = ?”;
  64.     $connection->query($sql, array($newPrice, $attributeId, $productId));
  65. }
  66. /***************** UTILITY FUNCTIONS ********************/
  67. $csv                = new Varien_File_Csv();
  68. $data               = $csv->getData(‘prices.csv’); //path to csv
  69. array_shift($data);
  70. $message = ”;
  71. $count   = 1;
  72. foreach($data as $_data){
  73.     if(_checkIfSkuExists($_data[0])){
  74.         try{
  75.             _updatePrices($_data);
  76.             $message .= $count . ’> Success:: While Updating Price (‘ . $_data[1] . ’) of Sku (‘ . $_data[0] . ’). <br />’;
  77.         }catch(Exception $e){
  78.             $message .=  $count .’> Error:: While Upating  Price (‘ . $_data[1] . ’) of Sku (‘ . $_data[0] . ’) => ’.$e->getMessage().’<br />’;
  79.         }
  80.     }else{
  81.         $message .=  $count .’> Error:: Product with Sku (‘ . $_data[0] . ’) does\’t exist.<br />’;
  82.     }
  83.     $count++;
  84. }
  85. echo $message;

 

上传的csv格式如图: