0

I have created custom tab with products in Catalog >> Product >> Edit

enter image description here

But want to apply same grid functionality like related products including remove products.

Tabs.php (Vendor/ModuleName/Block/Adminhtml/Catalog/Product/Tabs.php)

<?php

/**
 * Class N98_ARA_Block_Adminhtml_Catalog_Product_Tabs
 */
class N98_ARA_Block_Adminhtml_Catalog_Product_Tabs extends Mage_Adminhtml_Block_Widget_Grid
    implements Mage_Adminhtml_Block_Widget_Tab_Interface
{

    /**
     * Set the template for the block
     *
     */
    public function _construct()
    {
        parent::_construct();
        $this->setId('n98_ara_product_relation_grid');
        $this->setDefaultSort('ID');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }

    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', ['_current' => true]);
    }

    /**
     * Retrieve the label used for the tab relating to this block
     *
     * @return string
     */
    public function getTabLabel()
    {
        return $this->__('ARA Relations');
    }

    /**
     * Retrieve the title used by this tab
     *
     * @return string
     */
    public function getTabTitle()
    {
        return $this->__('Ara Product Relations');
    }

    /**
     * Determines whether to display the tab
     * Add logic here to decide whether you want the tab to display
     *
     * @return bool
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * Stops the tab being hidden
     *
     * @return bool
     */
    public function isHidden()
    {
        return false;
    }

    /**
     * @return $this|\Mage_Adminhtml_Block_Widget_Grid
     */
    protected function _prepareCollection()
    {
        parent::_prepareCollection();
        $product = Mage::registry('product');
        $store = $this->_getStore();
        $relationCollection =
            Mage::getModel('n98_ara/product_relation')->getCollection()->addFieldToFilter('sku', $product->getSku());

        $skus = [];

        foreach ($relationCollection as $relationitem) {
            $skus[] = $relationitem->getAraSku();
        }

        $collection = Mage::getModel('catalog/product')->getCollection();
        // Put in the array below the attributes you want to select
        $collection->addAttributeToSelect(['name', 'sku', 'status', 'store_id']);
        $collection->addAttributeToFilter('sku', ['in' => $skus]);

        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()
            );
        } else {
            $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
        }

        $this->setCollection($collection);

        parent::_prepareCollection();
        $this->getCollection()->addWebsiteNamesToResult();
        //$this->getCollection()->addPriceCalculationToResult();

        return $this;
    }

    protected function _getStore()
    {
        $storeId = (int) $this->getRequest()->getParam('store', 0);

        return Mage::app()->getStore($storeId);
    }

    protected function _prepareColumns()
    {
        $helper = Mage::helper('n98_ara');

        $this->addColumn(
            'itemCheckbox',
            [
                'index'      => 'entity_id',
                'type'       => 'checkbox',
                'width'      => 20,
                'sortable'   => false,
                'field_name' => 'map[]'
            ]
        );

        $this->addColumn(
            'entity_id',
            [
                'header' => $helper->__('ID'),
                'index'  => 'entity_id'
            ]
        );

        $this->addColumn(
            'name',
            [
                'header' => $helper->__('Name'),
                'index'  => 'name'
            ]
        );

        $this->addColumn(
            'sku',
            [
                'header' => $helper->__('Sku'),
                'index'  => 'sku'
            ]
        );

        $this->addColumn(
            'status',
            [
                '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',
                [
                    'header'   => Mage::helper('catalog')->__('Websites'),
                    'width'    => '100px',
                    'sortable' => false,
                    'index'    => 'websites',
                    'type'     => 'options',
                    'options'  => Mage::getModel('core/website')->getCollection()->toOptionHash(),
                ]
            );
        }

        return parent::_prepareColumns();
    }
}

But this is not working as related products, specially not removing any visible products relation which are in custom table.

any help would be appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.