Enforce conditions before checkout?
I'm looking for ideas on how to require certain conditions before allowing a user to checkout. I'm working on a site that requires "active member" status before users can register for programs, which are a special commerce product type.
One idea I've tried is a form alter that disables the checkout button if the user isn't an active member and the cart contains a program:
<?php
function module_form_views_form_commerce_cart_form_default_alter(&$form, &$form_state, $form_id) {
global $user;
//only proceed if the user is NOT an active member
if (!in_array('active member', array_values($user->roles))) {
// load cart for current user
$order = commerce_cart_order_load($user->uid);
// check to see if there is a program in the cart
if (module_rules_order_contains_program($order)) {
// yes, so disable the checkout button and provide a message
drupal_set_message("You need to be an active member to sign up for a program. Please add a membership to your cart.");
$form['actions']['checkout']['#disabled'] = TRUE;
}
}
}
?>
This works, with some issues: (1) you get an extra message even when removing a product from your cart that requires active member status (2) a user can easily bypass the check by following site/checkout path directly.
Are there better ways to enforce requirements? I'm looking for a practical approach that does the job, but want it to be user-friendly too, naturally.