0

I have a module that adds a free product to the cart automatically if a product exists.

The issue:

  1. If the customer keeps shopping and adds other products. The quantity for the free automatic product increases to quantity of 2.

What I need:

  1. I need the quantity of the free automatic product restricted to 1. I've tried to code this but I'm unsuccessful.

I can't figure out how to say, If productID is 222 then change quantity to 1 if > 1. Can someone help?

Here's the code:

public function addSubAction($observer)
{
    $productId = 123;
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    foreach ($quote->getAllVisibleItems() as $item) {
        if ($item->getData('product_id') == $productId) {
            $cart     = Mage::getSingleton('checkout/cart');
            $addprod  = 222;
            $qty      = 1;
            $quote    =  Mage::getSingleton('checkout/session')->getQuote()
                ->addProduct( Mage::getModel('catalog/product')->load($addprod), $qty)
                ->save();
            $cart->save();
        }
    }
}
0

If I understand it right, it's same question as this: Add new item to order automatically


Edit:

Hope I got it ... ;)

  • 1st product is the one that gives a free product, your SKU 123
  • 2nd is the free one, SKU 222, that should be added only one time and only when product 123 is in cart
  • additionally 222 is removed, when you remove 123 from cart

Some code I use for a similiar function ...

  1. add an observer to sales_quote_item_set_product, to add product 222 when 123 is added ...

    public function addFreeProduct(Varien_Event_Observer $observer)
    {
        $product = $observer->getProduct();
        if ($product->getSku() == '123') {
            $added = false;
            $quote = $observer->getQuoteItem();
            foreach ($quote->getQuote()->getAllVisibleItems() as $item) {
                # check if free product is already added
                if ($item->getProduct()->getSku() == '222') {
                    $added = true;
                    break;
                }
            }
    
            # add free product once
            if (!$added) {
                $addProduct = Mage::getModel('catalog/product');
                $addProduct->load($addProduct->getIdBySku('222'));
                $quote->getQuote()->addProduct($addProduct);
            }
        }
    }
    
  2. add an observer to sales_quote_remove_item to remove free product when last product 123 is removed from cart ...

    public function removeFreeProduct(Varien_Event_Observer $observer)
    {
        $quoteItem = $observer->getQuoteItem();
        if ($quoteItem->getProduct()->getSku() === '123') {
            $quote = Mage::getSingleton('checkout/session')->getQuote();
    
            $freeProduct = Mage::getModel('catalog/product');
            $freeProduct->load($freeProduct->getIdBySku('222'));
    
            $addItem = $quote->getItemByProduct($freeProduct);
            if ($addItem) {
                $newQty = $addItem->getQty() - $quoteItem->getQty();
                if ($newQty == 0) {
                    $quote->removeItem($addItem->getId());
                    $quote->save();
                }
            }
        }
    }
    
  • Thanks for answering. I actually have the part of adding the product and that part works. The problem is if I add other products to the cart it increases the free product quantity by 1. So in the cart you will see product quantity of 2 for the free product instead of 1. This happens because I left the cart and added another product. I hope that makes sense. – Brad Mar 22 '18 at 16:57
  • @ Brad depends the free product on another SKU? (Or should it always be added?) – sv3n Mar 22 '18 at 17:19
  • It should always be added. If $productId = 123; is added then $addprod = 222; is added with a quantity of 1. But if you go else where and add another product and go back to the cart the free product (222) has a quantity of 2. So I need to check to see if it's already added to the cart IF so then reduce the quantity of 1. – Brad Mar 22 '18 at 17:22
  • @brad then this code should work ... free-product will be added with the first item you add to cart. If the free-product depends on productId 123 e.g., you just add another check ... – sv3n Mar 22 '18 at 17:29
  • I can't figure out what that check is. That's the question. In the code I posted I can't figure how how to check it and change it to 1 if it's greater than 1. Can you tell me how in the code I posted how to check? I'm new at this. – Brad Mar 22 '18 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.