Tut Magento 017: Code thêm một product type trong Magento
Trong Tut Magento 16 mình có giới thiệu với các bạn 6 loại product mặc định của Magento đó là: simple product, virtual product, group product, bundle product, downloadable product, configurable product, tuy nhiên trong thực tế có những sản phẩm mà chẳng thuộc loại nào ở trên nên có thể ta sẽ phải thêm một loại product khác.
Trong Tut Magento 17 này mình sẽ hướng dẫn các bạn tạo thêm một product type cơ bản và tất nhiên mình sẽ không viết bất cứ hàm xử lý login nào của loại product này, việc đó sẽ dành cho các bạn vì trong mỗi trường hợp cụ thể ta phải viết nó theo một cách khác nhau.
Ta sẽ viết một module với tên Basetut_Tut17 để minh họa cho bài học này.
1. Thêm product type vào file config.xml
Code: app/code/local/Basetut/Tut17/etc/config.xml
<?xml version="1.0"?> <config> <modules> <Basetut_Tut17> <version>0.1.0</version> </Basetut_Tut17> </modules> <global> <catalog> <product> <type> <custom translate="label" module="tut17"> <label>Custom Product</label> <model>tut17/product_type_custom</model> <price_model>tut17/product_price</price_model> <is_qty>1</is_qty> </custom> </type> </product> </catalog> <helpers> <tut17> <class>Basetut_Tut17_Helper</class> </tut17> </helpers> </global> </config>
Trong file code này đơn giản chỉ có 3 phần chính:
– Khai báo tên module và version
<modules> <Basetut_Tut17> <version>0.1.0</version> </Basetut_Tut17> </modules>
– Khai báo thêm một product type
<catalog> <product> <type> <custom translate="label" module="tut17"> <label>Custom Product</label> <model>tut17/product_type_custom</model> <price_model>tut17/product_price</price_model> <is_qty>1</is_qty> </custom> </type> </product> </catalog>
Trong phần khai báo này ta có gọi đến 2 file:
+ Model của product type: tut17/product_type_custom
+ Model price của product type: tut17/product_price
– Khai báo file helper
<helpers> <tut17> <class>Basetut_Tut17_Helper</class> </tut17> </helpers>
2. Tạo file model cho product type
File code: app/code/local/Basetut/Tut17/Model/Product/Type/Custom.php
<?php class Basetut_Tut17_Model_Product_Type_Custom extends Mage_Catalog_Model_Product_Type_Abstract { //add code here }
3. Tạo file model price cho product type
File code: app/code/local/Basetut/Tut17/Model/Product/Price.php
<?php class Basetut_Tut17_Model_Product_Price extends Mage_Catalog_Model_Product_Type_Price { /* public function getPrice($product) { // do your price stuff here. }*/ }
Thêm một số file cần thiết để module có thể chạy được:
– Tạo file helper
File code: app/code/local/Basetut/Tut17/Helper/Data.php
<?php class Basetut_Tut17_Helper_Data extends Mage_Core_Helper_Abstract { //add code here }
– Tạo file xml active module
File code: app/etc/Basetut_Tut17.xml
<?xml version="1.0"?> <config> <modules> <Basetut_Tut17> <active>true</active> <codePool>local</codePool> </Basetut_Tut17> </modules> </config>
Bây giờ bạn có thể chạy thử module bằng cách xóa cache (nếu đang enable) sau đó vào backend:
– Catalog -> Manage Product
Click Add Product:
Chúc các bạn thêm product type thành công! Nếu có bất cứ vấn đề gì các bạn có thể post câu hỏi lên form: http://blankapham.com/answers/
anh ơi anh cho em hỏi chút muốn học magento thì cần phái biết trước những gì ạ
Nếu bạn đã từng lập trình với zend framework hoặc mô hình MVC rồi thì bạn học magento sẽ dễ hơn. Còn việc làm thế nào để lập trình tốt với Magento thì bạn cần phải hiểu được những thứ cơ bản của nó sau đó thực hành thật nhiều, chỉ khi đó thì bạn mới có thể hiểu được nó.
Chúc bạn thành công!
Khi nào có tut tiếp thế anh Admin
Có thể viết thêm các extention để manage những thứ khác kiểu như HRM hay ECM đc ko anh Ad?
Tut này rất hay và ý nghĩa.
Tutorial :))