1

I'm trying to get the store selector in a product grid which is displayed in a form of a custom module and loaded through ajax.

At the moment I have this code:

app/code/local/Module/Blog/Block/Manage/Blog/Edit/Tabs.php

protected function _beforeToHtml()
    {
        $this->addTab('related_products_section', array(
            'label'     => Mage::helper('blog')->__('Related products'),
            'title'     => Mage::helper('blog')->__('Related products'),
            'url'       => $this->getUrl('*/*/relatedproducts', ['_current' => true]),
            'class'     => 'ajax',
        ));

        return parent::_beforeToHtml();
    }

app/design/adminhtml/default/default/layout/module_blog.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <blog_admin_manage_blog_relatedproducts>
        <block type="core/text_list" name="root" output="toHtml">
            <block type="module_blog/adminhtml_blog_edit_form_renderer_fieldset_relatedproductgrid_grid"
                   name="related.product.grid"/>
            <block type="adminhtml/widget_grid_serializer" name="grid_serializer">
                <reference name="grid_serializer">
                    <action method="initSerializerBlock">
                        <grid_block_name>related.product.grid</grid_block_name>
                        <data_callback>getSelectedRelatedProducts</data_callback>
                        <hidden_input_name>related_products</hidden_input_name>
                        <reload_param_name>related_products</reload_param_name>
                    </action>
                    <action method="addColumnInputName">
                        <input_name>position</input_name>
                    </action>
                </reference>
            </block>
        </block>
    </blog_admin_manage_blog_relatedproducts>
</layout>

app/code/local/module/Blog/controllers/Manage/BlogController.php

 public function relatedProductsAction(){
        $this->loadLayout();

        /*
         * Remove serializer when ajax request has page parameter
         * The serializer has already been initialized at this moment
         */
        if ($this->getRequest()->getParam('ajax')) {
            $this->getLayout()->getBlock('root')->unsetChild('grid_serializer');
        }

        $this->getLayout()->getBlock('related.product.grid');

        $this->renderLayout();
    }

