3

i have added custom drop down parameter on product detail page.

custom drop down : say with value A,B,C

Now when user adds product with value dropdown A, next time when he adds same product with value B, Product should not get updated if value of custom parameter are same then only its should get updated .

what observer should i use or else to make it works

what i did (i want cart to be updated iff value of custom_id is same)

i am able to save value of custom id in database:

 <sales_quote_add_item>
            <observers>
                <addextraid>
                    <type>singleton</type>
                    <class>Mour_Customgroup_Model_Observer</class>
                    <method>addextraparameter</method>
                </addextraid>
            </observers>
</sales_quote_add_item>

observer.php

public function addextraparameter($observer) {
    $item = $observer->getEvent()->getQuoteItem();
    $item->setCustomId(Mage::app()->getRequest()->getParam('custom_id'));
}

i am also able to get value of custom id using below code

$item->getCustomId()

please guide what needed to be done

Below is what i am trying to do

1: For product say washing machine i have added simple dropdown or say text box in my product detail page

i am able to save value of this in tables sales_flat_quote_item,sales_flat_order_item

and also get value of this parameter in cart detail page using belwo code:

$_item->getCustomId()

now problem is when user adds washing machine to cart with value of custom attribute as A , and when he add same product with option B

same product get updated to cart , But i do not want to update cart if value of custom id is not same.

Please guide

  • 2
    You're question is not very clear. Can you elaborate with actual examples perhaps instead of 'value a', 'value b'? Sounds like customers aren't allowed to add a product if they already have added one. Is that correct? – Sander Mangel Jan 8 '15 at 6:55
  • update my question – user1799722 Jan 8 '15 at 7:02
  • I assume that this custom_id is not a configurable product option or a custom option (the one magento offers by default). Is it something that you added manually? – Marius Jan 8 '15 at 7:15
  • yeah i added manually – user1799722 Jan 8 '15 at 7:16
1
+50

The method that checks if a product is already in the cart when adding a new one is Mage_Sales_Model_Quote_Item::representProduct. It returns a Boolean value.

This one checks (among a few other things) if the custom options match and if the configurable products options match.
In your case they all match because Magento has no knowledge of your custom_id field.

So the solution on this would be to rewrite this method and tell it about your new field.

Something like this (I haven't tested the code so watch out for hidden, unintentional gems)

add this in the config.xml of your Mour_Customgroup module inside the models tag.

<sales>
    <rewrite>
         <quote_item>Mour_Customgroup_Model_Quote_Item</quote_item>
    </rewrite>
</sales>

then create the file Mour/Customgroup/Model/Quote/Item.php with this content

class Mour_Customgroup_Model_Quote_Item extends Mage_Sales_Model_Quote_Item 
{
    public function representProduct($product) 
    {
        //let the original method do it's job
        $represent = parent::representProduct($product);
        if ($represent) { //if parent returns true, check your custom id...if not, then return false because the products do not match.
           return $this->getCustomId() == $product->getCustomId();
        }
        return false;
    }
}

I have my doubts about the code, but I'm pretty sure that the representProduct method is where you need to start and what you need to change.

| improve this answer | |
  • thanks it working but every time new products gets added even it value of custom_id is same , i think some changes needed to be done in function representProduct – user1799722 Jan 8 '15 at 8:09
  • Hmmm...I told you I have doubts about the code. That's all I can do for now. It involves a lot of work to reproduce your feature so I'm not able to do it. At least you have a place to start debugging. – Marius Jan 8 '15 at 8:12
  • @mour, see my solution – mageUz Jan 8 '15 at 8:47
  • "but every time new products gets added even it value of custom_id is same", @mour please clarify the question, when products should be updated and when it not should be updated? And what should be updated, item quantity or some attributes? – mageUz Jan 9 '15 at 7:52
0

See my solution:
1. Add observer for sales_quote_item_qty_set_after event:

<sales_quote_item_qty_set_after>
    <observers>
       <addextraid>
            <class>Mour_Customgroup_Model_Observer</class>
            <method>salesQuoteItemQtySetAfter</method>
       </addextraid>
     </observers>
</sales_quote_item_qty_set_after>

2.Observer method:

public function salesQuoteItemQtySetAfter($observer)
{
    /**
     * @var $item Mage_Sales_Model_Quote_Item
     */
    $item = $observer->getEvent()->getItem();
    if ($item->getId() && $item->getData('custom_id') != Mage::app()->getRequest()->getParam('custom_id')) {
        //if you want throw exception to show some message
        Mage::throwException('Please, choose the appropriate option');
        //or use following 2 lines to prevent update qty
        $item->getQuote()->setIgnoreOldQty(false);
        $item->setUseOldQty(true);
    }
}

Clarification:

If you add some product to the cart, new quote item will be created and event sales_quote_add_item will be fired. If you add this product second time sales_quote_add_item will not be fired, only qty if quote item will be changed. While changing the qty, you can observe sales_quote_item_qty_set_after event and you can prevent quantity from new value using some logic like above. I tested the solution and it works perfectly.

| improve this answer | |
  • not working ......same qty gets updated – user1799722 Jan 8 '15 at 13:44
  • @mour What should not be updated? Maybe I didn't understand your purpose. Was this code affected somehow? I am sure that if you add same product another time with different custom_id, this code prevents increasing the quantity of item. Try to debug observer, maybe it is not triggered – mageUz Jan 9 '15 at 7:48
  • hey @mageUz i have similiar issue can u help me – mdeveloper Mar 9 '16 at 14:08
-1

I think you need to create configurable product for this, You can not simply create product attribute say A, B, C etc.

You will achieve this requirement by creating configurable product. Example Washing Machine with options color red, blue, white [A, B, C in your case].

For more information check this How to create configurable product

Thanks

| improve this answer | |
  • 5
    This is not really OP is looking for and I feel OP knows how to create Configurable Products. – dh47 Jan 8 '15 at 7:12

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.