문제

I'm trying to remove the notice in the cart about how the customer must have a subtotal of say $20 in their cart however I still want the restriction there for when the customer tries to go into the cart (I personally think we shouldn't remove the notice but the order comes down from up high)

At the moment, this is my code

$messages = $this->getMessagesBlock()->getMessages();
unset($messages[1]);
$this->getMessagesBlock()->setMessages($messages);
echo $this->getMessagesBlock()->getGroupedHtml();

I've been testing the code by going into the cart with 1 item and using the update cart button when they new qty is something stupid like 99999999999999

from the var dump i did of $this->getMessagesBlock() there is a protected piece of data called _messages, using getMessages() return an array of 2 items, one is the error about the qty at index 0, the other is an notice about the minimum purchase.

I tried using unset($messages[1]); to remove the notice as a test to see if I could remove it. If it worked, I would later fix it up to search for the notice and remove it no matter where in the array it is.

I called setMessages($messages) and did another var dump. I can see it's been put back into $this->getMessagesBlock() so i thought it would work, however nothing appeared.

I did a var_dump of $this->getMessagesBlock()->getGroupedHtml() and it shows an empty string.

I'm wondering what i could be doing wrong here or if there is a better way to just remove the message i don't want.


this is what is vardumped from $messages

array (size=2)
  0 => 
    object(Mage_Core_Model_Message_Error)[196]
      protected '_type' => string 'error' (length=5)
      protected '_code' => string 'The requested quantity for "6899WSAND" is not available.' (length=56)
      protected '_class' => string '' (length=0)
      protected '_method' => string '' (length=0)
      protected '_identifier' => null
      protected '_isSticky' => boolean false
  1 => 
    object(Mage_Core_Model_Message_Notice)[820]
      protected '_type' => string 'notice' (length=6)
      protected '_code' => string '' (length=0)
      protected '_class' => string '' (length=0)
      protected '_method' => string '' (length=0)
      protected '_identifier' => null
      protected '_isSticky' => boolean false
도움이 되었습니까?

해결책

I would try to solve this on another level. The notice for not reaching the minimum order amount is added fairly early on in Mage_Checkout_CartController.

One of the next events that is dispatched is controller_action_layout_load_before. Create an observer for this event and in there you can use something like the following:

$messages = Mage::getSingleton('checkout/session')->getMessages(true);

The true will clear out all messages for the checkout so this is not quite what you want so we need to re-add the ones we don't want to filter out back in.

Unfortunately Magento does not give the cart notice any unique identifier (which would allow you to use Mage_Core_Model_Message_Collection::deleteMessageByIdentifier). The distinction between the cart and the checkout is that one is a notice and the other is an error message. Something like the following should provide you with a good starting point.

$messageTypes =  array(
    Mage_Core_Model_Message::ERROR,
    Mage_Core_Model_Message::NOTICE,
    Mage_Core_Model_Message::SUCCESS,
    Mage_Core_Model_Message::WARNING
);
//Sidenote just because something is a collection in Magento it does not mean you
//can easily loop over it - see Mage_Core_Model_Message_Collection
foreach ($messageTypes as $type){
    foreach ($messages->getItems($type) as $typeMessages) {
        foreach ($typeMessages as $message){
            if (!($type == Mage_Core_Model_Message::NOTICE
                && $message == Mage::getStoreConfig('sales/minimum_order/description'))
            ) {
                $messageObject = Mage::getModel('core/message_'.$type, $message);
                Mage::getSingleton('checkout/session')->addMessage($messageObject);
            }
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top