Hi Guys!!! After a long time I am back on posting. While working on a project I came with a problem of updating customer group pricing in magento using CSV. Well not a big problem, let first discuss the logic. There are two tables in magento updates when you add customer group price:
1) catalog_product_index_group_price
2) catalog_product_entity_group_price
So here is the code
You can use below CSV format
This is the format. Hope this code will help you.
1) catalog_product_index_group_price
2) catalog_product_entity_group_price
So here is the code
<?php
//Upload File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
//readfile($_FILES['filename']['tmp_name']);
}
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$i=0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($i>0) {
$si = Mage::getModel('catalog/product')->loadByAttribute('sku',$data[0]);
$product = Mage::getModel ('catalog/product')->load($si->getId());
$group_prices = $product->getData('group_price');
if (is_null($group_prices)) {
$attribute = $product->getResource()->getAttribute('group_price');
if ($attribute) {
$attribute->getBackend()->afterLoad($product);
$group_prices = $product->getData('group_price');
}
}
if(!is_array($group_prices)){
$group_prices = array();
}
$new_price = array(array ('website_id'=>0,
'cust_group'=>$data[1],
'price'=>$data[2]));
$group_prices = array_merge($group_prices, $new_price);
try{
// First Delete all the group prices of product
$product->setData('group_price',array());
$product->save();
// Again save the old prices and new prices
$product->setData('group_price', $group_prices);
$product->save();
$outputStr[] = 'Sku ' . $data[0] . 'updated successfully.<br/>';
} catch(Exception $e) {
echo $e->getMessage();
}
}
$i++;
}
fclose($handle);
//view upload form
}
?>
You can use below CSV format
Sku | Customer Group Id | Price
----------------------------------------------------
12 | 1 | 234
This is the format. Hope this code will help you.
Comments
Post a Comment