Skip to content

Instantly share code, notes, and snippets.

@druman
Forked from caschbre/example.module
Created February 28, 2018 13:13
Show Gist options
  • Save druman/d90bcd06a1c34c3a40c25e74ef434bfa to your computer and use it in GitHub Desktop.
Save druman/d90bcd06a1c34c3a40c25e74ef434bfa to your computer and use it in GitHub Desktop.
Example of accepting drupal commerce coupon via querystring
<?php
/**
* Notes: the project this was taken from attempted to limit a user to one coupon per order.
* Some of the logic was taken directly from commerce_coupon 2.x.
*/
/**
* Implements hook_init()
*/
function example_commerce_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'])) {
_example_commerce_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 _example_commerce_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 (_example_commerce_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('example_commerce_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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment