`
kirenenko04
  • 浏览: 150968 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

Magento Adding Store View selection to your Module’s Adminhtml

 
阅读更多

While developing an extension recently I came across the need to add the ability to allow changes on a store view level.

To start off with I wanted to make sure this functioned the same as the built store view selection, to make it easy on the end user (take a look at how store view is handled with CMS Page creation to see what I’m referring to). However, by default Magento keeps its store view information in seperate tables (ie CMS_PAGE_STORE), which wasn’t ideal for this scenerio. The solution I came up with involves using one field ‘store_id’ in your modules table to save the Store ID’s as comma seperated values.

 

In your module’s adminhtml edit/create form (<Namespace>_<Module>_Block_Adminhtml_<Model>_Edit_Tab_Form add the following in the _prepareForm function where you would like the store selection to show up:

if (!Mage::app()->isSingleStoreMode()) {
    $fieldset->addField('store_id', 'multiselect', array(
        'name' => 'stores[]',
        'label' => Mage::helper('')->__('Store View'),
        'title' => Mage::helper('')->__('Store View'),
        'required' => true,
        'values' => Mage::getSingleton('adminhtml/system_store')
                     ->getStoreValuesForForm(false, true),
    ));
}
else {
    $fieldset->addField('store_id', 'hidden', array(
        'name' => 'stores[]',
        'value' => Mage::app()->getStore(true)->getId()
    ));
}

(This adds the store view multiselect to your form if you have multiple stores setup, if not it adds a hidden field with the current stores id taking into consideration the posibility of more stores being added in the future.)

Next in your module’s Controller file(<Namespace>_<Module>_Adminhtml_Controller) add the following to your saveAction:

if(isset($data['stores'])) {
    if(in_array('0',$data['stores'])){
        $data['store_id'] = '0';
    }
    else{
        $data['store_id'] = implode(",", $data['stores']);
    }
   unset($data['stores']);
}

(This sets store_id to 0 if “All Store Views” was selected or sets store_id as comma-seperated values representing the store selection.)

In your modules Grid file (<Namespace>_<Module>_Block_Adminhtml_<Model>_Grid), add the following:

protected function _prepareCollection(){
    $collection = Mage::getModel('_/')->getCollection();
    foreach($collection as $link){
        if($link->getStoreId() && $link->getStoreId() != 0 ){
            $link->setStoreId(explode(',',$link->getStoreId()));
        }
        else{
            $link->setStoreId(array('0'));
        }
    }
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

(This parses the collections store_id’s and resets them as an Array, taking into consideration any that might not be assigned yet or any with a value of 0, which represents ‘All Store Views’)

In the grids prepareColumns function add:

if (!Mage::app()->isSingleStoreMode()) {
    $this->addColumn('store_id', array(
        'header'        => Mage::helper('')->__('Store View'),
        'index'         => 'store_id',
        'type'          => 'store',
        'store_all'     => true,
        'store_view'    => true,
        'sortable'      => true,
        'filter_condition_callback' => array($this,
            '_filterStoreCondition'),
    ));
}

(If you have multiple stores setup this will add the “Store View” column with the store selection drop down, if no column is added as its not needed.)

and add the following function:

protected function _filterStoreCondition($collection, $column){
    if (!$value = $column->getFilter()->getValue()) {
        return;
    }
    $this->getCollection()->addStoreFilter($value);
}

(This function checks to see if a store filter has been selected and if so calls the function to add the filter to the collection.)

Lastly in your modules Collection file(<Namespace>_<Module>_Model_Mysql4_<Model>_Collection) add this function:

public function addStoreFilter($store, $withAdmin = true){

    if ($store instanceof Mage_Core_Model_Store) {
        $store = array($store->getId());
    }

    if (!is_array($store)) {
        $store = array($store);
    }

    $this->addFilter('store_id', array('in' => $store));

    return $this;
}

(This is the function that actually applies the store view filter to your collection)

分享到:
评论

相关推荐

    Magento 2 Beginners Guide

    Set up newsletter and transactional emails so that communication from your website corresponds to the website’s look and feel Make the store look good in terms of PCI compliance About the Author ...

    Magento 自定义后台menu Insert dynamical menu in Magento’s Admin

    Magento的后台菜单主要由` Mage_Adminhtml_Block_Menu`类控制,它通过遍历系统配置中的`adminhtml/menu`节点来生成菜单。每个菜单项都是一个XML节点,例如: ```xml &lt;custommodule module="Custom_Module"&gt; ...

    Magento 2 Cookbook

    compared to Magento 1. Where Magento 1 could be installed through FTP or SSH, Magento 2 is installable only via the command-line interface for an experienced webmaster. Chapter 2, Magento 2 System ...

    The Definitive Guide to Magento (Apress出品 Magento权威指南)

    - Magento采用了MVC(Model-View-Controller)架构模式,这种模式有助于提高代码的可读性和可维护性。 - 使用ZEND Framework作为底层框架,提供了强大的功能支持。 2. **模块化设计** - Magento的设计理念之一...

    magento2-store-switch-all-store-views:此模块使所有商店视图都可以在商店切换器中使用,无论它们的商店或网站如何

    它扩展了核心存储开关Magento\Store\Block\Switcher 。 通过将\IMI\StoreSwitch\ViewModel\StoreSwitchModel作为参数传递给切换器模块,并使用IMI_StoreSwitch::switch/languages.phtml模板,您可以在所有网站的...

    Magento2 CookBook

    compared to Magento 1. Where Magento 1 could be installed through FTP or SSH, Magento 2 is installable only via the command-line interface for an experienced webmaster. Chapter 2, Magento 2 System ...

    Magento 添加后台管理

    在`app/code/{Vendor}/{Module}/etc/adminhtml/routes.xml`中定义后台路由,然后在`app/code/{Vendor}/{Module}/view/adminhtml/layout`目录下创建布局文件,以指定页面结构。同时,在`app/code/{Vendor}/{Module}/...

    Magento: 后台添加预览按钮 View product in frontend from Magento admin

    这篇博客“Magento:后台添加预览按钮 View product in frontend from Magento admin”主要探讨了如何通过自定义开发来实现这一功能,让管理员能够快速检查商品在网站前台的显示状态。 在Magento中,通常管理员需要...

    The Definitive Guide to Magento.pdf

    ### Magento 全面指南知识点概览 #### 一、引言 《Magento全面指南》是电子商务平台Magento的权威参考书籍,由Adam McCombs与Robert Banh共同编写。本书不仅适用于初学者,对于有一定经验的开发者和技术人员也同样...

    magento2-blog-module-tutorial, 关于如何从头开始创建 magento 2模块的教程,带有测试.zip

    magento2-blog-module-tutorial, 关于如何从头开始创建 magento 2模块的教程,带有测试 2博客模块教程这个模块是 WIP,它将被更新为每个教程我目前正在写。介绍如何从头创建完整功能的Magento 2模块。 即使有测试,...

    RootCategory:Magento - 为 storeview 设置根类别

    在 Magento 电子商务平台中,根类别(Root Category)是一个至关重要的概念,它是商店视图(Store View)的基础结构,用于组织商品和子类别。本文将详细介绍如何为 Magento 的 storeview 设置根类别,以及如何利用 ...

    magento-store-locator:使用Google Maps的Magento商店定位器

    URL设置为YOUR_MAGENTO.LINK/store-location 兼容于 Magento CE 1.5+和EE 1.1+ ####使用安装 cd MAGENTO_ROOT modgit init modgit -i plugin/:. add Rabee3_StoreLocator ...

    magento 很不错的模板 收藏

    Magento是一款强大的开源电子商务平台,以其高度可定制性和灵活性著称。这款系统被广泛用于创建各种类型的在线商店,从高端时尚品牌到日常用品销售商。在本案例中,我们讨论的是一个专门为LV包包(以及可能包括内衣...

    Magento - Developing and Maintaining your Cloud Project

    ### Magento - 开发与维护您的云项目 #### Magento 云平台简介 Magento Commerce Cloud 是一个为企业提供强大且灵活的电子商务解决方案的平台。该平台通过整合前端用户体验与后端管理工具来帮助商家创建个性化且...

    Magento Beginner's Guide

    ### Magento初学者指南知识点概述 #### 一、Magento开源电子商务平台简介 Magento是一款功能强大的开源电子商务平台,旨在帮助用户创建动态且功能全面的在线商店。它不仅提供了丰富的特性和工具来满足不同规模的...

    Create new module “HelloWorld” – in Magento

    &lt;config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"&gt; &lt;module name="HelloWorld" setup_version="1.0.0"/&gt; ``` 接...

    phoenix-module-cashondelivery-1.0.2_lady4mf_magento_magento2_Pho

    标题 "phoenix-module-cashondelivery-1.0.2_lady4mf_magento_magento2_Pho" 提供的信息表明这是一个 Magento 2 模块,由 PhoenixModule 创建,版本号为 1.0.2,并且可能与 lady4mf 有关。这个模块专注于 "Cash on ...

    magento快速复制网站_magento_magento快速复制站_

    这里,`/path/to/source/magento/`是源站点的Magento根目录,`/path/to/destination/`是目标服务器的目录。 在新服务器上安装MySQL,并导入先前备份的数据库: ```bash mysql -u [new_username] -p[new_password] ...

Global site tag (gtag.js) - Google Analytics