2012-03-16 80 views
1

在我的自定義模塊中,我想顯示添加到購物車成功消息。 我已經按照我的一個.phtml文件的代碼:自定義模塊添加到購物車成功消息Magento

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。

+0

Magento已經做到了這一點?沒有? – Alexandre 2012-03-16 13:09:03

+0

Magento在產品頁面上做到這一點。我在我的自定義模塊中執行此操作。它將文件添加到購物車並保持在同一頁面上,但不顯示成功消息。我不確定哪一行需要從產品頁複製,或需要額外的代碼。 – Hum 2012-03-16 13:13:43

回答

1

我覺得這個代碼可以幫助你:

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

此外,這answer可能是你感興趣的。

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

+1

我認爲如果用戶未登錄但不應呈現此內容,但應驗證該內容。 – benmarks 2012-03-16 13:32:09

+0

我認爲@Ben是對的,所以我補充了一些說明。 – alphacentauri 2012-03-16 13:39:40

+0

它會渲染它。但是,只要消息在一個頁面上顯示,它就會從消息隊列中刪除。 – Alexandre 2012-03-16 14:13:59

相關問題