function commerce_coupon_remove_coupon_from_order in Commerce Coupon 7
Same name and namespace in other branches
- 7.2 commerce_coupon.module \commerce_coupon_remove_coupon_from_order()
Removes a coupon from the order context. references.
Parameters
$order: Order object to affect in the coupon removal.
$coupon: Coupon object to remove.
4 calls to commerce_coupon_remove_coupon_from_order()
- commerce_coupon_action_remove_coupon_from_order in ./
commerce_coupon.rules.inc - Action to remove a coupon from a given order.
- commerce_coupon_commerce_cart_order_refresh in ./
commerce_coupon.module - Implements hook_commerce_cart_order_refresh().
- commerce_coupon_remove_all_coupons_from_order in ./
commerce_coupon.module - Removes all coupons from a given order. references.
- commerce_coupon_ui_coupon_remove_coupon_from_order in ./
commerce_coupon_ui.module - Callback function to remove a coupon from the order used in the checkout pane view.
File
- ./
commerce_coupon.module, line 628 - Coupon System for Drupal Commerce.
Code
function commerce_coupon_remove_coupon_from_order($order, $coupon) {
$original_order = clone $order;
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Remove the coupons from the order relationship.
foreach ($order_wrapper->commerce_coupon_order_reference as $delta => $coupon_wrapper) {
if ($coupon_wrapper->coupon_id
->value() == $coupon->coupon_id) {
$order_wrapper->commerce_coupon_order_reference
->offsetUnset($delta);
}
}
if ($original_order != $order) {
commerce_order_save($order);
}
// Make a EFQ to get coupon line items in that order that match to the coupon.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'commerce_line_item')
->propertyCondition('order_id', $order->order_id)
->propertyCondition('type', 'commerce_coupon')
->fieldCondition('commerce_coupon_reference', 'target_id', $coupon->coupon_id, '=')
->execute();
if (empty($result)) {
return;
}
$line_item_ids = array_keys($result['commerce_line_item']);
if (!empty($line_item_ids)) {
// First remove the line items from the order and save it to avoid
// conflicts.
foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
if (in_array($line_item_wrapper->line_item_id
->value(), $line_item_ids)) {
$order_wrapper->commerce_line_items
->offsetUnset($delta);
}
}
commerce_line_item_delete_multiple($line_item_ids);
}
}