Just another WordPress.com weblog

Hello Guys & Gals,

I like to share some code for adding free product to cart when subtotal is greater or equal to N amount (Amount is define in Backend)

You have to add two fields in system->configuration->sales section, Check the code below to add fields:

Copy file from core :: YourProject/app/code/core/Mage/sales/etc/system.xml
Paste to Local:: YourProject/app/code/Local/Mage/sales/etc/system.xml
Open file:: Search for “</groups> </sales>” Add below code snippents before the search terms.


<offers translate="label">
<label>Offer Bundle Product Id</label>
<frontend_type>text</frontend_type>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<bundle_product_id translate="label comment">
<label>Product Id Used in Offer</label>
<frontend_type>text</frontend_type>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</bundle_product_id>
<cart_subtotal translate="label comment">
<label>Cart Subtotal Used in bundle product Offer </label>
<frontend_type>text</frontend_type>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</cart_subtotal>
</fields>
</offers>

Clear cache from backend. And open System->configuration->sales:

There you can find 2 fields under “Offer Bundle Product Id” Add product id to “Bundle Product Id Used in Offer” field and subtotal amount to “Cart Subtotal Used in bundle product Offer “

After adding the above lines in system.xml open admin panel
Add below function in CartController.php

In function indexAction::search term ” foreach ($cart->getQuote()->getMessages() as $message) ” add below lines before search term

$this->addFreeProductToCart(); // This function is define below indexAction function to add free product to cart

Function defination of addFreeProductToCart

public function addFreeProductToCart()
{
$freeGiftProductId = Mage::getStoreConfig('sales/offers/bundle_product_id');
$freeGiftSubtotal = Mage::getStoreConfig('sales/offers/cart_subtotal'); //not including tax
static $lowStockWarningAmount = 5;
$freeGiftCartItemId = null;
$subtotal = 0;
if(($freeGiftProductId > 0) && ($freeGiftSubtotal >0)) {

foreach ($cart->getQuote()->getAllItems() as $item) {
if ($item->getProduct()->getId() == $freeGiftProductId) {
if ($item->getQty() > 1) {
$item->setQty(1);
$cart->save();
}
$freeGiftCartItemId = $item->getItemId();
$cart->getQuote()->setGiftCartItemId($freeGiftCartItemId);
}
}
$subtotal = $cart->getQuote()->getSubtotal();

if ($subtotal >= $freeGiftSubtotal) {
if(!$cart->getQuote()->getGiftCartItemId()){
$bundled_product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($freeGiftProductId);
if($bundled_product->isSaleable()) {

$bundled_items = array();
$bundled_options = array();
if($bundled_product->getTypeId() == 'bundle'){

$selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
$bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
);

$bundled_options['bundle_option']=array();

$i=1;

foreach($selectionCollection as $option)
{
if(sizeof($selectionCollection)>1){
$bundled_options['bundle_option'][1][$option->option_id] = $option->selection_id;
$i++;

$bundled_items[] = $option->product_id;
} else {
$bundled_options['bundle_option'][$option->option_id] = $option->selection_id;
}
}

$bundled_options['qty'] = 1;
}
$cart->addProduct($bundled_product,$bundled_options)->save();
$cart->getCheckoutSession()->addSuccess('Your free gift has been added to your cart .');
} else {
$cart->getCheckoutSession()->addError('The requested quantity for "'.$bundled_product->getName().'" is not available.');
}
}
} else {
//remove product if it is already there because the subtotal is less than the threshold
if ($freeGiftCartItemId != null) {
$cart->removeItem($freeGiftCartItemId)->save();
$cart->getQuote()->setGiftCartItemId(null);
$cart->getCheckoutSession()->addSuccess('Your free gift has been removed from your cart due to insufficient subtotal.');
}
}

}

I am very glad to share my knowledge. If you have any question you can add comment to this post.

Thanks,
Bijal Bhavsar 🙂

Advertisements

Comments on: "Adding Free bundle product to cart when subtotal is equal or greater than N amount" (4)

  1. vishal lakhani said:

    Hi Bijal I need like this that Bundle Product can be add from product listing page can you help me Please…?

  2. vishal lakhani said:

    Hi this feature i want in mini cart(Side cart which is their)

    how can i achive that

    can you help me PleasE?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: