Làm cách nào để tạo Trang CMS Magento không thể chỉnh sửa?


16

Trên bản cài đặt mới của Magento, nó đi kèm với một số trang CMS mặc định mà bạn có thể chỉnh sửa CMS > Pages. Tuy nhiên, nó cũng đi kèm với một vài Trang CMS "không thể chỉnh sửa": Orders and ReturnsContact Us... cả hai đều là các trang có biểu mẫu trên đó và nêu bật một thiếu sót trong Magento CE: tạo và chỉnh sửa biểu mẫu.

Tôi đã quản lý để ghi đè Liên hệ mặc định bằng biểu mẫu của riêng tôi, nhưng tôi muốn thêm một biểu mẫu khác và có thể cần thêm nhiều biểu mẫu trong tương lai. Tôi có phần quen thuộc với việc tạo Mô-đun Magento để ghi đè các chức năng và trang hiện có, như tôi đã làm cho đến nay.

Tôi đã bắt đầu làm việc trên một mô-đun cho phép khả năng tạo các trang biểu mẫu trong Magento, nhưng chúng phải ẩn với quản lý CMS giống như các biểu mẫu mặc định. Tôi đã tìm thấy câu trả lời cho việc lập trình một trang CMS, nhưng nó bổ sung nó vào Magento CMS > Pages.

Làm cách nào để tạo một trang CMS chỉ có thể chỉnh sửa bởi Mô-đun Magento?


Tốt để biết! Có phải là bài đăng chéo hay thực tế được chấp nhận kể từ khi tôi đăng bài này ở đây không?
andyjv

Tôi sẽ nhấp vào liên kết "cờ" và yêu cầu một mod để di chuyển nó cho bạn. Nói chung đăng chéo là nhăn mặt.
John Conde

Nếu bạn đang tìm kiếm một hình thức liên lạc tùy chỉnh trong một trang CMS, xem magento.stackexchange.com/questions/79602/... hoặc chi tiết hơn stackoverflow.com/q/1066127/664108
Fabian Schmengler

Câu trả lời:


21

Trên thực tế 'Liên hệ với chúng tôi' và 'Đơn hàng và trả lại' không phải là trang CMS. Chúng thực sự là các trang từ một mô-đun riêng biệt. Chúng giống như trang 'Đăng nhập' hoặc 'Đăng ký' hơn là các trang CMS. Để tạo một trang như thế này, bạn có thể tạo một mô-đun đơn giản với bộ điều khiển, một khối và một mẫu. Hãy gọi phần mở rộng Easylife_Customform. Đối với điều này, bạn sẽ cần các tập tin sau.
app/etc/modules/Easylife_Customform.xml- tệp khai báo mô-đun

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Customform>
            <active>true</active>
            <codePool>local</codePool>
        </Easylife_Customform>
    </modules>
</config>

app/code/local/Easylife/Customform/etc/config.xml - tập tin cấu hình

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Customform>
            <version>0.0.1</version>
        </Easylife_Customform>
    </modules>
    <global>
        <blocks>
            <customform><!-- block alias -->
                <class>Easylife_Customform_Block</class>
            </customform>
        </blocks>
        <helpers>
            <customform><!-- helper alias -->
                <class>Easylife_Customform_Helper</class>
            </customform>
        </helpers>
    </global>
    <frontend>
        <routers>
            <customform>
                <use>standard</use>
                <args>
                    <module>Easylife_Customform</module>
                    <frontName>customform</frontName><!-- url key for module -->
                </args>
            </customform>
        </routers>
        <layout>
            <updates>
                <easylife_customform>
                    <file>easylife_customform.xml</file><!-- frontend layout file -->
                </easylife_customform>
            </updates>
        </layout>
        <translate>
            <modules>
                <Easylife_Customform>
                    <files>
                        <default>Easylife_Customform.csv</default><!-- translation file (not mandatory) -->
                    </files>
                </Easylife_Customform>
            </modules>
        </translate>
    </frontend>
</config>

app/design/frontend/base/default/layout/easylife_customform.xml - tập tin bố trí lối vào

<?xml version="1.0"?>
<layout>
    <customform_index_index translate="label" module="customform">
        <label>Custom form</label>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action><!-- can be different -->
        </reference>        
        <reference name="content">
            <block type="core/template" name="customform" template="easylife_customform/form.phtml" /><!-- content of page -->
        </reference>
    </customform_index_index>
</layout>

app/code/local/Easylife/Customform/Helper/Data.php - trợ giúp mô-đun mặc định

<?php
class Easylife_Customform_Helper_Data extends Mage_Core_Helper_Abstract{
}

app/design/frontend/base/default/template/easylife_customform/form.phtml - html thực tế cho biểu mẫu - làm cho nó trông giống như bạn cần

<form action="<?php echo $this->getUrl('customform/index/send')?>">
    <input type="text" name="name" />
    <input type="submit" />
</form>

app/code/local/Easylife/Customform/controllers/IndexController.php - bộ điều khiển mô-đun

<?php 
class Easylife_Customform_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){ //this will display the form
        $this->loadLayout();
        $this->_initLayoutMessages('core/session'); //this will allow flash messages
        $this->renderLayout();
    }
    public function sendAction(){ //handles the form submit
        $post = $this->getRequest()->getPost();
        //do something with the posted data
        Mage::getSingleton('core/session')->addSuccess($this->__('Your message was sent'));//add success message.
        $this->_redirect('*/*');//will redirect to form page
    }
}

Điều này nên được nó. Xóa bộ nhớ cache và bạn sẽ có thể truy cập vào biểu mẫu tại mysite.com/customform
Tôi hy vọng tôi đã viết mã chính xác và không bỏ lỡ điều gì


2
bạn thực sự đã đi xa hơn về câu trả lời này. +1
philwinkle

@philwinkle: tốt hay xấu? :)
Marius

Hướng dẫn thực sự tuyệt vời Marius, cảm ơn bạn! Tôi đang cố gắng đặt tiêu đề trang, <nhãn> trong bố cục xml của tôi đang bị bỏ qua và điều này <reference name="head"> <action method="setTitle" translate="title"><title>Subscribe to our Newsletter</title></action> </reference> sẽ không hoạt động.
loeffel

@loeffel. Có lẽ bạn có một cái gì đó khác ghi đè tiêu đề. Về lý thuyết mã nên hoạt động.
Marius

@Marius Điều này rất tiện dụng, nhưng làm thế nào chúng ta có thể thêm Thông báo lỗi? Tôi đã thử thêm Mage::getSingleton('core/session')->addError("Error");nhưng không có may mắn. Nó chỉ hiển thị thông điệp thành công. Có ai giúp đỡ không?
Aamir Siddique
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.