Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create/update group prices programmatically in Magento
<?php
class Namespace_Module_Model_Price_Import extends Mage_Core_Model_Abstract
{
/**
* DB Connection
*
* @var Varien_Db_Adapter_Interface
*/
protected $_conn;
/**
* Table to update
*
* @var string
*/
protected $_groupPriceTable;
/**
* Group price config ready to be imported
*
* @var array
*/
protected $_dataToImport = array(
array(
'entity_id' => PRODUCT_ID,
'all_groups' => 0,
'customer_group_id' => CUSTOMER_GROUP_ID_FOR_GROUP_PRICE,
'value' => PRICE_FOR_GROUP,
'website_id' => Mage_Core_Model_App::ADMIN_STORE_ID
),
array(
'entity_id' => PRODUCT_ID,
'all_groups' => 0,
'customer_group_id' => CUSTOMER_GROUP_ID_FOR_GROUP_PRICE,
'value' => PRICE_FOR_GROUP,
'website_id' => Mage_Core_Model_App::ADMIN_STORE_ID
),
// etc...
);
public function __construct()
{
$model = Mage::getModel('catalog/product');
$this->_conn = $model->getCollection()->getConnection();
$this->_groupPriceTable = $model->getResource()->getTable('catalog/product_attribute_group_price');
}
/**
* Process to prices creation
*
* @return Namespace_Module_Model_Price_Import
*/
public function process()
{
$this->_saveToDb();
return $this;
}
/**
* Save group prices to DB
*
* @return Namespace_Module_Model_Price_Import
*/
protected function _saveToDb()
{
$i = 0;
$savedItems = array();
foreach ($this->_dataToImport as $item) {
$i++;
$savedItems[] = $item;
// Save to DB every 1000 items
if ($i % 1000 == 0) {
$this->_saveItems($savedItems);
$savedItems = array();
}
}
// Save the remaining items to DB (the one that failed on the modulo check above)
$this->_saveItems($savedItems);
return $this;
}
/**
* Save items to DB
*
* @param array $savedItems
* @return Namespace_Module_Model_Price_Import
*/
protected function _saveItems($savedItems)
{
// Insert in 'catalog_product_entity_group_price'
$this->_conn->insertOnDuplicate($this->_groupPriceTable, $savedItems);
return $this;
}
}
?>
@herveguetin

This comment has been minimized.

Copy link
Owner Author

@herveguetin herveguetin commented Jun 10, 2015

Just call process() from your code.
Feel free to create your own logic to populate $_dataToImport.

@marianobrolio-santex

This comment has been minimized.

Copy link

@marianobrolio-santex marianobrolio-santex commented Jan 31, 2016

You sir, just saved my day. I was having lots of headaches updating group prices.
Thanks for the code!

@deodigital

This comment has been minimized.

Copy link

@deodigital deodigital commented Aug 14, 2016

Hi Herve, do you have any more guidance on usage? I have created an array following the template, and changed line 24 to

protected $_dataToImport = $myarray;

$myarray is an array of arrays which follow your template. But when I run it I get this...

Parse error: syntax error, unexpected T_VARIABLE in /myfile.php on line 280

It's magento 1.9 if that helps?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment