Magento: Free gift/item shopping cart rule addon
November 17th 2014 (9 years ago)
I often work with Magento Community Edition and from time to time, there is a request for “quick solution” or something, that actually is so small, that does not need to be a module/add-on etc (plus, i really really hate having xxx additional modules, when there is simple no need for that).
This time, the goal was to create a promotion, that will give you a free gift (for my client – this was a brand new blanket with their logo) with your purchase (no other conditions, but you can always add you own).
app\code\core\Mage\SalesRule\Model\Rule.php, Line 124
app\code\core\Mage\Adminhtml\Block\Promo\Quote\Edit\Tab, Line 104
app\code\core\Mage\SalesRule\Model\Validator.php
After you have this set it up, rest is easy, code does:
This time, the goal was to create a promotion, that will give you a free gift (for my client – this was a brand new blanket with their logo) with your purchase (no other conditions, but you can always add you own).
Three choices on the road…
Important: This will work only for Magento lower than 2.0, as from 2.0 overriding/adding modules etc. is a little different To resolve this, you can:- Override SalesRule class by using local code pool: app\code\local\Mage\SalesRule (if you’re new to magento, then read this: Magento for Developers: Part 2 – The Magento Config, in short, Magento has 3 code pools, from which it will “collect” proper code to execute,app\code\local\
- Extend SalesRule class with your own module and add what you need
- Download module/plugin
For me, this would be option A.
So, to be able to give a free item (simple product, created only for this promotion) to each order, I copied and modified:app\code\core\Mage\SalesRule\Model\Rule.php, Line 124
const GET_ITEM_FREE = 'get_item_free';
app\code\core\Mage\Adminhtml\Block\Promo\Quote\Edit\Tab, Line 104
Mage_SalesRule_Model_Rule::GET_ITEM_FREE => Mage::helper('salesrule')->__('Get Item Free')
app\code\core\Mage\SalesRule\Model\Validator.php
case Mage_SalesRule_Model_Rule::GET_BLANKET_FREE: // use $promoImage if you want to have additional image to Promo item, for example: a gift icon. //$promoImage = '<br><img src="'.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN).'\frontend\default\default\images\YOUR_IMAGE.jpg" alt="Promo" title="Promo">'; $product = Mage::getModel('catalog/product')->loadByAttribute('sku', "[YOUR_SKU_HERE]"); //simple product only $productId = $product ->getId(); $productSku = $product ->getSku(); Mage::getModel('cataloginventory/stock_item')->assignProduct($product); $quoteItem = Mage::getModel('sales/quote_item')->setProduct($product); $quoteItem->setQuote($quote) ->setQty(1) ->setCustomPrice(0.0) //->setName($product ->getName().$promoImage) //USE THIS with $promoImage ->setTaxAmount(0.0) ->setTaxPercent(0.0) ->setOriginalCustomPrice(0.0) ->setWeeeTaxApplied('a:0:{}') // this will remove warnings when generating invoices ->setStoreId($storeId); if ($rule->getIsApplied()) { return; } else { if ($quote->hasProductId($productId)) { foreach ($quote->getAllItems() as $item) { if ($item->getSku() == $productSku) { $quote->removeItem($item->getId()); } } } else { } if (count($quote->getAllItems()) > 0) { $quote->addItem($quoteItem); $quoteItem->setApplyingRule($rule); $rule->setIsApplied(true); } } break;
How This works?
First of all, in Admin area, in Promotions -> Shopping Cart Price Rules You have additional option “Get Item Free”, where you can choose additional conditions for you cart rule.After you have this set it up, rest is easy, code does:
- check if there are already items with the same SKU as free item, if yes – remove item(s) from cart
- Add a free item with “0.0” price to cart
- set the rule to “applied” to prevent further adding the same item multiple times