自定义模块添加到购物车成功消息Magento

时间:2012-03-16 13:02:04

标签: php magento

在我的自定义模块中,我想显示添加到购物车的成功消息。 我在.phtml文件中有以下代码:

echo '<form action="/checkout/cart/add/product/'.$_product->getId().'/" method="get">'."\n";
$_attributes = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray( $_product );
echo '<div class="product-attribute-options">'."\n";
$_child_products = Mage::getModel( 'catalog/product_type_configurable' )->getUsedProducts( null, $_product );
foreach( $_child_products as $_child_product ):
    if( $_child_product->isSaleable() ):
        $_child_product_qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct( $_child_product->getId() )->getQty();
        if( $_child_product_qty > 0 ):
            $_child_product_size_label = $_child_product->getResource()->getAttribute('size')->getFrontend()->getValue( $_child_product );
            $_child_product_size_val = Mage::getResourceModel('catalog/product')->getAttributeRawValue( $_child_product->getId(), 'size', Mage::app()->getStore()->getId() );
            echo '<button value="'.$_child_product_size_val.'">'.$_child_product_size_label.'</button>'."\n";
        endif;
    endif;
endforeach;
echo '<input type="hidden" class="super_attribute_val" name="super_attribute[145]" value="" />'."\n";
echo '</div>'."\n";
echo '<input type="hidden" name="qty" value="1" />'."\n";
echo '<div class="add-to-cart">'."\n";
echo '<button class="button btn-cart"><span>'.$this->__('Buy Now').'</span></button>'."\n";
echo '</div>'."\n";
echo '</form>'."\n";

在按立即购买时,它会将产品添加到购物车,但我也想在顶部显示绿色成功消息。

EDITED

我被建议调用以下函数:

function showMessage(txt, type) {
    var html = '<ul class="messages"><li class="'+type+'-msg"><ul><li>' + txt + '</li></ul></li></ul>';
    $('messages').update(html);
}

但考虑到我的表格,我不知道从哪里调用此功能。我有:

echo '<button class="button btn-cart"><span>'.$this->__('Buy Now').'</span></button>'."\n"; 

按下按钮后,它会通过动作get提交表单。

1 个答案:

答案 0 :(得分:1)

我认为此代码可以帮助您:

Mage::getSingleton(‘customer/session’)->addMessage("Your message");

另外,这个answer对你来说很有意思。

也许您需要在模板中使用$this->getMessagesBlock()->getGroupedHtml();