Skip to content

Validing the Cart

If you want to apply some custom logic or business rules to the cart before you checkout then you can implement an event listener to validate the cart.

Event Listener: civi.shoppingcart.submitcart

It takes three parameters: - Cart ID. - contributionValues - The list of values that should be set on the contribution. - lineItems - The list of lineitems generated from the items in the cart.

Example:

<?php
namespace Civi\Customextension;

use Civi\Core\Service\AutoSubscriber;
use Civi\Shoppingcart\Event\SubmitCartEvent;

class SubmitCartSubscriber extends AutoSubscriber {

  /**
   * @return array
   */
  public static function getSubscribedEvents(): array {
    return [
      'civi.shoppingcart.submitcart' => [['onSubmitCart', 100]],
    ];
  }

  public function onSubmitCart(SubmitCartEvent $event) {
    $cartID = $event->cartID;
    $contributionValues = $event->contributionValues;
    $lineItems = $event->lineItems;

    foreach ($event->lineItems as $lineItem) {
      // Do something with the lineitems..
    }
  }

}