2

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
    }
}
0

You can distinguish your EXAMPLE products by adding additional options to them. It will also allow you to display to which product the EXAMPLE product belongs.
You can try below code, Note that this is just a starting code, you need to do code to update EXAMPLE product when a user updates his main cart product.

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
    $extraOptions = array(
        'label'                    => 'For',
        'option_value'             => $product->getProduct()->getName(),
        'value'                    => $product->getProduct()->getName(),
        'print_value'              => $product->getProduct()->getName(),
    );
    $value = array('for'=> $extraOptions);
    $value = serialize($value);
    $exampleItem->addOption(array('code'=> 'additional_options', 'product_id'=> $exampleItem->getProductId(), 'value'=> $value));
    $quote->save(); // save quote
}
2
  • Will that get saved somewhere in database .e.g. sales_flat_quote_item table? – rits Mar 13 '17 at 12:23
  • Yes, it will be saved in database table – Jaimin Sutariya Mar 13 '17 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.