2

I have a sku, let's call it sku100 for now. I only want that sku to be allowed to be added to cart if any other sku exists in the cart. I tried looking at shopping cart price rules but can't find any combinations to make this work. Any help would be appreciated.

0

You can override Mage_Checkout_CartController::addAction() method and set validation like this

$product = Mage::getModel('catalog/product')->load(Mage::app()->getRequest()->getParam('product', 0));
$skus = array('sku100');
$sku = $product->getSku();
if(count($cart->getAllItems()))
{
    if(in_array($sku,$skus)){
        $msg = "Product add to cart";
    }
    else{
        $msg = "Please select another product";
            throw new Exception($msg);
    }
}

you can also use product add to cart event set validation same as above

0

I'd add a new product attribute to store the required SKUs (i.e. required_sku) and add an observer to checkout_cart_product_add_after.

config.xml

<checkout_cart_product_add_after>
    <observers>
        <some_alias>
            <type>model</type>
            <class>my_module/observer</class>
            <method>checkoutCartProductAddAfter</method>
        </some_alias>
    </observers>
</checkout_cart_product_add_after>

Observer.php

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

    # new product attribute 'required_sku'
    $required   = $product->getRequiredSku();
    $canBeAdded = false;

    if ($required) {
        foreach ($cartItems as $item) {
            if ($required == $item->getProduct()->getSku()) {
                $canBeAdded = true;
                break;
            }
        }

        if (!$canBeAdded) {
            $quote->getQuote()->removeItem($quote->getId());
            Mage::throwException(
                Mage::helper('sales')->__('Product ' . $required . ' has to be added first.')
            );
        }
    }
}

Full code here: How to exclude products being added to cart based on products attributes?

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.