Skip to content

Instantly share code, notes, and snippets.

@generalconsensus
Created June 14, 2012 07:52
Show Gist options
  • Save generalconsensus/2928865 to your computer and use it in GitHub Desktop.
Save generalconsensus/2928865 to your computer and use it in GitHub Desktop.
I was just recently trying to figure this problem out and I realized that the issue might be that we need a different kind of action for applying a percentage coupon against an order total. Here is my rough code...
<?php
function hook_rules_action_info() {
$actions['commerce_coupon_pct_apply_to_total'] = array(
'label' => t('Apply a percentage coupon to the order total'),
'parameter' => array(
'coupon' => array(
'type' => 'commerce_coupon',
'label' => t('Coupon'),
),
'commerce_order' => array(
'type' => 'commerce_order',
'label' => t('commerce order'),
),
'component_name' => array(
'type' => 'text',
'label' => t('Price component type'),
'description' => t('Price components track changes to prices made during the price calculation process, and they are carried over from the unit price to the total price of a line item. When an order total is calculated, it combines all the components of every line item on the order. When the unit price is altered by this action, the selected type of price component will be added to its data array and reflected in the order total display when it is formatted with components showing. Defaults to base price, which displays as the order Subtotal.'),
'options list' => 'commerce_price_component_titles',
'default value' => 'base_price',
),
'round_mode' => array(
'type' => 'integer',
'label' => t('Price rounding mode'),
'description' => t('Round the resulting price amount after performing this operation.'),
'options list' => 'commerce_round_mode_options_list',
'default value' => COMMERCE_ROUND_HALF_UP,
),
),
'base' => 'commerce_coupon_pct_apply_to_total',
'group' => t('Commerce Coupon'),
);
return ($actions);
}
function commerce_coupon_pct_apply_to_total($coupon, $commerce_order, $component_name, $round_mode) {
$coupon_wrapper = entity_metadata_wrapper('commerce_coupon', $coupon);
$fields = $coupon_wrapper->getPropertyInfo();
// Apply the coupon just if it's active, the type is of pct and it has the
// field for percentage set.
if ($coupon->is_active == TRUE && $coupon->type == 'commerce_coupon_pct'
&& isset($fields['commerce_coupon_percent_amount']) && $coupon_wrapper->commerce_coupon_percent_amount->value() > 0) {
$rate = $coupon_wrapper->commerce_coupon_percent_amount->value();
if ($rate > 1) {
// Ensure that the rate is never bigger then 100%
$rate = $rate / 100;
}
else {
return;
}
$order_wrapper = entity_metadata_wrapper('commerce_order', $commerce_order);
//how the heck to do i just grab the subtotal from the order :(
// Calculate the updated amount and create a price array representing the
// difference between it and the current amount.
$current_amount = $commerce_subtotal['amount'];
$updated_amount = commerce_round($round_mode, $current_amount - $amount);
$difference = array(
'amount' => $updated_amount - $current_amount,
'currency_code' => $commerce_subtotal['currency_code'],
'data' => array(),
);
$updated_amount = $updated_amount * -1;
$line_item = commerce_coupon_line_item_new($coupon, $commerce_order->order_id->raw());
commerce_line_item_save($line_item);
$commerce_order->commerce_line_items[] = $line_item;
return array('commerce_coupon_line_item' => $line_item);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment