function commerce_coupon_commerce_coupon_discount_value_display_alter in Commerce Coupon 7.2
Implements hook_commerce_coupon_discount_savings_value_alter().
File
- ./
commerce_coupon.module, line 1056 - Provides coupon functionality for Drupal Commerce.
Code
function commerce_coupon_commerce_coupon_discount_value_display_alter(&$text, $discount, $order) {
// Common variables.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$discount_wrapper = entity_metadata_wrapper('commerce_discount', $discount);
$offer_wrapper = $discount_wrapper->commerce_discount_offer;
$offer_type = $offer_wrapper->type
->value();
$discount_type = $discount_wrapper->type
->value();
// Savings value implementations on behalf of commerce discount.
switch ($discount_type) {
case 'order_discount':
switch ($offer_type) {
case 'fixed_amount':
$price = $offer_wrapper->commerce_fixed_amount
->value();
$text = commerce_currency_format($price['amount'], $price['currency_code']) . ' ' . t('off order');
break;
case 'percentage':
$text = t('@percentage% off order', array(
'@percentage' => $offer_wrapper->commerce_percentage
->value(),
));
break;
case 'free_shipping':
$text = t('Free shipping');
break;
case 'free_products':
foreach ($offer_wrapper->commerce_free_products
->value() as $product) {
$product_names[] = check_plain($product->title);
}
if (isset($product_names)) {
$product_text = implode(', ', $product_names);
}
$text = t('Free product(s):') . ' ' . $product_text;
break;
}
break;
case 'product_discount':
// By default, product discounts will show the value of the discount as
// well as what product it is for, as defined in its inline conditions.
$product_text = t('all products');
$conditions = $discount_wrapper->inline_conditions
->value();
// Get a list of product ids on the order.
$order_product_ids = array();
foreach ($order_wrapper->commerce_line_items as $commerce_line_item) {
if (isset($commerce_line_item->commerce_product)) {
$product_id = $commerce_line_item->commerce_product
->getIdentifier();
$order_product_ids[$product_id] = $product_id;
}
}
foreach ($conditions as $condition) {
if ($condition['condition_name'] == 'commerce_product_contains_products') {
foreach ($condition['condition_settings']['sku'] as $data) {
if (isset($order_product_ids[$data['product_id']])) {
$product = commerce_product_load($data['product_id']);
if ($product && empty($condition['condition_negate'])) {
$product_names[] = check_plain($product->title);
}
}
}
if (isset($product_names)) {
$product_text = '<em>' . implode('</em>, <em>', $product_names) . '</em>';
}
break;
}
}
switch ($offer_type) {
case 'fixed_amount':
$price = $offer_wrapper->commerce_fixed_amount
->value();
$offer_text = commerce_currency_format($price['amount'], $price['currency_code']);
break;
case 'percentage':
$offer_text = t('@percentage%', array(
'@percentage' => $offer_wrapper->commerce_percentage
->value(),
));
break;
}
$text = $offer_text . ' ' . t('off') . ' ' . $product_text;
break;
default:
// By default, just use the label.
$text = check_plain($discount->label);
break;
}
}