Magento message

1. Magento 消息提示使用方法

Magento 消息提示

//成功

Mage::getSingleton('customer/session')->addSuccess('恭喜您关联会员卡成功!');

//失败

Mage::getSingleton('customer/session')->addError($e->getMessage());

错误处理

if($this->getRequest()->isPost()){

    try {

$methodList = Mage::helper('recharge')->getRechargeMethodList();

$method = $this->getRequest()->getPost('method','');

$points = floatval($this->getRequest()->getPost('points',0));

if($points <= 0){

throw new exception('充值金额不能小于等于0');  //1错误异常

}

if('' == $method || !array_key_exists($method,$methodList)){

throw new exception('充值方式选择错误');

}

$methodInstance = Mage::helper('recharge')->getMethodInstance($method);

if (!$methodInstance) {

throw new exception('初始化充值错误');

}

Mage::getSingleton('customer/session')->addSuccess('恭喜您关联会员卡成功!');

    }catch (exception $e){

Mage::getSingleton('customer/session')->addError($e->getMessage());//2添加错误

    }

}

$this->loadLayout();

$this->_initLayoutMessages('customer/session'); //3.回显错误

$this->renderLayout();

phtml中添加

<?php echo $this->getMessagesBlock()->getGroupedHtml(); ?>

magento 成功 错误 警告 提示的使用方法

控制器里面添加下面代码 加页面提示

Mage::getSingleton('customer/session')->addSuccess($this->__('This email does not require confirmation.'));

这个是绿色的提示

Mage::getSingleton('customer/session')->addError($this->__('Login and password are required.'));

这个是红色的提示

Mage::getSingleton('customer/session')->addNotice($this->__('Please specify product option(s).'));

这个是黄色的提示


phtml里面必须添加下面代码 才可以show页面提示
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php echo $this->getMessagesBlock()->toHtml() ?>


控制器里面必须添加下面代码
$this->_initLayoutMessages('customer/session');

使用其他session ,如 check/session 等,请参考上面方式 举一反三

2. Magento的三种默认提示信息

比如,表单提交的时候,提交成功or提交失败,都应有一个反馈信息,来告诉用户,提交操作是否成功,在magento中,有几种默认的提示:

发送成功后的提示信息:

//在控制器中使用:
Mage::getSingleton('core/session')->addSuccess('Your request has been sent!');

发送失败后的提示信息:

//同样在控制器中使用:
Mage::getSingleton('core/session')->addError('Unable to send!');

备注,这个提示信息是红色的。


发送后的注意提示信息:

//同样是在控制器中添加:
Mage::getSingleton('core/session')->addNotice('Your request has been sent!!!!');

备注,这个提示信息橙红色的。

需要注意的是,使用上面三种提示信息,需要在IndexAction()中添加如下代码:

public function indexAction()
    {
        $this->loadLayout();
        //需要添加如下代码:
        $this->_initLayoutMessages('core/session');
        $this->renderLayout();
    }

在模板phtml页面添加:

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php echo $this->getMessagesBlock()->toHtml() ?>

3.商品加入购物车的消息提示

商品加入购物车之后,给你消息提示需要在控制器里面必须添加以下代码

$this->_initLayoutMessages('checkout/session');

完整代码如下:

public function historyAction()    
    {
        $this->loadLayout();
        $this->_initLayoutMessages('catalog/session');
        $this->_initLayoutMessages('checkout/session');
        $this->getLayout()->getBlock('head')->setTitle($this->__('My  Products'));
        if ($block = $this->getLayout()->getBlock('customer.account.link.back')) {
            $block->setRefererUrl($this->_getRefererUrl());
        }
        $this->renderLayout();
}

在输出文件模板里面添加

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>

Leave a comment

您的电子邮箱地址不会被公开。 必填项已用 * 标注