disallow certain product combinations (in cart)
Hi,
I want to block certain product from being added to cart (based on existing cart's content). So, I thought I'll do this via an observer, that when triggered, looks at the cart's content and marks it as invalid for checkout. However... it doesn't seem to be working. Any thoughts about what I'm doing wrong?
My code is similar to (in this case I want to see an error when adding product id 1 IF there are other products in cart already):
class order_restrict extends base
{
function __construct() {
global $zco_notifier;
$zco_notifier->attach($this, array('NOTIFIER_CART_ADD_CART_END'));
}
function update(&$class, $eventID, $paramsArray = array())
{
global $messageStack;
switch ($eventID)
{
case 'NOTIFIER_CART_ADD_CART_END':
$cart = $_SESSION['cart'];
$product1_id = zen_get_uprid(1, '');
$products = $cart->get_products();
if (sizeof($products)>1 && $cart->in_cart($product1_id) )
{
$_SESSION['valid_to_checkout'] = false;
$messageStack->add('shopping_cart', TEXT_CANT_ADD_THIS, 'error');
$_SESSION['cart_errors'] .= TEXT_CANT_ADD_THIS. '<br />';
}
break;
default:
break;
}
}
}
thanks!
Re: disallow certain product combinations (in cart)
My code works fine, the problem is that NOTIFIER_CART_ADD_CART_END is not received when adding an item to the cart (via actionBuyNow). Why?
Re: disallow certain product combinations (in cart)
1. $zco_notifier->attach() should be $this->attach() since you're using a notifier hook that exists inside a class.
2. Your auto_loader entry point needs to be something greater than like 190 or so. I usually suggest people use 200 or greater, but 190 works too.
Re: disallow certain product combinations (in cart)
Yes, I used 190 and, before posting, I tried $this->attach($this, array('NOTIFIER_CART_ADD_CART_END')), too. Either way I don't get that notification. I subscribed to pretty much all of them. While I did add products, all I got was:
Got event: NOTIFIER_CART_GET_PRODUCTS_START
Got event: NOTIFIER_CART_GET_PRODUCTS_END
Got event: NOTIFIER_CART_COUNT_CONTENTS_START
Got event: NOTIFIER_CART_GET_QUANTITY_START
Got event: NOTIFIER_CART_GET_QUANTITY_END_QTY
Got event: NOTIFIER_CART_GET_QUANTITY_START
Got event: NOTIFIER_CART_GET_QUANTITY_END_QTY
Got event: NOTIFIER_CART_COUNT_CONTENTS_END
Got event: NOTIFIER_CART_SHOW_TOTAL_START
Got event: NOTIFIER_CART_SHOW_TOTAL_END
Got event: NOTIFIER_CART_GET_PRODUCTS_START
Got event: NOTIFIER_CART_GET_PRODUCTS_END
Got event: NOTIFIER_CART_COUNT_CONTENTS_START
Got event: NOTIFIER_CART_GET_QUANTITY_START
Got event: NOTIFIER_CART_GET_QUANTITY_END_QTY
Got event: NOTIFIER_CART_GET_QUANTITY_START
Got event: NOTIFIER_CART_GET_QUANTITY_END_QTY
Got event: NOTIFIER_CART_COUNT_CONTENTS_END
Got event: NOTIFIER_CART_COUNT_CONTENTS_START
Got event: NOTIFIER_CART_GET_QUANTITY_START
Got event: NOTIFIER_CART_GET_QUANTITY_END_QTY
Got event: NOTIFIER_CART_GET_QUANTITY_START
Got event: NOTIFIER_CART_GET_QUANTITY_END_QTY
Got event: NOTIFIER_CART_COUNT_CONTENTS_END
Got event: NOTIFIER_CART_COUNT_CONTENTS_START
Got event: NOTIFIER_CART_GET_QUANTITY_START
Got event: NOTIFIER_CART_GET_QUANTITY_END_QTY
Got event: NOTIFIER_CART_GET_QUANTITY_START
Got event: NOTIFIER_CART_GET_QUANTITY_END_QTY
Got event: NOTIFIER_CART_COUNT_CONTENTS_END
Got event: NOTIFIER_CART_COUNT_CONTENTS_START
Got event: NOTIFIER_CART_GET_QUANTITY_START
Got event: NOTIFIER_CART_GET_QUANTITY_END_QTY
Got event: NOTIFIER_CART_GET_QUANTITY_START
Got event: NOTIFIER_CART_GET_QUANTITY_END_QTY
Got event: NOTIFIER_CART_COUNT_CONTENTS_END
Anyway, using NOTIFIER_CART_COUNT_CONTENTS_END is sufficient for my workflow. But why am I not getting NOTIFIER_CART_ADD_CART_ is odd...