Bạn muốn 1 promotion hay 1 tin nào đó sử dụng cho nhiều store, nếu bạn không muốn sử dụng store_id trên 1 bảng thì bạn tách ra 2 bảng, và phần hiển thị store_id trên Grid và Form như sau:
Ví dụ ta có 2 table:
promotion(promotion_id, promotion_title, date_create,…)
promotion_store(promotion_id, store_id)
Ví dụ này xem như bạn đã viết hoàn chỉnh module này rồi.
Trong admin panel, phần grid hiển thị promotion, bạn cần hiển thị thêm mục sotre view, thì sẽ làm như sau:
trong thư mục Model, trong thư mục Resource hoặc Mysql4 (tùy theo code của bạn), mở file Modelpromotion.php và thêm vào đoạn code sau nếu chưa có:
protected function _afterLoad(Mage_Core_Model_Abstract $object)
{
$select = $this->_getReadAdapter()->select()
->from($this->getTable(‘modelpromotion_store’))
->where(‘promotion_id = ?’, $object->getId());if ($data = $this->_getReadAdapter()->fetchAll($select)) {
$stores = array();
foreach ($data as $row) {
$stores[] = $row[‘store_id’];
}
$object->setData(‘store_id’, $stores);
}return parent::_afterLoad($object);
}
protected function _afterSave(Mage_Core_Model_Abstract $object)
{$condition = $this->_getWriteAdapter()->quoteInto(‘promotion_id = ?’, $object->getId());
$this->_getWriteAdapter()->delete($this->getTable(‘modelpromotion_store’), $condition);if (count($object->getData(‘stores’)) && (!in_array(0, (array)$object->getData(‘stores’)))) {
foreach ((array)$object->getData(‘stores’) as $store) {
$data = array();
$data[‘promotion_id’] = $object->getId();
$data[‘store_id’] = $store;
$this->_getWriteAdapter()->insert($this->getTable(‘modelpromotion_store’), $data);
}
} else {
$data = array();
$data[‘promotion_id’] = $object->getId();
$data[‘store_id’] = ‘0’;
$this->_getWriteAdapter()->insert($this->getTable(‘modelpromotion_store’), $data);
}return parent::_afterSave($object);
}
protected function _beforeDelete(Mage_Core_Model_Abstract $object){
$adapter = $this->_getReadAdapter();
$adapter->delete($this->getTable(‘modelpromotion_store’), ‘promotion_id=’.$object->getId());}
Sau đó vào thư mục Modelpromotion trong thư mục phía trên (Resource hay Mysql4), mở file Collection.php và chèn váo hàm sau nếu chưa có:
public function _construct()
{
$this->_init(‘promotion/modelpromotion’);//không có promotion_id – là khóa chính
}public function addStoreFilter($store)
{
$this->getSelect()->join(
array(‘modelpromotion_store_table’ => $this->getTable(‘modelpromotion_store’)),
‘main_table.promotion_id = modelpromotion_store_table.promotion_id’,
array()
)
->where(‘modelpromotion_store_table.store_id in (?)’, array(0, $store));
$this->getSelect()->distinct();
return $this;
}
Tiếp theo vào thư mục Block/Adminhtml/Promotion mở thư mục grid.php lên thêm vào hàm sau nếu chưa có:
protected function _afterLoadCollection()
{
$this->getCollection()->walk(‘afterLoad’);
parent::_afterLoadCollection();
}protected function _filterStoreCondition($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return;
}$this->getCollection()->addStoreFilter($value);
}
Trong hàm protected function _prepareColumns() thêm vào hoặc sửa column store_id như sau:
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn(‘store_id’, array(
‘header’ => Mage::helper(‘promotion’)->__(‘Store View’),
‘align’ =>’right’,
‘width’ => ‘100px’,
‘index’ => ‘store_id’,
‘type’ => ‘store’,
‘store_all’ => true,
‘store_view’ => true,
‘sortable’ => false,
‘filter_condition_callback’ => array($this, ‘_filterStoreCondition’),
));
}
Sau đó kiểm tra trong grid có hiển thị lên chưa.
Tiếp theo vào mở file Form.php trong thư mục Edit/Tab, sửa hoặc thêm đoạn code sau:
$fieldset->addField(‘store_id’,’multiselect’,array(
‘name’ => ‘stores[]’,
‘label’ => Mage::helper(‘promotion’)->__(‘Store View’),
‘title’ => Mage::helper(‘promotion’)->__(‘Store View’),
‘required’ => true,
‘values’ => Mage::getSingleton(‘adminhtml/system_store’)->getStoreValuesForForm(false, true)
));
Ngoài frontend, muốn hiển thị dữ liệu theo 1 store thì sẽ sử dụng code sau, thướng sử dụng trong controller hay file phtml:
$store_id = Mage::app()->getStore()->getId();
$collection = Mage::getModel(‘promotion/modelpromotion’)->getCollection();
$collection->addStoreFilter($store_id);
Hoặc nếu bạn sử dụng store_id trên 1 bảng, kiểu dữ liệu của store_id là varchar, thì bạn tham khảo bài viết này: https://www.demacmedia.com/magento-commerce/mini-tutorial-adding-store-view-selection-to-your-modules-adminhtml/