diff --git a/uc_cart/uc_cart.pages.inc b/uc_cart/uc_cart.pages.inc index d3a2f09..e32c10d 100644 --- a/uc_cart/uc_cart.pages.inc +++ b/uc_cart/uc_cart.pages.inc @@ -164,6 +164,19 @@ function uc_cart_checkout() { } } + // Check if the cart is valid for checkout. + $cart_validity = new stdClass(); + $cart_validity->validity = TRUE; + $cart_validity->messages = array(); + rules_invoke_event('uc_cart_checkout_start', $order, $cart_validity); + if (!$cart_validity->validity) { + drupal_set_message(t('You can not check out.'), 'error'); + foreach ($cart_validity->messages as $message) { + drupal_set_message(check_plain($message), 'error'); + } + drupal_goto('cart'); + } + return drupal_get_form('uc_cart_checkout_form', $order); } diff --git a/uc_cart/uc_cart.rules.inc b/uc_cart/uc_cart.rules.inc index 26b4003..a3e599d 100644 --- a/uc_cart/uc_cart.rules.inc +++ b/uc_cart/uc_cart.rules.inc @@ -7,6 +7,29 @@ */ /** + * Implements hook_rules_data_info(). + */ +function uc_cart_rules_data_info() { + $types['uc_cart_validity'] = array( + 'label' => t('Ubercart checkout validity'), + 'wrap' => TRUE, + 'group' => t('Ubercart'), + 'property info' => array( + 'validity' => array( + 'type' => 'boolean', + 'label' => t('Whether the checkout can proceed') + ), + 'messages' => array( + 'type' => 'list', + 'label' => 'List of error messages', + ), + ), + ); + + return $types; +} + +/** * Implements hook_rules_event_info(). */ function uc_cart_rules_event_info() { @@ -21,5 +44,64 @@ function uc_cart_rules_event_info() { ), ); + $events['uc_cart_checkout_start'] = array( + 'label' => t('Customer starts checkout'), + 'group' => t('Cart'), + 'variables' => array( + 'order' => array( + 'type' => 'uc_order', + 'label' => t('Order'), + ), + 'validity' => array( + 'type' => 'uc_cart_validity', + 'label' => t('Validity'), + ), + ), + ); + return $events; } + +/** + * Implements hook_rules_action_info(). + */ +function uc_cart_rules_action_info() { + + $actions['uc_cart_checkout_abort'] = array( + 'label' => t('Indicate that Ubercart checkout should not start'), + 'group' => t('Cart'), + 'base' => 'uc_cart_checkout_abort', + 'parameter' => array( + 'cart_validity' => array( + 'type' => 'uc_cart_validity', + 'label' => t('Cart validity'), + ), + 'message' => array( + 'type' => 'text', + 'label' => 'error message', + 'translatable' => TRUE, + ), + ), + ); + + return $actions; +} + +/** + * Indicate that a customer may not proceed to checkout. + * + * @param object $cart_validity + * Object to represent the validity of the cart. + * Updating this object sends information back to the function + * that invoked the event. + * - boolean $validity: Whether checkout can start. + * - array $messages: List of error messages. + * @param string $message + * New error message to add to the list of messages. + */ +function uc_cart_checkout_abort($cart_validity, $message) { + $cart_validity->validity = FALSE; + if (!empty($message)) { + $cart_validity->messages[] = $message; + } +}