1

I have created user1 and user2 only to add the product and I want to show there ID in a new column I want to add new column in catlog/Manage Product before name column.

1
  • Please share, what you have done so far ?
    – Arunendra
    Feb 26, 2016 at 12:51

2 Answers 2

2

This is my approach:

  1. I have created a table where a will save the admin user id and the product that has been saved by this admin. In this table i have created a FK on product_id with catalog_product_entity on entity_id, product_id that is saved under this table is unique.

  2. On catalog_product_save_after event I have checked that current product is saved in my database. If it is, I just call return, else(means that is created a new product) I save the admin user id and product id in my database.

  3. When you enter in product grid you will see the admin_id for each product.

if something is unclear, feel free to ask

Resources:

http://inchoo.net/magento/magento-install-install-upgrade-data-and-data-upgrade-scripts/

http://www.divisionlab.com/solvingmagento/event-driven-architecture-in-magento-observer-pattern/

https://www.atwix.com/magento/thumbnail-images-admin-product-grid/

app/etc/modules/Namespace_AdminId.xml

will contain

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_AdminId>
            <active>true</active>
            <codePool>local</codePool>
        </Namespace_AdminId>
    </modules>
</config>

next

app/code/local/Namespace/AdminId/Adminhtml/Block/Catalog/Product/Grid.php

with

<?php 
class Namespace_AdminId_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
    protected function _prepareColumns()
    {
        $this->addColumn('admin_id', array(
            'header' => Mage::helper('catalog')->__('AdminId'),
            'align' => 'left',
            'index' => 'admin_id',
            'width'     => '70',
            'renderer' => 'Namespace_AdminId_Block_Adminhtml_Template_Grid_Renderer_AdminId'
        )); 
        return parent::_prepareColumns();
    }
}

next

app/code/local/Namespace/AdminId/Block/Adminhtml/Template/Grid/Renderer/AdminId.php

with

<?php 
class Namespace_AdminId_Block_Adminhtml_Template_Grid_Renderer_AdminId extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        return $this->_getValue($row);
    } 
    protected function _getValue(Varien_Object $row)
    {       
        $userAdminId = Mage::getModel('namespace_adminid/savedby')->load($row->getId())->getAdminId();
        return $userAdminId;
    }
}

next

app/code/local/Namespace/AdminId/Model/Savedby.php

with

<?php

class Namespace_AdminId_Model_Savedby extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init('namespace_adminid/savedby');
    }
}

next

app/code/local/Namespace/AdminId/Model/Resource/Savedby.php

with

<?php

class Namespace_AdminId_Model_Resource_Savedby extends Mage_Core_Model_Resource_Db_Abstract
{
    protected function _construct()
    {
        $this->_init('namespace_adminid/savedby', 'savedby_id');
    }
}

next

app/code/local/Namespace/AdminId/Model/Resource/Savedby/Collection.php

with

<?php
class Namespace_AdminId_Model_Resource_Savedby_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    public function _construct()
    {
        $this->_init('namespace_adminid/savedby');
    }
}

next is observer

app/code/local/Namespace/AdminId/Model/Observer.php

with

<?php
class Namespace_AdminId_Model_Observer
{
     public function addProductSaver($observer)
     {
        $productId = $observer->getProduct()->getId();
        $user = Mage::getSingleton('admin/session'); 
        $userId = $user->getUser()->getUserId();
        $saveByModel = Mage::getModel('namespace_adminid/savedby');
        $collection = $saveByModel->getCollection()
                        ->addFieldToFilter('admin_id', array('eq' => $userId))
                        ->addFieldToFilter('product_id', array('eq' => $productId));

        if(count($collection)){
            return;
        }
        else{
            $data = array('admin_id' => $userId, 'product_id' => $productId);
            $saveByModel->addData($data);
            try {
                $saveByModel->save();
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        }
     }
}

next install script

app/code/local/Namespace/AdminId/sql/namespace_adminid_setup/install-1.0.0.0.php

with

<?php

$installer = $this;

$installer->startSetup();

$table = $installer->getConnection()
    ->newTable($installer->getTable('namespace_adminid/savedby'))
    ->addColumn('savedby_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'identity'  => true,
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
        ), 'Id')
    ->addColumn('admin_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'nullable'  => false,
        ), 'Admin Id')
    ->addColumn('product_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'nullable'  => false,
        ), 'Product_Id')
    ->addIndex($installer->getIdxName('namespace_adminid/savedby', array('product_id'), Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE),
        array('product_id'), array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE)
    )
    ->addForeignKey(
        $installer->getFkName('namespace_adminid/savedby', 'product_id', 'catalog/product','entity_id'),
        'product_id',
        $installer->getTable('catalog/product'), 
        'entity_id',
        Varien_Db_Ddl_Table::ACTION_CASCADE, 
        Varien_Db_Ddl_Table::ACTION_CASCADE
    ); 
$installer->getConnection()->createTable($table);

$installer->endSetup();

and...

app/code/local/Namespace/AdminId/etc/config.xml

with

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_AdminId>
            <version>1.0.0.0</version>
        </Namespace_AdminId>
    </modules>
    <adminhtml>
         <events>
            <catalog_product_save_after>
                <observers>
                    <addproductsaver>
                        <class>Namespace_AdminId_Model_Observer</class>
                        <method>addProductSaver</method>
                    </addproductsaver>
                </observers>
            </catalog_product_save_after>
        </events> 
    </adminhtml>
    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <catalog_product_grid>Namespace_AdminId_Adminhtml_Block_Catalog_Product_Grid</catalog_product_grid>
                </rewrite>
            </adminhtml>
        </blocks>
        <models>
            <namespace_adminid>
                <class>Namespace_AdminId_Model</class>
                <resourceModel>namespace_adminid_resource</resourceModel>
            </namespace_adminid>
            <namespace_adminid_resource>
                <class>Namespace_AdminId_Model_Resource</class>
                <entities>
                    <savedby>
                        <table>namespace_adminid_savedby</table>
                    </savedby>                  
                </entities>
            </namespace_adminid_resource>
        </models>
        <resources>
            <namespace_adminid_setup>
                <setup>
                    <module>Namespace_AdminId</module>
                </setup>
            </namespace_adminid_setup>
            <namespace_adminid_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </namespace_adminid_read>
            <namespace_adminid_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </namespace_adminid_write>
        </resources> 

    </global>
</config>
13
  • should i create new table in magento database if yes then tell me the name of the table, and one thing that what will be the table name
    – sam
    Feb 28, 2016 at 16:09
  • you don't need to create a table in the database...you just need to copy the code from above and everything will work. The table is created by install script 'app/code/local/Namespace/AdminId/sql/namespace_adminid_setup/install-1.0.0.0.php' Feb 28, 2016 at 16:32
  • I have provided 3 links as refferences. Take a look at first link for a better understanding the concept of install script and table creation in magento Feb 28, 2016 at 16:36
  • and please mark as answered if everything is working fine :D Feb 28, 2016 at 16:40
  • sorry its not working
    – sam
    Feb 28, 2016 at 18:34
0

There are many possible way available for your question. Below is one of possible way.

Step 1 : Create product attribute for Created By (create_by) using setup script which will store admin id

Step 2 : Rewrite/Over write app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php saveAction to add admin id in product OR catch event observer for this.

Step 3 : Rewrite/Over write app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php to join and display admin name before product name

2
  • please make a module .
    – sam
    Feb 26, 2016 at 12:37
  • @anp.wismad sorry no time :) Feb 26, 2016 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.