Question

I have some problem with removing product from quote.

I observe "Add to cart" with "checkout_cart_product_add_after" event and there simply do this:

$quote->removeItem($quoteItem->getEntityId())->save();

Problem is if this product is first which is added to cart. Otherwise works fine.

Scenario: I am on category page chose a product which I want to buy. I click "Add to cart". In this moment I am checking if product can be in cart or must be removed (when product must be removed is depended of some attributes on product). Let me say, that this product shouldn' be in the cart so I am trying to remove it. But I can't. But If I add exacty same product again, it will not be added (it will be removed). I know that because qty of product doesn't increase.

Am I using wrong event? Can I maybe against removing product from cart, prevent product to be added to cart? Can anybody help me with this?

Tnx

Was it helpful?

Solution

Taking into consideration your scenario, yes, it is better to prevent the product being added to cart, instead of adding it and removing it right after.

There are a number of ways to do this, but I think the simplest is this - create an observer on the controller_action_predispatch_checkout_cart_add event and inside the observer class do the following:

public function rejectProductToCart(Varien_Event_Observer $observer)
{
   // retrieve the product
    $product =  Mage::app()->getRequest()->getParam('product');

    if (/* add your conditions and see if the current product should be skipped */) {
       // set the URL to return the client to
       $returnUrl = $this->getRequest()->getParam('return_url');

       // set a custom session message to inform the client
       Mage::getSingleton('checkout/session')->addError(Mage::helper('checkout')->__("We're sorry, this product cannot be added to the cart."));

       // skip dispatching the controller action for addition to cart
       $observer->getControllerAction()->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
       Mage::app()->getResponse()->setRedirect($returnUrl);     
    }

    return $this;
}

OTHER TIPS

Diana answer help me a lot, but I found out where was a problem and also found solution on deleting/removing item from quote.

If you use observer like I did "checkout_cart_product_add_after" you sholudn't use

$quote->removeItem($quoteItem->getEntityId())->save();

It is better to use

$quote->deleteItem($quoteItem)->save();

I think that first command did't work beacuse when this event is triggerd, there is no item saved on the quote. So item can't be removed.

On the other hand delete from quote work fine because you deleting item from class property $_items or directy from quote.

public function deleteItem(Mage_Sales_Model_Quote_Item $item)
{
    if ($item->getId()) {
        $this->removeItem($item->getId());
    } else {
        $quoteItems = $this->getItemsCollection();
        $items = array($item);
        if ($item->getHasChildren()) {
            foreach ($item->getChildren() as $child) {
                $items[] = $child;
            }
        }
        foreach ($quoteItems as $key => $quoteItem) {
            foreach ($items as $item) {
                if ($quoteItem->compare($item)) {
                    $quoteItems->removeItemByKey($key);
                }
            }
        }
    }

    return $this;
}

It works for me. Diana thanks again and I hope it will help someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top