Skip to content

Instantly share code, notes, and snippets.

@proofoftom
Created December 27, 2016 21:10
Show Gist options
  • Save proofoftom/3d3b104e937068d7ef2451fe6c60085c to your computer and use it in GitHub Desktop.
Save proofoftom/3d3b104e937068d7ef2451fe6c60085c to your computer and use it in GitHub Desktop.
Limits a coupon type (i.e. Discount Coupons) to one per order, while allowing other types (i.e. Gift Cards) to have multiple applied to the order.
<?php // Required for gist code formatting.
/**
* Implements hook_commerce_coupon_condition_outcome_alter().
*/
function mymodule_custom_commerce_coupon_condition_outcome_alter(&$outcome, $context) {
$order_wrapper = entity_metadata_wrapper('commerce_order', $context['order']);
$order_coupons = $order_wrapper->commerce_coupons->value();
$coupon = $context['coupon']->value();
// Validate only if the order doesn't contain other *Discount* coupons,
// or if the current coupon is the first one added to the order.
if (!empty($order_coupons) && ($coupon->type == 'discount_coupon')) {
// Find first discount coupon in the order.
foreach ($order_coupons as $order_coupon) {
// If the first discount coupon in the order isn't this coupon,
// then reject the coupon.
if ($order_coupon->type == 'discount_coupon' && $order_coupon->coupon_id !== $coupon->coupon_id) {
drupal_set_message(t('You cannot use more than 1 discount coupon for the same order.'), 'error');
$outcome = FALSE;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment