Search code examples
phpmagentomodulecartobservers

Magento - add free child product to cart


I have been attempting to get a product (simple product), which is selected by the customer, to be added to cart and the price changed to free. At present I have this working with the exception of two requirements:

Adding as child to main product in cart The product is being add to the cart, but as an additional product rather than a child to the main product that was added. I've tried various methods with no luck, here is the current code:

$product = $event->getProduct();
$product_gift = $this->_initProduct($gift); // function returns product based on ID
$params = array(
    'product' => $product_gift->getId(),
    'qty' => 1
);

$request = new Varien_Object();
$request->setData($params);

$product_gift->addCustomOption('child_product', 1);
$cart->addProduct($product_gift, $request);
$cart->save();

Changing price to zero This has not worked at all, but here is what I have based on reading online.

$product_gift->setFinalPrice( 0 );
$product_gift->setCustomPrice( 0 );
$product_gift->setPrice( 0 );
$product_gift->setOriginalPrice( 0 );
$product_gift->setPriceCalculation( 0 );
$product_gift->setSpecialPrice( 0 );

The observer is working with the exception of the two points highlighted above. Here is the full observer code:

class Magestore_Promotionalgiftcustom_Model_Observer
{
    protected function _initProduct($productId)
    {
        if ($productId) 
        {
            $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);
            if ($product->getId()) 
            {
                return $product;
            }
         }
        return false;
    }

    public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
    {
        if(!Mage::helper('promotionalgiftcustom')->enablePromotionalgiftcustom()){
            return;
        }

        $event = $observer->getEvent();  //Gets the event
        $product = $event->getProduct();

        $params = Mage::app()->getRequest()->getParams();
        $cart = Mage::getSingleton('checkout/cart');
        $added = array();

        if( Mage::getModel('promotionalgift/catalogrule')->validateItem( $product->getId() ) ){

            if(isset($params['gifts'])){

                foreach ($params['gifts'] as $gift) {     

                    if(!in_array($product->getId(), $added)) {
                        $product_gift = $this->_initProduct($gift);

                        if ($product_gift->getTypeId() == 'simple') {
                            if (!$product_gift->isSaleable()) {
                                continue;
                            }

                            $qty_4gift = 1;
                            $product_gift->setFinalPrice( 0 );
                            $product_gift->setCustomPrice( 0 );
                            $product_gift->setPrice( 0 );
                            $product_gift->setOriginalPrice( 0 );
                            $product_gift->setPriceCalculation( 0 );
                            $product_gift->setSpecialPrice( 0 );

                            $params = array(
                                'product' => $product_gift->getId(),
                                'qty' => $qty_4gift
                            );
                            $request = new Varien_Object();
                            $request->setData($params);
                            $cart->addProduct($product_gift, $request);
                            $cart->save();

                            Mage::getSingleton('checkout/session')->setCartWasUpdated(false);

                        }
                        $added = $product->getId();
                    }
                }
            }
        }
    }
}

I managed to get the price changing to zero with just these two methods:

$product_gift->setPrice(0);
$product_gift->setFinalPrice(0);

Solution

  • After many hours testing and hacking away at this I finally got it working. Here is the solution for anyone interested:

    protected function _initProduct( $productId ) {
        if ( $productId ) {
            $product = Mage::getModel( 'catalog/product' );
            $product->load( $productId );
            if ( $product->getId() ) {
                return $product;
            }
        }
        return false;
    }
    
    public function checkoutCartProductAddAfter( Varien_Event_Observer $observer ) {
    
        if ( ! Mage::helper( 'promotionalgiftcustom' )->enablePromotionalgiftcustom() ) {
            return;
        }
    
        $event   = $observer->getEvent();
        $product = $event->getProduct();
        $quote_item = $event->getQuoteItem();
    
        $params = Mage::app()->getRequest()->getParams();
        $cart   = Mage::getSingleton( 'checkout/cart' );
    
        if ( Mage::getModel( 'promotionalgift/catalogrule' )->validateItem( $product->getId() ) ) {
    
            $cart->save();
    
            if ( isset( $params['gifts'] ) ) {
    
                foreach ( $params['gifts'] as $gift ) {
    
                    $product_gift = $this->_initProduct( $gift );
    
                    if ( $product_gift->getTypeId() == 'simple' ) {
                        if ( ! $product_gift->isSaleable() ) {
                            continue;
                        }
    
                        $qty_4gift = 1;
    
                        $params  = array(
                            'product'           => $product_gift->getId(),
                            'qty'               => $qty_4gift,
                            'parent_product_id' => $product->getId()
                        );
    
                        $request = new Varien_Object();
                        $request->setData( $params );
                        $cart->addProduct( $product_gift, $request );
                    }
                }
    
                $cart->save();
    
                Mage::getSingleton( 'checkout/session' )->setCartWasUpdated( false );
            }
        }
    
        if($quote_item->getOptionByCode('info_buyRequest')){
            $info_buyRequest = unserialize($quote_item->getOptionByCode('info_buyRequest')->getValue());
            if($info_buyRequest['parent_product_id']){
                $customPrice = 0;
                $quote_item->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice);
                $quote_item->save();
            }
        }
    
    }