Skip to content

Instantly share code, notes, and snippets.

@ss81
Last active October 12, 2015 20:58
Show Gist options
  • Save ss81/4086579 to your computer and use it in GitHub Desktop.
Save ss81/4086579 to your computer and use it in GitHub Desktop.
Only one product in shopping cart.
<?php
/**
* Implementation of hook_form_alter()
*/
function hook_form_alter(&$form, $form_state, $form_id) {
if (drupal_match_path($form_id, 'uc_product_add_to_cart_form_*')) {
array_unshift($form['#submit'], 'uc_product_before_add_to_cart_submit');
}
}
/**
* Callback, which clears shopping cart if it's not empty.
* Next submit callback (provided by ubercart) will add new product into shopping cart.
*/
function uc_product_before_add_to_cart_submit($form, &$form_state) {
$cart_items = uc_cart_get_contents();
if (!empty($cart_items)) {
uc_cart_empty(uc_cart_get_id(FALSE));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment