I have a product created in backend that will always have SKU 'EXAMPLE'. I add this product in a cart_product_add_after event observer whenever normally a product gets added to a cart.
This 'EXAMPLE' product should belong to product that got added to the cart, but currently if I add, for example, 5x product A and 3x product B then I got 8x of SKU 'EXAMPLE' in cart.
What I need in that situation is 5x of example product that belongs to product A and 3x of example product that belongs to product B.
Main idea is that the SKU 'EXAMPLE' product should belong to the product that gets added to cart via the "Add to Cart" button. It should be reflected in cart view by splitting them and not having all of them stacked in one quantity.
How cart should look:
5x Product A _________________ 20$ ____ Total 100$
5x Example For Product A ____ 10$ ____ Total 50$
3x Product B _________________ 30$ ____ Total 90$
3x Example For Product B ____ 10$ ____ Total 30$
How cart currently looks:
5x Product A ______ 20$ ____ Total 100$
8x Example _______ 10$ ____ Total 80$
3x Product B ______ 30$ ____ Total 90$
Example Obeserver.php:
class Test_Example_Model_Observer
{
public function cartProductAddAfter($observer)
{
$product = $observer->getEvent()->getQuoteItem(); // get product that gets added to cart
$exampleQty = $product->getQtyToAdd(); // get quantity of added product
$exampleProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', 'EXAMPLE'); // get the example product
$exampleToAdd = $exampleProduct->load($exampleProduct->getId()); // load the example product
$quote = Mage::getSingleton('checkout/session')->getQuote(); // get quote
$exampleItem = $quote->addProduct($exampleToAdd, $exampleQty); // add example product to quote matching quantity of the normal product
$quote->save(); // save quote
}
}