cancel
Showing results for 
Search instead for 
Did you mean: 

Add product attribute that will auto_increment aftersave

Add product attribute that will auto_increment aftersave

Hello , i am trying to add an attribute like product_id for products that will have auto_increment.

 

What i did so far and is not working:

 

etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Mymodule>
      <version>0.1.0</version>
    </Mymodule>
  </modules>
  <global>
    <helpers>
      <mymodule>
        <class>Mymodule_Helper</class>
      </mymodule>
    </helpers>
  <models>
                    <Mymodule>
                       <class>Mymodule_Model</class>
                    </Mymodule>
                </models>
	<resources>
	  <mymodule_setup>
		<setup>
		  <module>mymodule</module>
		</setup>
		<connection>
		  <use>core_setup</use>
		</connection>
	  </mymodule_setup>
	  <mymodule_write>
		<connection>
		  <use>core_write</use>
		</connection>
	  </mymodule_write>
	  <mymodule_read>
		<connection>
		  <use>core_read</use>
		</connection>
	  </mymodule_read>
	</resources>
  </global>
</config> 

The model:

<?php
class Mymodule_Model_Myitem extends Mage_Core_Model_Abstract {
 //this method simply adds one to the increment id an returns the new one
    public function getIncrementId(){
        return Mage::getSingleton('eav/config')
            ->getEntityType('myitem')
            ->fetchNewIncrementId();
    }

protected function _beforeSave()
{ 

    parent::_beforeSave();

    if (!$this->getIncrementId()) {
        $incrementId = Mage::getSingleton('eav/config')
            ->getEntityType('custom_sku2')
            ->fetchNewIncrementId($this->getStoreId());
        $this->setIncrementId($incrementId);
    }

}

}	 

and the installer script:

 

<?php
$install = $this;
$install->startSetup();

$install->addEntityType('mymodule', array(
    'entity_model'          => 'mymodule',
    'increment_model'       => 'eav/entity_increment_numeric',
    'increment_per_store'   => false,
));

$entityType = Mage::getModel('eav/entity_type')->loadByCode('custom_sku2');
$entityStoreConfig = Mage::getModel('eav/entity_store')->loadByEntityStore($entityType ->getId(), 12);
$entityStoreConfig->setEntityTypeId($entityType ->getId())
    ->setStoreId(0)
    ->setIncrementPrefix("MYPREFIX")
    ->setIncrementLastId(100)
    ->save();

$install->endSetup();

Then i create an attribute the normal way in Magento with the name "custom_sku2" and i add this attribute in products grid.
Even after save i cannot see this attribute auto increment, it is blank.