0

I'm doing a rewrite of: Mage_Adminhtml_Block_Catalog_Product_Grid

I already done most of the job on my custom module. The function getStoreRestrictions() returns an array of all available stores view ids to that user

ex: user No.1 has stores 1, 2, 4

User No.2 has store 3

User No.3 has all stores

The problem I'm having is on the _prepareCollection() on the edit column. the idea is that when the user clicks edit they can only edit the parameters of the storeview instead of the global parameters of the product. I was able to modify the storeswitcher and works fine. now my problem is when the catalog is loaded the first time and there is no $this->getRequest()->getParam('store') because that parameter is empty. I'm trying to make to where when they go to the product catalog they can see the products for the first storeview they have allowed unless they change that using the storeswitcher.

<?php

class Namespace_Module_Block_Rewrite_AdminCatalogProductGrid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
protected function _getStore()
 {
    $storeId = (int) $this->getRequest()->getParam('store', 0);
    $storeIds = Namespace_Module_Model_Module::getStoreRestrictions();
    if(!empty($storeIds) && $storeId == 0){
        return Mage::app()->getStore($storeIds[0]);
    }else{
        return Mage::app()->getStore($storeId);
    }
 }

 protected function _prepareCollection()
{
    $store = $this->_getStore();
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('sku')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('attribute_set_id')
        ->addAttributeToSelect('type_id');

    if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
        $collection->joinField('qty',
            'cataloginventory/stock_item',
            'qty',
            'product_id=entity_id',
            '{{table}}.stock_id=1',
            'left');
    }
    if ($store->getId()) {
        //$collection->setStoreId($store->getId());
        $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
        $collection->addStoreFilter($store);
        $collection->joinAttribute(
            'name',
            'catalog_product/name',
            'entity_id',
            null,
            'inner',
            $adminStore
        );
        $collection->joinAttribute(
            'custom_name',
            'catalog_product/name',
            'entity_id',
            null,
            'inner',
            $store->getId()
        );
        $collection->joinAttribute(
            'status',
            'catalog_product/status',
            'entity_id',
            null,
            'inner',
            $store->getId()
        );
        $collection->joinAttribute(
            'visibility',
            'catalog_product/visibility',
            'entity_id',
            null,
            'inner',
            $store->getId()
        );
        $collection->joinAttribute(
            'price',
            'catalog_product/price',
            'entity_id',
            null,
            'left',
            $store->getId()
        );
    }
    else {
        $collection->addAttributeToSelect('price');
        $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
        $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
    }

    $this->setCollection($collection);

    parent::_prepareCollection();
    $this->getCollection()->addWebsiteNamesToResult();
    return $this;
}

 protected function _prepareColumns()
{
    $this->addColumn('entity_id',
        array(
            'header'=> Mage::helper('catalog')->__('ID'),
            'width' => '50px',
            'type'  => 'number',
            'index' => 'entity_id',
    ));
    $this->addColumn('name',
        array(
            'header'=> Mage::helper('catalog')->__('Name'),
            'index' => 'name',
    ));

    $store = $this->_getStore();
    if ($store->getId()) {
        $this->addColumn('custom_name',
            array(
                'header'=> Mage::helper('catalog')->__('Name in %s', $store->getName()),
                'index' => 'custom_name',
        ));
    }

    $this->addColumn('type',
        array(
            'header'=> Mage::helper('catalog')->__('Type'),
            'width' => '60px',
            'index' => 'type_id',
            'type'  => 'options',
            'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
    ));

    $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
        ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
        ->load()
        ->toOptionHash();

    $this->addColumn('set_name',
        array(
            'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
            'width' => '100px',
            'index' => 'attribute_set_id',
            'type'  => 'options',
            'options' => $sets,
    ));

    $this->addColumn('sku',
        array(
            'header'=> Mage::helper('catalog')->__('SKU'),
            'width' => '80px',
            'index' => 'sku',
    ));

    $store = $this->_getStore();
    $this->addColumn('price',
        array(
            'header'=> Mage::helper('catalog')->__('Price'),
            'type'  => 'price',
            'currency_code' => $store->getBaseCurrency()->getCode(),
            'index' => 'price',
    ));

    if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
        $this->addColumn('qty',
            array(
                'header'=> Mage::helper('catalog')->__('Qty'),
                'width' => '100px',
                'type'  => 'number',
                'index' => 'qty',
        ));
    }

    $this->addColumn('visibility',
        array(
            'header'=> Mage::helper('catalog')->__('Visibility'),
            'width' => '70px',
            'index' => 'visibility',
            'type'  => 'options',
            'options' => Mage::getModel('catalog/product_visibility')->getOptionArray(),
    ));

    $this->addColumn('status',
        array(
            'header'=> Mage::helper('catalog')->__('Status'),
            'width' => '70px',
            'index' => 'status',
            'type'  => 'options',
            'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(),
    ));

    if (!Mage::app()->isSingleStoreMode()) {
        $this->addColumn('websites',
            array(
                'header'=> Mage::helper('catalog')->__('Websites'),
                'width' => '100px',
                'sortable'  => false,
                'index'     => 'websites',
                'type'      => 'options',
                'options'   => Mage::getModel('core/website')->getCollection()->toOptionHash(),
        ));
    }

    if ($store->getId()) {
    echo "testing";
    $this->addColumn('action',
        array(
            'header'    => Mage::helper('catalog')->__('Action'),
            'width'     => '50px',
            'type'      => 'action',
            'getter'     => 'getId',
            'actions'   => array(
                array(
                    'caption' => Mage::helper('catalog')->__('Edit'),
                    'url'     => array(
                        'base'=>'*/*/edit',
                        'params'=>array('store'=>$store->getId())
                    ),
                    'field'   => 'id'
                )
            ),
            'filter'    => false,
            'sortable'  => false,
            'index'     => 'stores',
    ));
    }else{
    $this->addColumn('action',
        array(
            'header'    => Mage::helper('catalog')->__('Action'),
            'width'     => '50px',
            'type'      => 'action',
            'getter'     => 'getId',
            'actions'   => array(
                array(
                    'caption' => Mage::helper('catalog')->__('Edit'),
                    'url'     => array(
                        'base'=>'*/*/edit',
                        'params'=>array('store'=>$this->getRequest()->getParam('store'))
                    ),
                    'field'   => 'id'
                )
            ),
            'filter'    => false,
            'sortable'  => false,
            'index'     => 'stores',
    ));
    }

    if (Mage::helper('catalog')->isModuleEnabled('Mage_Rss')) {
        $this->addRssList('rss/catalog/notifystock', Mage::helper('catalog')->__('Notify Low Stock RSS'));
    }

    return parent::_prepareColumns();
}
}
3
  • And where is your problem? Commented Jun 14, 2016 at 8:12
  • the rewrite works, but I don't know how to make work the part I say about click edit and edit only the product storeview they are allowed. also I don't have any idea how to do the store switcher to work.
    – juanmrad
    Commented Jun 14, 2016 at 14:13
  • Code modified to explain better. please check and let me know if makes more sense and if you can help.
    – juanmrad
    Commented Jun 15, 2016 at 9:21

1 Answer 1

0

I solved it.

the problem was that I was missing a function overwrite:

public function getRowUrl($row)
{
    $store = $this->_getStore();
    if($store->getId()){
    return $this->getUrl('*/*/edit', array(
        'store'=>$store->getId(),
        'id'=>$row->getId())
    );
    }else{
    return $this->getUrl('*/*/edit', array(
        'store'=>$this->getRequest()->getParam('store'),
        'id'=>$row->getId())
    );
    }
}

that way when you have a storeId that is provided by the _getStore() this will give each row the proper URL when the user comes to the catalog->manage products for the first time and they are not allowed to modify the default global values.

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.