I want to add Y free product to cart if user buy X product.
I have changed code of Mage_Sales_Model_Quote
public function addProductAdvanced(Mage_Catalog_Model_Product $product, $request = null, $processMode = null){
.............
............
$specialPrice = 0;
$item->setCustomPrice($specialPrice);
$item->setOriginalCustomPrice($specialPrice);
$item->getProduct()->setIsSuperMode(true);
...............
..............
}
it's work fine.
but when user add Y product from product page it's add with price 0.
but i want to add only one qty. with 0 and other with regular price.
Please help me.
Best Answer
first of all: prefer events to rewrites, prefer rewrites to monkey patches and never touch the core unless you are a Magento core developer :)
So said, let's decompose the problem into two sub problems:
Identify the event
The most appropriate event is
sales_quote_collect_totals_before
because it is called not only when an item is manually added by the user to the cart but also when the item is added in a programmatic manner, for example when the user logs in and the quote merge occurs.We should pay attention to the fact that this event is called not only when adding a product to cart, so we should keep track of the fact that we already added the gift.
Differentiate quote items
How Magento determines whether two quote items are the same?
product_id
parent_id
(if any)As far as I understand from your description, the free Y and the non-free Y have the same
product_id
and are simple products (so they don't have aparent_id
).Thus we will differentiate them by adding dynamic custom options to the quote item representing the gift.
Implementation steps
Here are the steps needed to implement our solution.
Activate a custom module
Create the following
{magento_install_dir}/app/etc/modules/Yourname_Yourmodule.xml
file:Note: substitute
Yourname
with your vendor name andYourmodule
with your module name.Note 2: if you intend to distribute the extension prefer the
community
code pool.Define the module configuration
Create the following
{magento_install_dir}/app/code/local/Yourname/Yourmodule/etc/config.xml
file:Note 1: I'm observing the event only in frontend area; if the store manager creates an order in the Admin Panel, our logic won't be applied.
Note 2: I'm using Magento 2 convention for observers: a class associated to each event whose name ends with
Observer
suffix and with a single responsibility performed by theexecute()
method.Note 3: even if I'm not providing
system.xml
for module configuration I set module configuration default values through the<default>
XML node.Implement the helper
Our helper is a simple gateway towards system configuration.
Create the following
{magento_install_dir}/app/code/local/Yourname/Yourmodule/Helper/Data.php
file:Implement the observer
Here are the logical steps of our observer:
Create the following
{magento_install_dir}/app/code/local/Yourname/Yourmodule/Model/Observer/AddFreeProductObserver.php
file:Conclusion
I've not tested this solution too much, consider it a guiding example and do your tests to be sure it works the way you desire.
Hope it helps.