1
votes

I have written a buy one get one free module for Magento which works perfectly in Magento 1.3.x.

It extends "Mage_Checkout_CartController" to add the free (simple) product to the cart when the qualifying logic is met. I have modified the "indexAction()" function in the extended "CartController.php" and added the extra code and logic to add the free item which is basically added using

$cart->addProduct($FREPRODUCTID, 1);
$cart->save();
$this->_getSession()->setCartWasUpdated(true);

This works fine in Magento 1.3.x (CE) however in 1.4 and 1.5 what I am seeing is that the free item is added to the cart but the cart total does not update to include the product added by my code, and at checkout the product is not seen. Although the product is free, has a zero price configured, I also notice that when I give the product a price, i.e. $1 the product still shows up with a zero price in the cart as though its simply not recognized. The free product is correctly configured and is saleable, in stock etc.

Any suggestions as to why my product is being added to the cart but somehow is not being correctly seen by the system in Magento 1.4/1.5?

Thanks

PAJ

2

2 Answers

1
votes

Try with this code & test it:-

$product = Mage::getModel('catalog/product');
$product->setStoreId(Mage::app()->getStore()->getId());
$product->load($FREPRODUCTID);

$cart = Mage::getSingleton('checkout/cart');
$cart->init();

$cartItems = $cart->getQuote()->getAllItems();
$counter = 1;
$cartData = array();
if (count($cartItems)) {
    foreach ($cartItems as $_eachItem) {
        $cartData[$counter]['qty'] = $_eachItem->getQty();
        $counter++;
    }
}

$freeProductQty = 1;
$cart->addProduct($product, array('qty' => $freeProductQty, 'product' => $FREPRODUCTID));
$cartData[$counter]['qty'] = $freeProductQty;

$cartData = $cart->suggestItemsQty($cartData);
$cart->updateItems($cartData)
     ->save();

$this->_getSession()->setCartWasUpdated(true);

In the method "addProduct()", I have specifically mentioned the required product object (which is to be added to the Cart) as the first parameter & the array with the quantity of that product as the second parameter.

Hope it helps.

0
votes

This should make sure the cart is properly updated :

        session_write_close();
        $this->_redirect('checkout/cart');

When I add this to my extended cartcontroller functions after my product add and cart save then the cart renders my added product correctly.