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

Php 自定义模块添加到购物车成功消息Magento,php,magento,Php,Magento,在我的自定义模块中,我想显示添加到购物车成功消息。 我的.phtml文件中有以下代码: echo '<form action="/checkout/cart/add/product/'.$_product->getId().'/" method="get">'."\n"; $_attributes = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray( $_product ); ec

在我的自定义模块中,我想显示添加到购物车成功消息。 我的.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";
echo“”。“\n”;
$\u attributes=$\u product->getTypeInstance(true)->getConfigurableAttributeSarray($\u product);
回显“”。“\n”;
$\u child\u products=Mage::getModel('catalog/product\u type\u configurable')->getUsedProducts(null,$\u product);
foreach($\子产品作为$\子产品):
如果($\子产品->可销售()):
$\u child\u product\u qty=Mage::getModel('cataloginventory/stock\u item')->loadByProduct($\u child\u product->getId())->getQty();
如果($\子项\产品\数量>0):
$\u child\u product\u size\u label=$\u child\u product->getResource()->getAttribute('size')->getFrontend()->getValue($\u child\u product);
$\u child\u product\u size\u val=Mage::getResourceModel('catalog/product')->getAttributeRawValue($\u child\u product->getId(),'size',Mage::app()->getStore()->getId());
回显“.$\u子项\u产品\u尺寸\u标签”。“\n”;
endif;
endif;
endforeach;
回显“”。“\n”;
回显“”。“\n”;
回显“”。“\n”;
回显“”。“\n”;
回显“.$this->(立即购买)”。“\n”;
回显“”。“\n”;
回显“”。“\n”;
按Buy Now,它会将产品添加到购物车中,但我想在顶部显示绿色成功消息

已编辑

我被建议调用以下函数:

function showMessage(txt, type) {
    var html = '<ul class="messages"><li class="'+type+'-msg"><ul><li>' + txt + '</li></ul></li></ul>';
    $('messages').update(html);
}
函数显示消息(txt,类型){
var html='
    • '+txt+'
          • ; $('messages').update(html); }
但是考虑到我的形式,我不确定从哪里调用这个函数。我有:

echo '<button class="button btn-cart"><span>'.$this->__('Buy Now').'</span></button>'."\n"; 
echo'.$this->(“立即购买”)。“\n”;

按下按钮后,它将提交带有action get的表单。

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

Mage::getSingleton(‘customer/session’)->addMessage("Your message");
而且,这对你来说可能很有趣


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

我认为这段代码可以帮助您:

Mage::getSingleton(‘customer/session’)->addMessage("Your message");
而且,这对你来说可能很有趣


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

Magento在产品页面上完成此操作。我在自定义模块中执行此操作。它会将文件添加到购物车中,并停留在同一页面上,但不会显示成功消息。我不确定哪一行需要从产品页复制,或者需要额外的代码。Magento在产品页上执行。我在自定义模块中执行此操作。它会将文件添加到购物车中,并停留在同一页面上,但不会显示成功消息。我不确定哪一行需要从产品页复制,或者需要额外的代码。我认为,如果用户未登录,这将不会呈现,但应该进行验证。它将呈现它。但是一旦消息显示在一个页面上,它就会从消息队列中删除。我仍然迷路了。。。这应该放在哪里?我想你不需要使用javascipt。只需使用
Mage::getSingleton('customer/session')->addMessage()
将消息放入队列的任何位置(控制器、模型、模板可能适用)。然后放置
$this->getMessagesBlock()->getGroupedHtml()在模板(.phtml)中,您要在其中输出队列中的消息。我的表单如下所示:。我应该把Mage::getSingleton('customer/session')->addMessage()放在哪里,以便它在单击按钮时添加消息?我认为如果用户未登录,这将不会呈现,但应该进行验证。它将呈现消息。但是一旦消息显示在一个页面上,它就会从消息队列中删除。我仍然迷路了。。。这应该放在哪里?我想你不需要使用javascipt。只需使用
Mage::getSingleton('customer/session')->addMessage()
将消息放入队列的任何位置(控制器、模型、模板可能适用)。然后放置
$this->getMessagesBlock()->getGroupedHtml()在模板(.phtml)中,您要在其中输出队列中的消息。我的表单如下所示:。我应该把Mage::getSingleton('customer/session')->addMessage()放在哪里,以便它在单击按钮时添加消息?