# Drupal commerce (Coupon & Discounts) - Alter rule condition

I have a drupal commerce website on which I have add the discout module to make discounts and coupons. I have in my backoffice a list of all the orders and I can modify them if I want.

The problem is the following :

If I create an order with a discount limited in the time, at the moment when I create the order the coupon is valid and it's added to my order. But then, multiple days ago I have to modify this order for some reasons, but now the coupon is expired, and if I update my order then the coupon is recalculed but it's expired so the order doesn't have the discount now.

But I need to have the possibility to add / remove coupon after an order was done. How can I fix this ?

EDIT :

I think this is this one I need to modify :

/**
* Rules condition: Check discount can be applied.
*/
function commerce_discount_date_condition($discount_name) {$wrapper = entity_metadata_wrapper('commerce_discount', $discount_name);$time = time();

return $time >=$wrapper->commerce_discount_date->value->value() && $time <=$wrapper->commerce_discount_date->value2->value();
}

But how can I modify it without change the module files, I don't think that this function have a hook ?

And you say that I just have to add the uid 1, but I have some other person that are admin on the site and can modify orders, I think I can add a verification on the roles ?

EDIT 2 :

The function doesn't have a hook but the two functions that call it have one :

function commerce_discount_commerce_discount_rule_build($rule,$discount)

And

function commerce_discount_rules_condition_info()

I try to use these hooks in my module (with _alter and without) but they are not triggering, I don't know these functions maybe the rules function are called only certain times ?

• You'll have to edit the coupon rule so it fired if coupon is not expired OR current user id is 1. – No Sssweat Dec 5 '18 at 15:09
• Can you look at my edit ? thanks. – Jess Dec 5 '18 at 15:18

If someone need to do the same thing as me, finally I compare the dates of the coupon with the order date, so if a user use a coupon on an order, when I edit it even if the coupon is expired the order still have it.

Here is how I proceed :

First I rewrite the condition 'commerce_discount_date_condition' :

function mymodule_rules_condition_info_alter(&$conditions) {$conditions['commerce_discount_date_condition'] = array(
'label' => t('Check discount dates'),
'group' => t('Commerce Discount'),
'parameter' => array(
'commerce_discount' => array(
'label' => t('Commerce Discount'),
'type' => 'token',
'options list' => 'commerce_discount_entity_list',
),
'commerce_order' => array(
'type' => 'commerce_order',
'label' => t('Commerce order'),
'description' => t('The order the discount would be applied to.'),
'wrapped' => TRUE,
)
),
'base' => 'ga_registration_discount_date_condition',
);
}

I add in this condition the order on which the discount is, so I can use it in the 'base' function :

function mymodule_discount_date_condition($discount_name,$order) {
$wrapper = entity_metadata_wrapper('commerce_discount',$discount_name);
dsm("cond");
// Use the order time
$time =$order->created->value();
return $time >=$wrapper->commerce_discount_date->value->value() && $time <=$wrapper->commerce_discount_date->value2->value();
}

This function use the order creation time to compare with the discount dates.

The last thing to do was to rewrite the whole commerce_discount_rule_build hook (The only thing that changes are in the end) :

function mymodule_commerce_discount_rule_build($rule,$discount) {
$wrapper = entity_metadata_wrapper('commerce_discount',$discount);
$discount_offer =$wrapper->commerce_discount_offer->value();
$wrapper_discount_offer = entity_metadata_wrapper('commerce_discount_offer',$discount_offer);

// Check if property is attached to commerce free shipping!
if (isset($wrapper_discount_offer->commerce_free_shipping)) { if (!$shipping_service = $wrapper_discount_offer->commerce_free_shipping->value()) { // No need to change the rules event. return; } // Add missing parameter. foreach ($rule->actions() as $action) { if ($action->getElementName() == 'commerce_discount_free_shipping_service') {
$action->settings['shipping_service'] =$shipping_service;
}
}
}

// Product level discounts must pass the line item's order.
$map = array( 'order_discount' => 'commerce-order', 'product_discount' => 'commerce-line-item:order', ); if (isset($map[$discount->type])) { // Add condition for per-person usage. if ($wrapper->discount_usage_per_person->value()) {
$rule->condition( 'commerce_discount_usage_max_usage_per_person', array( 'commerce_discount' =>$discount->name,
'order:select' => $map[$discount->type],
'usage' => $wrapper->discount_usage_per_person->value(), ) ); } // For normal usage. if ($wrapper->discount_usage_limit->value()) {
$rule->condition( 'commerce_discount_usage_max_usage', array( 'commerce_discount' =>$discount->name,
'order:select' => $map[$discount->type],
'usage' => $wrapper->discount_usage_limit->value(), ) ); } } if (!empty($discount->commerce_discount_date)) {

$types = commerce_discount_types();$type = $types[$discount->type];
// On désactive pas les règles des discounts
$rule->active = TRUE; // Ajoute la condition$rule->condition('commerce_discount_date_condition', array(
'commerce_discount' => $discount->name, 'commerce_order:select' =>$type['entity type'] == 'commerce_order' ? 'commerce-order' : 'commerce-line-item:order',
));

}

}

What i change is all the things inside the last if, I always set the rule to active (normally rules are desactivated when the discount is expired), and I add to the rule the condition with the discount name and the order. If I rewrite the whole hook it's because the $rule->condition add a condition, even if I add a condition with the same name it's added and not updated, so if I just use the hook without unset the base function I have the original condition and mine in the rule. So, I unset the module only for this hook : function mymodule_module_implements_alter(&$implementations, $hook) { if ($hook == 'menu_alter') {
// Move ga_registration_menu_alter() to the end of the list.
$group =$implementations['ga_registration'];
unset($implementations['ga_registration']);$implementations['ga_registration'] = $group; } if ($hook == 'commerce_discount_rule_build'){
unset(\$implementations['commerce_discount']);
}
}