3

As the title says. I need to be able to disable adding to cart if a product sku or shipping type already exists in the cart. Any way this can be done?

Created checkout_stop.xml in app/etc/modules with below code

<?xml version="1.0"?>
<config>
    <modules>
        <Checkout_Stop>
            <codePool>local</codePool>
            <active>true</active>
        </Checkout_Stop>
    </modules>
</config>

Created config.xml in app/code/local/Checkout/Stop/etc with below code

<?xml version="1.0"?>
<config>
<modules>
    <Checkout_Stop>
        <version>0.1.0</version>
    </Checkout_Stop>
</modules>
<global>
    <models>
        <checkout_stop>
            <class>checkout_stop_Model</class>
        </checkout_stop>
    </models>
    <events>
        <checkout_cart_product_add_after>
            <observers>
                <checkout_stop_Model_Observer>
                    <type>singleton</type>
                    <class>checkout_stop_Model_Observer</class>
                    <method>addtocartEvent</method>
                </checkout_stop_Model_Observer>
            </observers>
        </checkout_cart_product_add_after>

         <review_save_before>
            <observers>
                <checkout_stop_Model_Observer>
                    <type>singleton</type>
                    <class>checkout_stop_Model_Observer</class>
                    <method>autoApproveReview</method>
                </checkout_stop_Model_Observer>
            </observers>
        </review_save_before>
    </events>
</global>

</config>

Created Observer.php in app/code/local/Checkout/Stop/Model with below code

<?php

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();

foreach ($items as $item){
    if ($item->getSku() == 'shoes1001'){
        Mage::throwException(Mage::helper('checkout')->__('You cannot add that product to your cart');
    }
}
?> 
1
  • For single SKU in cart you can set maximum allowed quantity in cart to 1 from admin panel. What do you mean by shipping type.? Is it an attribute? Commented Mar 3, 2017 at 18:26

3 Answers 3

0

You can do this by observing the event: checkout_cart_product_add_after

So, after you've created your event observer here's what you'll do (substitute Mycompany_Mymodule with respective values):

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();

foreach ($items as $item){
    if ($item->getSku() == 'YourSKU'){
        Mage::throwException(Mage::helper('checkout')->__('You cannot add that product to your cart');
    }
}

What I'm doing is getting the users's session quote items (cart) and comparing each item to see if their cart has the SKU I don't want. If it does I throw an exception and send a message to the customer.

Edit:

First, your module is disabled. So in the etc/modules file change <active>false</active> to <active>true</active.

Take a look at your config.xml you specify a method that your event will trigger once the event occurs. In your specific example you named the method, addtocartEvent. I've added this below:

<?php

class Checkout_Stop_Model_Observer
{
    public function addtocartEvent()
    {
        $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();

        foreach ($items as $item){
            if ($item->getSku() == 'shoes1001'){
                Mage::throwException(Mage::helper('checkout')->__('You cannot add that product to your cart');
            }
        }
    }
}
?> 
30
  • Any chance you can walk me through this?
    – MHeredia
    Commented Mar 13, 2017 at 15:52
  • Where are you stuck?
    – Ryan Cady
    Commented Mar 13, 2017 at 20:00
  • I created the xml and the config.xml as well as the folders and observer. But nothing happens
    – MHeredia
    Commented Mar 13, 2017 at 20:25
  • Can you add your code to your original question? Just show us what you have so far
    – Ryan Cady
    Commented Mar 13, 2017 at 20:28
  • Just edited the code
    – MHeredia
    Commented Mar 13, 2017 at 20:35
0

The code from your question will not work. When observing checkout_cart_product_add_after the product is already added to cart/quote and exception is thrown after that.

Since there is no checkout_cart_product_add_before event, you have to remove this item from cart before throwing exception.

class Checkout_Stop_Model_Observer extends Mage_Core_Model_Observer
{
    public function addtocartEvent(Varien_Event_Observer $observer)
    {
        $quote      = $observer->getQuote();
        $product    = $observer->getProduct();

        # new product attribute 'disallowed_sku'
        $disallowed = $product->getDisallowdSku();

        if ($disallowed) {
            foreach ($quote->getAllItems() as $item) {
                if ($disallowed == $item->getProduct()->getSku()) {
                    $quote->removeItem($quote->getId());
                    Mage::throwException(
                        Mage::helper('sales')->__('You cannot add that product to your cart.')
                    );
                }
            }
        }
    }
}

The example uses a new product attribute, where you can set "disalowd" SKU from product edit page.


There are some other issues that should be fixed like in config.xml. Your model declaration should fit to your class names and use uppercase letters for the first letter of you class name words ... so you should change

<models>
    <checkout_stop>
        <class>checkout_stop_Model</class>
    </checkout_stop>
</models>

To

<models>
    <checkout_stop>
        <class>Checkout_Stop_Model</class>
    </checkout_stop>
</models>

Not required, but maybe you want to rewrite your observer from another extension, you should change the full class name to [model]/observer. (same for review_save_before)

<checkout_cart_product_add_after>
    <observers>
        <checkout_stop_Model_Observer>
            <type>singleton</type>
            <class>checkout_stop_Model_Observer</class>
            <method>addtocartEvent</method>
        </checkout_stop_Model_Observer>
    </observers>
</checkout_cart_product_add_after>

To

<checkout_cart_product_add_after>
    <observers>
        <some_unique_name>
            <class>checkout_stop/observer</class>
            <method>checkDisallowedProductSku</method>
        </some_unique_name>
    </observers>
</checkout_cart_product_add_after>
-1

You are mistaken at the file names and inside !, update your code with:

app/etc/modules/Checkout_Stop.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Checkout_Stop>
            <active>true</active>
            <codePool>local</codePool>
        </Checkout_Stop>
    </modules>
</config>

app/code/local/Checkout/Stop/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Checkout_Stop>
            <version>0.1.0</version>
        </Checkout_Stop>
    </modules>
    <global>
        <helpers>
            <checkout_stop>
                <class>Checkout_Stop_Helper</class>
            </checkout_stop>
        </helpers>
        <models>
            <checkout_stop>
                <class>Checkout_Stop_Model</class>
            </checkout_stop>
        </models>
    </global>
    <frontend>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <Checkout_Stop_Model_Observer_addafter>
                        <type>singleton</type>
                        <class>checkout_stop/observer</class>
                        <method>addtocartEvent</method>
                    </checkout_stop_Model_Observer>
                </observers>
            </checkout_cart_product_add_after>
            <review_save_before>
                <observers>
                    <Checkout_Stop_Model_Observer_savebefore>
                        <type>singleton</type>
                        <class>checkout_stop/observer</class>
                        <method>autoApproveReview</method>
                    </checkout_stop_Model_Observer>
                </observers>
            </review_save_before>
        </events>
    </frontend>
</config>

app/code/local/Checkout/Stop/Helper/Data.php

<?php
class Checkout_Stop_Helper_Data extends Mage_Core_Helper_Abstract
{

}

app/code/local/Checkout/Stop/Model/Observer.php

class Checkout_Stop_Model_Observer extends Varien_Event_Observer
{
    public function addtocartEvent($observer)

    // your logic here...
}
1
  • The courageous who downvote, explain why ! Im waiting for your explanation ?
    – PЯINCƎ
    Commented Aug 12, 2017 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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