function commerce_discount_usage_order_discounts in Commerce Discount 7
Loads all discounts connected to an order.
Loads discounts including line item level discounts traced through line item unit price components.
Parameters
object $order: A fully qualified order object.
Return value
array List of order discounts.
1 call to commerce_discount_usage_order_discounts()
- commerce_discount_usage_record_order_usage in ./
commerce_discount.module - Record order usage.
File
- ./
commerce_discount.module, line 1141 - Defines the discount and discount offer entities, bundles and functionality.
Code
function commerce_discount_usage_order_discounts($order) {
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_discounts = array();
foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
try {
$unit_price = commerce_price_wrapper_value($line_item_wrapper, 'commerce_unit_price', TRUE);
} catch (Exception $ex) {
// Log and continue if we're unable to load the unit price.
// @TODO Resolve how to prevent commerce_cart_order_refresh() from firing
// inside an order save via entity_load_unchanged().
// @see https://www.drupal.org/node/2661530
$backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS);
$watchdog_variables = array(
'@backtrace' => json_encode($backtrace, JSON_PRETTY_PRINT),
);
watchdog('commerce_discount', 'Discount usage line item is missing. Backtrace: <pre>@backtrace</pre>', $watchdog_variables, WATCHDOG_DEBUG);
continue;
}
foreach ($unit_price['data']['components'] as $key => $component) {
if (strpos($component['name'], 'discount|') === 0 && !empty($component['price']['data']['discount_name'])) {
$order_discount_name = $component['price']['data']['discount_name'];
$order_discount_wrapper = entity_metadata_wrapper('commerce_discount', $order_discount_name);
// Make a list of discounts present via the order's line item price
// components.
if ($order_discount_wrapper
->value()) {
$order_discounts[] = $order_discount_wrapper->name
->value();
}
}
}
}
// Add the set of discounts directly referenced on the order.
if (isset($order->commerce_discounts) && $order_wrapper->commerce_discounts
->value()) {
foreach ($order_wrapper->commerce_discounts
->value() as $discount) {
if (isset($discount->name)) {
$order_discounts[] = $discount->name;
}
}
}
$order_discounts = array_unique($order_discounts);
return $order_discounts;
}