app/code/local/Module/Blog/Block/Adminhtml/Blog/Edit/Form/Renderer/Fieldset/Relatedproductgrid/Grid.php setId('relatedProductGrid'); $this->setUseAjax(true); $this->setDefaultSort('grid_id'); $this->setDefaultDir('DESC'); $this->setSaveParametersInSession(false); }

    /**
     * @return Mage_Adminhtml_Block_Widget_Grid
     */
    protected function _prepareCollection()
    {
        /** @var Mage_Catalog_Model_Resource_Product_Collection $collection */
        $collection = Mage::getModel('catalog/product')
            ->getCollection()
            ->addWebsiteFilter(
                $this->getWebsiteIds(
                    $this->getBlogData()->getStoreId()
                )
            )
            ->addAttributeToSelect([
                'name',
                'sku',
                'url_key',
                'visibility',
                'status',
                'manufacturer',
                'type_id',
                'product_soort_filter'
            ]);
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    /**
     * @param array $storeIds
     * @return array
     */
    protected function getWebsiteIds (array $storeIds)
    {
        $websiteIds = [];

        foreach ($storeIds as $storeId) {
            $websiteIds[$storeId] = Mage::getModel('core/store')
                ->load($storeId)
                ->getWebsiteId();
        }

        return $websiteIds;
    }

    /**
     * @param $column
     * @return $this|void
     */
    protected function _addColumnFilterToCollection($column)
    {
        // Set custom filter for in product flag
        if ($column->getId() == 'grid_product_id') {
            $relatedProducts = $this->_getSelectedRelatedProducts();

            if (empty($relatedProducts)) {
                $relatedProducts = 0;
            }
            if ($column->getFilter()->getValue()) {
                $this->getCollection()->addFieldToFilter('entity_id', ['in' => $relatedProducts]);
            } else {
                if ($relatedProducts) {
                    $this->getCollection()->addFieldToFilter('entity_id', ['nin' => $relatedProducts]);
                }
            }
        } else {
            parent::_addColumnFilterToCollection($column);
        }
    }

    /**
     * @return array
     */
    protected function _getSelectedRelatedProducts ()
    {
        return $this->getSelectedRelatedProducts();
    }

    /**
     * @return array
     */
    protected function getCurrentRelatedProducts ()
    {
        if (! $this->getData('blog_data')) {
            $this->setBlogData(
                Mage::getModel('module_blog/blog')
                    ->load($this->getRequest()->getParam('id'))
            );
        }

        return (array) $this->getBlogData()->getRelatedProducts();
    }

    /**
     * @return array
     */
    public function getSelectedRelatedProducts ()
    {
        $currentRelated = (array) $this->getCurrentRelatedProducts(); // get post ids from database
        $requestRelated = (array) $this->getRequest()->getParam('related_products'); // get product ids from request
        $related = [];

        foreach ($currentRelated as $item) {
            if (in_array($item, $requestRelated) || empty($requestRelated)) {
                $related[$item] = $item;
            }
        }
        return $related;
    }

    /**
     * @return $this
     */
    protected function _prepareColumns()
    {
        $this->addColumn('grid_related_product_id', [
            'header' => Mage::helper('module_blog')->__(''),
            'index' => 'entity_id',
            'type' => 'checkbox',
            'field_name' => 'grid_related_product_id',
            'values' => $this->_getSelectedRelatedProducts()
        ]);

        $this->addColumn('position', [
            'header'            => Mage::helper('module_blog')->__('Position'),
            'column_css_class'  =>'no-display', //this sets a css class to the column row item
            'header_css_class'  =>'no-display', //this sets a css class to the column header
            'name'              => 'position',
            'width'             => 60,
            'type'              => 'number',
            'validate_class'    => 'validate-number',
            'index'             => 'entity_id',
            'editable'          => true,
            'edit_only'         => true
        ]);

        $this->addColumn('grid_id', [
            'header' => Mage::helper('module_blog')->__('ID'),
            'width' => '50px',
            'index' => 'entity_id',
            'type' => 'number'
        ]);

        $this->addColumn('grid_title', [
            'header' => Mage::helper('module_blog')->__('Title'),
            'index' => 'name'
        ]);

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

        $this->addColumn('grid_identifier', [
            'header' => Mage::helper('module_blog')->__('Identifier'),
            'index' => 'url_key'
        ]);

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

        $this->addColumn('type', [
            'header'=> Mage::helper('catalog')->__('Type'),
            'width' => '70px',
            'index' => 'type_id',
            'type'  => 'options',
            'options' => Mage::getModel('catalog/product_type')->getOptionArray(),
        ]);

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

        $this->addColumn('grid_manufacturer', [
            'header'=> Mage::helper('catalog')->__('Manufacturer'),
            'width' => '70px',
            'index' => 'manufacturer',
            'type'  => 'options',
            'options' => $this->getAttributeOptions('manufacturer')
        ]);

        $this->addColumn('grid_product_filter', [
            'header' => Mage::helper('module_blog')->__('Product soort'),
            'width' => '50px',
            'index' => 'product_soort_filter',
            'type' => 'options',
            'options' => $this->getAttributeOptions('product_soort_filter')
        ]);

        $this->addColumn('grid_created_time', [
            'header'    => Mage::helper('blog')->__('Created'),
            'align'     => 'left',
            'width'     => '120px',
            'type'      => 'date',
            'default'   => '--',
            'index'     => 'created_at'
        ]);

        $this->addColumn('grid_update_time', [
            'header'    => Mage::helper('blog')->__('Updated'),
            'align'     => 'left',
            'width'     => '120px',
            'type'      => 'date',
            'default'   => '--',
            'index'     => 'updated_at'
        ]);

        return parent::_prepareColumns();
    }

    /**
     * @return string
     */
    public function getGridUrl()
    {
        return $this->_getData('grid_url') ?
            $this->_getData('grid_url') :
            $this->getUrl('*/*/relatedproducts', [
                '_current' => true
            ]);
    }

    /**
     * Get option array for an attribute
     *
     * @param $attributeCode
     * @return array
     */
    protected function getAttributeOptions ($attributeCode)
    {
        /** @var Mage_Eav_Model_Entity_Attribute $attributeInfo */
        $attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
            ->setCodeFilter($attributeCode)
            ->getFirstItem();
        $attributeId = $attributeInfo->getAttributeId();
        /** @var Mage_Catalog_Model_Resource_Eav_Attribute $attribute */
        $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
        $options = [];

        if ($attribute->usesSource()) {
            $attributeOptions = $attribute
                ->getSource()
                ->getAllOptions(false);

            foreach ($attributeOptions as $k => $data) {
                $options[$data['value']] = $data['label'];
            }
        }

        return $options;
    }
}

This is all working fine and a product grid is shown and pagination and checkboxes for saving products is all working fine.

The problem: We have multiple stores and at the moment only products for a single store are shown. In the magento product grid there is a simple solution for this: a store view select. I'd like to display this selector above my own grid as well, but I can't figure out how to do this.

To clearify:

I have this grid working properly: I have this grid working properly

I want this above my grid: I want this above my grid

Any ideas?

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.