magento批量更新库存,价格,客户组价格bulk update Group Price

批量更新库存,价格等简单属性的字段值,magento自带的import/export就可以实现。

Group Price是没有自带功能或者免费插件可以实现批量更新的字段。(其实tier price也是)

下面是整理出可以用的脚本文件,收集整理来源与网络。

主要参考的有:

https://community.magento.com/t5/Programming-Questions/update-group-prices-from-CSV-file-programmatically/m-p/43281#

stackoverflow准备用SOAP API

API方法二

 

对SOAP不是很熟悉,还是采用更直接的PHP脚本:

<?php
define('MAGENTO', realpath(dirname(__FILE__))); 
 require_once MAGENTO . '/app/Mage.php'; 
 Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
 umask(0); 
 Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 
 $count = 0; 
 

 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 _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 _getIdFromSku($sku){
    $connection = _getConnection('core_read');
    $sql        = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
    return $connection->fetchOne($sql, array($sku));
}
 
function _updateStocks($data){
    $connection     = _getConnection('core_write');
    $sku            = $data[0];
    $newQty         = $data[1];
    $productId      = _getIdFromSku($sku);
    $attributeId    = _getAttributeId();
 
    $sql            = "UPDATE " . _getTableName('cataloginventory_stock_item') . " csi,
                       " . _getTableName('cataloginventory_stock_status') . " css
                       SET
                       csi.qty = ?,
                       csi.is_in_stock = ?,
                       css.qty = ?,
                       css.stock_status = ?
                       WHERE
                       csi.product_id = ?
                       AND csi.product_id = css.product_id";
					   
					   ;
    $isInStock      = $newQty > 0 ? 1 : 0;
    $stockStatus    = $newQty > 0 ? 1 : 0;
    $connection->query($sql, array($newQty, $isInStock, $newQty, $stockStatus, $productId));
}


function _updatePrices($data){
	
	
    $connection     = _getConnection('core_write');
    $sku            = $data[0];

	$entity_id = _getIdFromSku($sku);
	
	$newPrice1       = $data[2];
     $newPrice2       = $data[3];
     $newPrice4       = $data[4];
     $newPrice5       = $data[6];
	
	$id_newPrice1 = '4' ;
	 $id_newPrice2 = '5';
	 $id_newPrice4='6' ;
	 $id_newPrice5 = '3';
	 
	 $combind_groupid_groupprice = array(
											array($newPrice1 ,$id_eco_3pb),
											array($newPrice2,$id_newPrice2),
											array($newPrice4,$id_newPrice4),
											array($newPrice5 ,$id_newPrice5 )
											);
	
		foreach ($combind_groupid_groupprice as $groupid_groupprice){
    /*  catalog_product_entity_group_price table */
		if($groupid_groupprice[0] > 0){
		
		$sql_price_one = "INSERT INTO " . _getTableName('catalog_product_entity_group_price') . " 
		(entity_id,all_groups,customer_group_id,value,website_id)
							VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE 
					  value = ?,customer_group_id =?";
		$connection->query($sql_price_one, array($entity_id,0,$groupid_groupprice[1],$groupid_groupprice[0],0,
									$groupid_groupprice[0],$groupid_groupprice[1]));
		
		}
		else{}
		
		}
		
}

/***************** UTILITY FUNCTIONS ********************/
 
$csv                = new Varien_File_Csv();
$data               = $csv->getData(MAGENTO . '/var/import/updateGP.csv'); //path to csv
array_shift($data);
 
$message = '';
$count   = 1;
foreach($data as $_data){
    if(_checkIfSkuExists($_data[0])){
        try{
            //_updateStocks($_data);  //update qty
			
			_updatePrices($_data);
            $message .= $count . '> Success:: Qty (' . $_data[1] . ') of Sku (' . $_data[0] . ') has been updated.
<br />';
 
        }catch(Exception $e){
            $message .=  $count .'> Error:: while Upating  Qty (' . $_data[1] . ') of Sku (' . $_data[0] . ') => '.$e->getMessage().'
<br />';
        }
    }else{
        $message .=  $count .'>>> Error:: Product with Sku (' . $_data[0] . ') does\'t exist.
<br />';
    }
    $count++;
}
echo $message;

 

csv文件放在对应的path然后第一列是sku,第二列是qty,第三group pirce,注意id的对应关系。

tier price大致类似,找到对应数据库就好。

在magento论坛里的增加后面几个保存价格index特tmp的表格,我没有些,还是交给自带的index更新吧

 

mysql语句根据sku 查询magento的产品名

答案来自http://stackoverflow.com/questions/5323102/magento-products-import-from-database-using-sql-query

第一步,找出各个属性对应的attribute_code

select attribute_code, attribute_id, backend_type from eav_attribute
    where entity_type_id = (select entity_type_id from eav_entity_type where entity_type_code = 'catalog_product')
      and attribute_code in ('name', 'url_path', 'price', 'image', 'description', 'manufacturer');

一般结果为

+----------------+--------------+--------------+
| attribute_code | attribute_id | backend_type |
+----------------+--------------+--------------+
| description    |           61 | text         |
| image          |           74 | varchar      |
| manufacturer   |           70 | int          |
| name           |           60 | varchar      |
| price          |           64 | decimal      |
| url_path       |           87 | varchar      |
+----------------+--------------+--------------+

对应的name是60

第二步

select p.sku, p.entity_id, n.value name
    from catalog_product_entity p
    join catalog_product_entity_varchar n on n.entity_id = p.entity_id
  where n.attribute_id = 60 AND p.sku='001-C';

可以得出结果了,其他属性自己添加查询

再次感谢Joseph Mastey的回答