LemonStand Documentation

shop:onBeforeAddToCart event

The shop:onBeforeAddToCart event is triggered before a product is added to the cart. The event handler function should accept the following parameters:

  • Cart name
  • Product object (Shop_Product)
  • Quantity
  • Product options array
  • Product extra options array
  • Product custom data fields array

You can use the event handler to perform custom validation when a customer clicks the Add to Cart button. Also, inside the handler you can trigger an exception in order to prevent adding a product to the cart.

Event handler example:

public function subscribeEvents()
{
  Backend::$events->addEvent('shop:onBeforeAddToCart', $this, 'before_add');
}
 
public function before_add($cart_name, $product, $quantity, $options, $extra_options, $custom_data)
{
  traceLog('Add '.$product->name);
  if ($product->sku == '123')
    throw new Phpr_ApplicationException('You cannot add this product to the cart!');
}

Next: shop:onAfterAddToCart event
Previous: shop:onUpdateCartItemPrice event
Return to Handling LemonStand events