code4business/freeproduct — pull request #2 by C4rter (2014-05-23 15:00:14+00:00)
Title: Add to Cart Problem and WeeeTax Fix

Hi Nikolai,

I'm using your extension in one project and I stumbled upon two issues.

The first one was a "warning" in the system.log, every time I created an Invoice for an order that contained a "free item".
Warning: Invalid argument supplied for foreach() in app/code/core/Mage/Sales/Model/Order/Item.php on line 843

The reason for this was, that the free product was missing the value "setWeeeTaxApplied". The field was "null" which resulted in a "foreach" that tried to loop this "null" value inside Magento core. I set this field to the default value now, during the creation of the free product, which seems to fix that. (2930212)

The second one was a little bit more tricky:
Once a customer received the free product, he wasn't able to go to the product detail page of the product and add the product to the cart again. Instead he received an error, saying that the product could not be added to the cart. That prevented customers to buy the product they received for free again, but for money this time.

At first I thought this was a feature of the freeproduct extension, but the strange thing was that it worked the other way around: If I added the product that was configured to be free before I received the actual free product, I had both products in my shopping cart, one for free and one for the correct price.

In addition I noticed an exception in the log, every time a customer tried to add the free product to the cart manually:
exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mysqluser`.`sales_flat_quote_item_option`, CONSTRAINT `FK_5F20E478CA64B689
1EA8A9D6C2735739` FOREIGN KEY (`item_id`) REFERENCES `sales_flat_quote_item` (`item_id`) ON DELETE CASCADE ON UPD)' in lib/Zend/Db/Statement/Pdo.php:228

That got me thinking, that this can't be a desired feature. I started analysing the problem and noticed, that during the "add to cart" of a product, Magento checks if the specific product already is in the cart.
If the free product already is in the cart, this function will say that the product I'm trying to add and the free product that's already in the cart are the same, which they are not. The free product is of a far simpler nature than the product Magento adds the usual way. That's why the add to cart breaks with the mysql exception.

So I tried to teach Magento, that a "free product" is never the same as any other product I try to add. I tried many approaches that did not use a rewrite, none of which were without other consequences, what's the reason that I had to use a rewrite to get it done.
I used sort of a "intelligent rewrite" that still uses the core functionality (parent function) and only adds other logic if there is any need. (d6b72b5)

With this change, the customer is able to do both, get the free item and buy this product again for the normal price.

Kind regards,
Benedikt