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)
相关推荐
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 ...
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 cloud - Developing and Maintaining your Cloud Project.
Apress.The.Definitive.Guide.to.Magento.Dec.2009.pdf Apress于2009重磅推出的Magento上手书籍,原书地址:http://www.apress.com/book/view/9781430272298
它扩展了核心存储开关Magento\Store\Block\Switcher 。 通过将\IMI\StoreSwitch\ViewModel\StoreSwitchModel作为参数传递给切换器模块,并使用IMI_StoreSwitch::switch/languages.phtml模板,您可以在所有网站的...
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入门原版书.。。The Definitive Guide to Magento.pdf
Magento Beginner's Guide
magento2-blog-module-tutorial, 关于如何从头开始创建 magento 2模块的教程,带有测试 2博客模块教程这个模块是 WIP,它将被更新为每个教程我目前正在写。介绍如何从头创建完整功能的Magento 2模块。 即使有测试,...
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 访牌LV包包网站模板主题,很不错的模板
Per-category and per-store view aheadWorks Market Segmentation Suite extension integration Enabling/disabling of any block and its items Customizable block and item sorting Independent items ...
composer require smile/module-store-delivery 启用它 bin/magento module:enable Smile_StoreDelivery 安装模块并重建DI缓存 bin/magento setup:upgrade 如何配置 商店>配置>销售>送货方式>商店交货 场地 类型...
To use banner in your CMS pages use: {{block type="banner/banner" name="banner.home" banner_group_code="banner_group1" template="unibanner/banner.phtml"}} NOTE: Here banner_group_code value i.e. ...
Understand Magento extensions, and build your own from scratch! Overview Get a detailed insight into the structure of Magento Learn about event/observer-driven modules Get a hands-on introduction to...
对于首次安装magento过程中出现的问题的小结
Magento 2 Store Switcher扩展程序通过从IP地址检测他们的位置,为国际网站访问者带来了适当的商店视图。 在此商店视图导航之后,客户可以使用正确的语言,货币和其他本地化因素,从而获得更好的购物体验。 社区版:...
Magento 1.3 PHP Developer's Guide mangeto开发资料
Ajax-magento2-module-ajax.zip,用于ajax请求的magento 2模块,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态网页,其中网页的小部分在不重新加载网页...