Lexperts
Lexperts

Reputation: 353

Add extra item to the cart (observer)

I try to add a extra product to the cart. I have created a observer for this.

<?php
class WP_Plugadapter_Model_Observer
{

    public function hookToControllerActionPostDispatch($observer)
    {
        if($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add')
        {
            Mage::dispatchEvent("add_to_cart_after", array('request' => $observer->getControllerAction()->getRequest()));
        }
    }

    public function hookToAddToCartAfter($observer)
    {

        $request                = $observer->getEvent()->getRequest()->getParams();
        $_product               = Mage::getModel('catalog/product')->load($request['product']);
        $extra_functions        = $_product->getExtra_functions();

        if(!empty($extra_functions)){

            $extra_functions    = explode(',', $extra_functions);
            if(array_search('121', $extra_functions)){

                $cart           = Mage::getSingleton('checkout/cart'); 
                $cart->addProduct(10934, 1);
                $cart->save();

                if (!$cart->getQuote()->getHasError()){
                   Mage::log("Product ADD TO CART is added to cart.");
                }else{
                   Mage::log("BOEM");
                }

            }  

        }

    }

}

When i check mine system log i see the following log message. Product ADD TO CART is added to cart.

I have no clue what i'm doing wrong. When a load the script standalone it's working fine. For example:

<?php

include_once '../app/Mage.php';

Mage::app();
umask(0);

$session        = Mage::getSingleton('core/session', array('name'=>'frontend'));        
$cart           = Mage::getSingleton('checkout/cart'); 
$cart->addProduct(10934, 1);
$cart->save();

Is it possible that in a observer you have it to do it in a different way?

Upvotes: 2

Views: 2780

Answers (1)

Oleg Ishenko
Oleg Ishenko

Reputation: 2233

The problem is that cart's quote object is not saved to the database and later in the request processing is overwritten by the quote object from the session. Why the cart quote is not saved is quite confusing. The save method of the quote model expects that the internal property _hasDataChanges is set to true. This property is, however, remains at false, even though a product was added to the quote.

You can force that property to be set to true by adding some data (any property would do) to the quote using the setData method:

                $cart = Mage::getSingleton('checkout/cart'); 
                $cart->addProduct(10934, 1);
                //force _hasDataChanges to true
                $cart->getQuote()->setData('updated', true);
                $cart->save();

Alternatively you can use the checkout session quote object to add a product to the cart

 if(array_search('121', $extra_functions)){

            $cart           = Mage::getSingleton('checkout/cart'); 
            $qty = 1;
            $quote =  Mage::getSingleton('checkout/session')->getQuote()
                ->addProduct(
                    Mage::getModel('catalog/product')->load(10934),
                    $qty)
                ->save();
            $cart->save();

            if (!$cart->getQuote()->getHasError()){
               Mage::log("Product ADD TO CART is added to cart.");
            }else{
               Mage::log("BOEM");
            }

        } 

Upvotes: 5

Related Questions