Skip to content

Instantly share code, notes, and snippets.

@caschbre
Last active November 20, 2015 18:26
Show Gist options
  • Save caschbre/8715916a8ffa561f23a7 to your computer and use it in GitHub Desktop.
Save caschbre/8715916a8ffa561f23a7 to your computer and use it in GitHub Desktop.
/**
* Implements hook_init()
*/
function MYMODULE_discounts_init() {
// Grab the query parameters.
$query_parameters = drupal_get_query_parameters();
// If we find a coupon code then attempt to redeem it.
if (!empty($query_parameters['coupon'])) {
_MYMODULE_discounts_redeem_coupon_code($query_parameters['coupon']);
}
}
/**
* Custom commerce coupon redeem helper function
*
* @param string $coupon_code
* The coupon code that is setup on each coupon.
* @param (optional) integer $order_id
* An order ID may optionally be passed. If not passed then attempt to
* load the order for the active user.
*/
function _MYMODULE_discounts_redeem_coupon_code($coupon_code, $order_id = '') {
$error = '';
$uid = $GLOBALS['user']->uid;
// Load the order based on the order_id or the active user.
$order = !empty($order_id) ? commerce_order_load($order_id) : commerce_cart_order_load($uid);
// New users may not have an order to attach a coupon to, so do some extra
// work to create the order if the previous order load did not retrieve one.
if (empty($order)) {
// Try to find the order ID in the session.
if ($order_id = commerce_cart_order_id($uid)) {
$order = commerce_order_load($order_id);
}
// Create a new order
else {
$order = commerce_cart_order_new($uid);
}
}
// Instead of restricting a coupon coming from the querystring from being
// applied to an order with an existing coupon, it was decided to remove the
// existing coupon and add the new one. Leaving the following code commented
// out in case we need to revert back in the futre.
// if (_MYMODULE_discounts_max_coupons_reached($order, $error)) {
// $error = t('User attempted to add more than !count coupons to the cart/order', array('!count' => variable_get_value('MYMODULE_discounts_max_coupon_per_order')));
// }
// Remove any existing coupons before applying the new one.
if (!empty($order->commerce_coupons)) {
$order->commerce_coupons = NULL;
}
// Redeem the coupon and get a coupon object in return.
$coupon = commerce_coupon_redeem_coupon_code($coupon_code, $order, $error);
// If there is no error and we get a coupon back then reload the order and
// recalculate the order line items.
if (empty($error) && !empty($coupon)) {
// Reload the order so it is not out of date.
$order = commerce_order_load($order->order_id);
// Recalculate discounts.
commerce_cart_order_refresh($order);
watchdog('commerce_coupon', 'Applied coupon code @code to order !order_id', array('@code' => $coupon_code, '!order_id' => $order->order_id));
}
// If there was an error during the redeem process then log the error and
// remove the coupon from the order.
elseif (!empty($error)) {
watchdog('commerce_coupon', 'An error occurred redeeming a coupon: @error', array('@error' => $error));
if (!empty($coupon)) {
commerce_coupon_remove_coupon_from_order($order, $coupon);
}
}
}
projects[commerce_coupon][version] = 2.x-dev
projects[commerce_coupon][download][type] = git
projects[commerce_coupon][download][revision] = 8d70886
projects[commerce_coupon][download][branch] = 7.x-2.x
projects[commerce_coupon][patch][2263315] = https://www.drupal.org/files/issues/commerce_coupon-apply_without_discount-2263315-25.patch
projects[commerce_coupon][patch][2280505] = http://www.drupal.org/files/issues/commerce_coupon-auto_unserialize_data-2280505-6.patch
;projects[commerce_discount][version] = 1.x-dev
; Using a specific dev version to work with the newer coupon module.
projects[commerce_discount][download][type] = file
projects[commerce_discount][download][url] = http://cgit.drupalcode.org/commerce_discount/snapshot/26175ce30c720b0380c8eda5cce23f7d2510c1dc.tar.gz
projects[commerce_discount][patch][2257297] = http://www.drupal.org/files/issues/issue-2257297-fixes-text-format-alerts_0.patch
projects[commerce_discount][patch][2262225] = http://drupal.org/files/issues/commerce_discount-missing_calendar_png-2262225-1.patch
projects[commerce_discount][patch][2363331] = http://www.drupal.org/files/issues/commerce_discount-allow_free_shipping_custom_actions-2363331-1.patch
projects[commerce_discount][patch][2369899] = http://www.drupal.org/files/issues/commerce_discount-commerce_discount_usage_missing_data_property-2369899-1.patch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment