Skip to content

Instantly share code, notes, and snippets.

@herveguetin
Created June 10, 2015 06:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save herveguetin/808c2a6f2df5cb23cb4d to your computer and use it in GitHub Desktop.
Save herveguetin/808c2a6f2df5cb23cb4d to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@marianobrolio-santex
Copy link

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

@deodigital
Copy link

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