5

How can i set a condition, if pre-ordered products are purchased, no other products can be added to cart?

How can I set a condition like, if some of the "non-pre-order" products is in the cart, not to be able to add a "pre-order" product. Visa verse if my cart consists "pre-order" products, not allow "non-pre-order" products to be added?

How can I do that?

5
+150

You can use checkout_cart_product_add_after event to allow or disallow products being added ...

app/code/local/My/Module/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <My_Module>
            <version>0.0.1</version>
        </My_Module>
    </modules>
    <global>
        <models>
            <my_module>
                <class>My_Module_Model</class>
            </my_module>
        </models>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <test>
                        <class>my_module/observer</class>
                        <method>disallowProductAddToCart</method>
                    </test>
                </observers>
            </checkout_cart_product_add_after>
        </events>
        <!-- make custom attrribute available in quote item product -->
        <sales>
            <quote>
                <item>
                    <product_attributes>
                        <preorder />
                    </product_attributes>
                </item>
            </quote>
        </sales>
    </global>
</config>

app/code/local/My/Module/Model/Observer.php

class My_Module_Model_Observer extends Mage_Core_Model_Observer
{
    public function disallowProductAddToCart(Varien_Event_Observer $observer)
    {
        $quote = $observer->getQuoteItem();
        $cartItems = $quote->getQuote()->getAllItems();

        if (count($cartItems) > 1) {
            $product = $observer->getProduct();
            $isPreorder = $product->getPreorder();

            if ($isPreorder) {
                $quote->getQuote()->removeItem($quote->getId());
                Mage::throwException(
                    Mage::helper('sales')->__('Can not add pre-order product to existing cart.')
                );
            }

            foreach ($cartItems as $item) {
                if (!$isPreorder && $item->getProduct()->getPreorder()) {
                    $quote->getQuote()->removeItem($quote->getId());
                    Mage::throwException(
                        Mage::helper('sales')->__('Cart already contains an pre-ordered product.')
                    );
                    break;
                }
            }
        }
    }
}

Edit: allow multiple "pre-order" products in cart

public function disallowProductAddToCart(Varien_Event_Observer $observer)
{
    $quote = $observer->getQuoteItem();
    $cartItems = $quote->getQuote()->getAllItems();

    if (count($cartItems) > 1) {
        $product = $observer->getProduct();
        $isPreorder = $product->getPreorder();

        foreach ($cartItems as $item) {
            if ($isPreorder != $item->getProduct()->getPreorder()) {
                $quote->getQuote()->removeItem($quote->getId());
                $message = $isPreorder ? 'Can not add pre-order product to existing cart.' : 'Cart already contains pre-ordered products.';
                Mage::throwException(
                    Mage::helper('sales')->__($message)
                );
                break;
            }
        }
    }
}

If your are curious why using checkout_cart_product_add_after and remove item from cart later $quote->getQuote()->removeItem($quote->getId()) ... it is just because there is no checkout_cart_product_add_before event dispatched in Mage_Checkout_Model_Cart::addProduct() :(

1

You achieve many way. One of them is overwirte Mage_Sales_Model_Quote method addItem


<global>
    <models>
        <mymodule>
            <class>Vendor_Module_Model</class>
        </mymodule>
        <sales>
            <rewrite>
                <quote>Vendor_Module_Model_Quote</quote>
            </rewrite>
        </sales>
    </models>
</global>

And now app/code/local/Vendor/Module/Model/Quote.php


class Vendor_Module_Model_Quote extends Mage_Sales_Model_Quote
{
    /**
     * Adding new item to quote
     *
     * @param   Mage_Sales_Model_Quote_Item $item
     * @return  Mage_Sales_Model_Quote
     */
    public function addItem(Mage_Sales_Model_Quote_Item $item)
    {
        $isCurrentProductAllowPreorder = 0;
        $currentProduct = Mage::getModel('catalog/product')->load($item->getProductId());
        if($currentProduct->getPreorder()) {
            $isCurrentProductAllowPreorder = 1;
        }

        $cart = $this->getCart();
        $quote = $cart->getQuote();
        $quoteItems = $quote->getAllItems();
        foreach ($quoteItems as $quoteItem) {
            $itemProduct = Mage::getModel('catalog/product')->load($quoteItem->getProductId());
            if((!$itemProduct->getPreorder()) && ($isCurrentProductAllowPreorder)) {
                Mage::throwException(
                    Mage::helper('sales')->__('Not allow mix product')
                );
            }
            if(($itemProduct->getPreorder()) && (!$isCurrentProductAllowPreorder)) {
                Mage::throwException(
                    Mage::helper('sales')->__('Not allow mix product')
                );
            }
        }

        return parent::addItem($item);
    }

    /**
     * @return Mage_Checkout_Model_Cart|Mage_Core_Model_Abstract
     */
    public function getCart()
    {
        return Mage::getSingleton('checkout/cart');
    }
}

You can prevent to add mix product into cart.

  • Actually, i have created 10 pre-order products, I have created Pre-Order using this link, pixelpaul.net/create-pre-order-products-in-magento – user51361 Jun 10 '17 at 12:21
  • It's successfully working. How can I give condition like, if some of the non-pre-order products in the cart not able to allow my pre-order products and another one if my cart consists Pre-order products not allowed non-pre-orders products? How can I do that? – user51361 Jun 10 '17 at 12:25
  • Pls, check my updated questions. – user51361 Jun 10 '17 at 12:37
  • check updated answer – Sohel Rana Jun 10 '17 at 14:09
  • where I can add "method addItem" as you said the first point. – user51361 Jun 13 '17 at 5:34

Your Answer

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