Skip to content

Instantly share code, notes, and snippets.

@mblarsen
Created August 15, 2016 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mblarsen/012dce6f55ad5d442f41e9157e74530e to your computer and use it in GitHub Desktop.
Save mblarsen/012dce6f55ad5d442f41e9157e74530e to your computer and use it in GitHub Desktop.
Module rewrite that enables same increment prefix for each Magento store under a website.
<!-- app/code/local/Namespace/Modulename/etc/config.xml -->
<config>
...
<global>
...
<models>
<eav>
<rewrite>
<entity_increment_numeric>Namespace_Modulename_Eav_Model_Entity_Increment_Numeric</entity_increment_numeric>
</rewrite>
</eav>
</models>
...
</global>
...
</config>
<?php
// app/code/local/Namespace/Modulename/Eav/Model/Entity/Increment/Numeric.php
/**
* Changes the next id behaviour so that all stores share the same increment
* for orders, invoces, creditmemos and shipments if they share the same
* increment prefix.
*
* 1. In eav_entity_type leave increment_model to be the default eav/entity_increment_numeric
*
* 2. Ensure that increment_per_store is 1 (default)
*
* 3. Ensure that when creating a new store you set increment_prefix to whatever it
* is for the website group in eav_entity_store. Note: First time a new index
* generated an entry for each store of website will be created. You only have
* to create them manually if stores needs to have separate prefixes.
*
* @see Mage_Eav_Model_Entity_Increment_Numeric
*/
class Namespace_Modulename_Eav_Model_Entity_Increment_Numeric extends Mage_Eav_Model_Entity_Increment_Numeric
{
public function getNextId()
{
$next = parent::getNextId();
// 5 order 6 invoice 7 creditmemo 8 shipment
if ($this->getEntityTypeId() < 5 || $this->getEntityTypeId() > 8) return $next;
// Store same increment id for all stores of same website
$storeIds = Mage::getModel('core/store')
->load($this->getStoreId())
->getWebsite()
->getStoreIds()
;
// Get prefix for store IDs used in case a config a store does not
// exists this prefix will be used.
$incrementPrefix = $this->findFirstPrefix($this->getEntityTypeId(), $storeIds);
// Set last id for configs that has a matching prefix (new configs will
// always match).
foreach ($storeIds as $storeId) {
$entityStoreConfig = $this->findOrCreate($this->getEntityTypeId(), $storeId, $incrementPrefix);
if ($this->getPrefix() == $entityStoreConfig->getIncrementPrefix()) {
$entityStoreConfig->setIncrementLastId($next);
$entityStoreConfig->save();
}
}
return $next;
}
/**
* Finds first entity store prefix with in collection of store ids.
*
* @param mixed $entityTypeId
* @param array $storeIds
* @param string $incrementDefault
*/
protected function findFirstPrefix(int $entityTypeId, array $storeIds, $incrementDefault = '1') : string
{
$incrementPrefix = $incrementDefault;
foreach ($storeIds as $storeId) {
$entityStoreConfig = Mage::getModel('eav/entity_store')
->loadByEntityStore($getEntityTypeId, $storeId)
;
if ($entityStoreConfig->getId()) {
$incrementPrefix = $entityStoreConfig->getIncrementPrefix();
break;
}
}
return $incrementPrefix;
}
/**
* Find or create (if missing) entity store for store with id.
*
* @param int $entityTypeId
* @param int $storeId
* @param string $incrementPrefix
*/
protected function findOrCreate(int $entityTypeId, int $storeId, string $incrementPrefix) : Mage_Eav_Model_Entity_Store
{
$entityStoreConfig = Mage::getModel('eav/entity_store')
->loadByEntityStore($this->getEntityTypeId(), $storeId)
;
if (!$entityStoreConfig->getId()) {
$entityStoreConfig
->setEntityTypeId($this->getEntityTypeId())
->setStoreId($storeId)
->setIncrementPrefix($incrementPrefix)
->save();
}
return $entityStoreConfig;
}
}
@wchief
Copy link

wchief commented Oct 11, 2016

Hejsa mblarsen .. kan ikke rigtigt få det til at virke (ændret namespace og modulename og lagt i de respektive steder ..men nada) :)

@EnigmaWebdesign
Copy link

I'm getting this error, Parse error: syntax error, unexpected ':', expecting ';' or '{' in /var/www/organzaold/app/code/local/EW/Numeric/Eav/Model/Entity/Increment/Numeric.php on line 53

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