[英]Custom module add to cart success message Magento
In my custom module I want to show the add to cart success message. 在我的自定义模块中,我想显示添加到购物车成功消息。 I have following piece of code in my .phtml file: 我的.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";
On pressing Buy Now, it adds the product to the cart but I want to show the green success message on top as well. 按立即购买时,它将产品添加到购物车,但我也想在顶部显示绿色成功消息。
EDITED 已编辑
I was advised to call the following function: 建议我调用以下函数:
function showMessage(txt, type) {
var html = '<ul class="messages"><li class="'+type+'-msg"><ul><li>' + txt + '</li></ul></li></ul>';
$('messages').update(html);
}
But considering my form, I am not sure where to call this function from. 但是考虑到我的表单,我不确定从哪里调用此函数。 I have: 我有:
echo '<button class="button btn-cart"><span>'.$this->__('Buy Now').'</span></button>'."\n";
and on pressing the button it submits the form with action get. 并在按下按钮时提交带有动作get的表单。
I think this code could help you: 我认为这段代码可以帮助您:
Mage::getSingleton(‘customer/session’)->addMessage("Your message");
Also, this answer could be interesting for you. 另外,这个答案对您来说可能很有趣。
Maybe you need to use $this->getMessagesBlock()->getGroupedHtml();
也许您需要使用$this->getMessagesBlock()->getGroupedHtml();
in your template. 在您的模板中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.