Làm việc với module và block
2.1 Tạo module mới
2.2 Tạo block mới
2.2.1 Tạo block thông dụng
2.2.1.1 Yêu cầu
2.2.1.2 Các bước thực hiện
2.2.1.2.2 Tạo block class
2.2.1.2.3 Tạo block view
2.2.1.2.3.1 Tạo block view file
2.2.1.2.3.2 Chỉnh sửa tập tin xml
2.2.1.2.3.3 Đặt block vào theme của chúng ta
2.2.2 Tạo content block cho trang chủ
2.2.2.1 Yêu cầu
2.2.2.2 Các bước thực hiện
2.2.2.3 Tạo block class
2.2.2.4 Tạo block view
2.2.2.4.1 Tạo block view file
2.2.2.4.2 Chỉnh sửa tập tin xml
2.2.2.4.3 Đặt block vào theme của chúng ta
2.2.3 Thông tin hữu ích
2.1 Tạo module mớiTạo tập tin sau:
- app/etc/modules/Training_Foot.xml
<?xml version="1.0"?>
<config>
<modules>
<Training_Foot>
<active>true</active>
<codePool>local</codePool>
</Training_Foot>
</modules>
</config>
Ghi chú:
- Chúng ta có thể enable hoặc disable Foot module ở System → Configuration → Advanced → Advanced → Disable module output → Training_Foot.
2.2.1 Tạo block thông dụng
2.2.1.1 Yêu cầu
- Tạo block “Category” ở module “Foot”.
- Chúng ta có thể sử dụng block dùng chung “category”.
- Cấu hình Foot module.
- Tạo block class.
- Tạo block view.
Tạo các tập tin và thư mục sau:
- app/code/local/Training/Foot
- app/code/local/Training/Foot/etc
- app/code/local/Training/Foot/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Training_Foot>
<version>1.0</version>
</Training_Foot>
</modules>
</config>
2.2.1.2.2 Tạo block class
Tạo các tập tin và thư mục sau:
- app/code/local/Training/Foot/Block
- app/code/local/Training/Foot/Block/Category.php
<?xml version="1.0"?>
<config>
<modules>
<Training_Foot>
<version>1.0</version>
</Training_Foot>
</modules>
<global>
<blocks>
<foot>
<class>Training_Foot_Block</class>
</foot>
</blocks>
</global>
</config>
Nội dung tập tin “Category.php”:
<?php
class Training_Foot_Block_Category extends Mage_Core_Block_Template
{
public function _construct()
{
}
//put our functions that return value for block view here.
}
Ghi chú:
- Ở block view, chúng ta có thể gọi bằng cách $this->ourFunctionName()
Các bước tiến hành:
- Tạo block view file.
- Chỉnh sửa tập tin xml.
- Đặt block vào theme của chúng ta.
Tạo tập tin sau:
- app/design/frontend/training/foot/template/page/html/category.phtml
- Chúng ta có thể đổi tên tập tin “category”.
- Chúng ta có thể để tập tin “category.phtml” ở một chỗ khác trong “app/design/frontend/training/foot/template”.
- Gọi $this->ourFunctionName() để lấy giá trị từ block class.
- Mở tập tin “app/design/frontend/training/foot/layout/page.xml”.
- Đặt vào dòng
<block type="foot/category" name="category" as="category" template="page/html/category.phtml" />
- Mở tập tin “app/design/frontend/training/foot/template/page/2columns-left.phtml”.
- Đặt dòng
<?php echo $this->getChildHtml('category') ?>
vào khu vực tương ứng.
2.2.2.1 Yêu cầu
- Tạo “Home” block.
- Lấy dữ liệu động cho block này.
- Tạo Foot module → Không có gì thay đổi bởi mình đã tạo rồi.
- Tạo block class → Xem hướng dẫn phía trên.
- Tạo block view.
Tạo các tập tin và thư mục sau:
- app/code/local/Training/Foot/Block
- app/code/local/Training/Foot/Block/Home.php
<?php
class Training_Foot_Block_Home extends Mage_Core_Block_Template
{
public function _construct()
{
}
//put our functions that return value for block view here.
}
Ghi chú:
- Ở block view, lấy giá trị từ block class bằng cách gọi $this->ourFunctionName()
- Tạo block view file.
- Chỉnh sửa tập tin xml.
- Đặt block vào theme của chúng ta.
Tạo tập tin sau:
- app/design/frontend/training/foot/template/cms/home.phtml
- Chúng ta có thể đổi tên tập tin “home”.
- Chúng ta có thể đặt “home.phtml” ở một ví trí khác trong “app/design/frontend/training/foot/template”.
- Dùng $this->ourFunctionName() để lấy giá trị từ block class.
- Sao chép “cms.xml” từ “app/design/frontend/base/default/layout” sang “app/design/frontend/training/foot/layout”.
- Chỉnh sửa element “cms_index_index”, đặt vào đó
<reference name="content">
<block type="foot/home" name="foot.home" template="cms/home.phtml"/>
</reference>
2.2.2.4.3 Đặt block vào theme của chúng ta
Không cần thay đổi gì ở “app/design/frontend/training/foot/template/page/2columns-left.phtml”, bởi vì nó được gọi bởi
<?php echo $this->getChildHtml('content') ?>
2.2.3 Thông tin hữu ích
Get Simple products
|
Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' =>
'simple'))->addAttributeToSelect('*');
|
Get checkout url
|
$this->helper('checkout/cart')->getAddUrl($product_object)
|
Mutator method (getter, setter)
| View at app/code/core/Mage/Catalog/Model/Product.php |
Useful url
|
Blogger Comment
Facebook Comment