Created
January 29, 2013 14:32
-
-
Save DuaelFr/4664660 to your computer and use it in GitHub Desktop.
hook_default_rules_configuration() advanced sample.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_default_rules_configuration(). | |
*/ | |
function MYMODULE_default_rules_configuration() { | |
$rules = array(); | |
// Custom rule : calculate the weight of a line item | |
$rule = rule($variables, $provides); | |
$rule->label = t('(MYMODULE) Calculate total weight of line item'); | |
$rule->active = TRUE; | |
$rule | |
->condition('entity_has_field', array( | |
'entity:select' => 'line_item', | |
'field' => 'commerce_product', | |
)) | |
->condition('entity_has_field', array( | |
'entity:select' => 'line-item:commerce-product', | |
'field' => 'field_weight', | |
)) | |
->action('data_calc', array( | |
'input_1:select' => 'line-item:commerce-product:field-weight:weight', | |
'op' => '*', | |
'input_2:select' => 'line-item:quantity', | |
'result:var' => 'result', | |
'result:label' => 'Résultat du calcul', | |
)) | |
->action('data_set', array( | |
'data:select' => 'total-weight', | |
'value:select' => 'result', | |
)); | |
$rules['MYMODULE_action_calculate_weight_line_item'] = $rule; | |
// Custom rule : calculate the weight of an entire order | |
$rule = rule($variables, $provides); | |
$rule->label = t('(MYMODULE) Calculate total weight of order'); | |
$rule->active = TRUE; | |
$rule | |
->action('data_set', array( | |
'data:select' => 'total_weight', | |
'value' => '0', | |
)) | |
->action( | |
rules_loop(array( | |
'list:select' => 'commerce-order:commerce-line-items', | |
'item:var' => 'line_item', | |
'item:label' => 'Line item', | |
)) | |
->action('component_MYMODULE_action_calculate_weight_line_item', array( | |
'line_item:select' => 'line-item', | |
'total_weight:var' => 'total_weight_line_item', | |
'total_weight:label' => 'Total weight of line item', | |
)) | |
->action('data_calc', array( | |
'input_1:select' => 'total-weight', | |
'op' => '+', | |
'input_2:select' => 'total-weight-line-item', | |
'result:var' => 'result', | |
'result:label' => 'Résultat du calcul', | |
)) | |
->action('data_set', array( | |
'data:select' => 'total-weight', | |
'value:select' => 'result', | |
)) | |
); | |
$rules['MYMODULE_action_calculate_weight_order'] = $rule; | |
// Custom condition : order's total weight is between 0 and 99g | |
$rule = rules_and($variables); | |
$rule->label = t('(MYMODULE) Condition weight between 0 and 99g'); | |
$rule->active = TRUE; | |
$rule | |
->condition( | |
rules_or() | |
->condition('data_is', array( | |
'data:select' => 'total_weight', | |
'op' => '=', | |
'value' => '0', | |
)) | |
->condition('data_is', array( | |
'data:select' => 'total_weight', | |
'op' => '>', | |
'value' => '0', | |
)) | |
) | |
->condition('data_is', array( | |
'data:select' => 'total_weight', | |
'op' => '<', | |
'value' => '0.1', | |
)); | |
$rules['MYMODULE_condition_weight_0_100'] = $rule; | |
// Custom rule : if order weight between O and 99g, apply the given shipping rate | |
$rule = rule($variables); | |
$rule->label = t('(MYMODULE) Enable flat rate "area1_0_100"'); | |
$rule->active = TRUE; | |
$rule | |
->condition('component_MYMODULE_condition_weight_0_100', array( | |
'total_weight:select' => 'total_weight', | |
)) | |
->action('commerce_shipping_service_rate_order', array( | |
'shipping_service_name' => 'area1_0_100', | |
'commerce_order:select' => 'commerce_order', | |
)); | |
$rules['MYMODULE_action_enable_rate_area1_0_100'] = $rule; | |
// Custom rule : collect rates | |
$rule = rule($variables); | |
$rule->label = t('(MYMODULE) Enable flat rates for Area 1'); | |
$rule->active = TRUE; | |
$rule | |
->condition('component_MYMODULE_condition_shipping_area1', array( | |
'commerce_order:select' => 'commerce_order', | |
)) | |
->action('component_MYMODULE_action_enable_rate_area1_0_100', array( | |
'commerce_order:select' => 'commerce_order', | |
'total_weight:select' => 'total_weight', | |
)) | |
->action('component_MYMODULE_action_enable_rate_area1_100_500', array( | |
'commerce_order:select' => 'commerce_order', | |
'total_weight:select' => 'total_weight', | |
)) | |
->action('component_MYMODULE_action_enable_rate_area1_500_1000', array( | |
'commerce_order:select' => 'commerce_order', | |
'total_weight:select' => 'total_weight', | |
)) | |
->action('component_MYMODULE_action_enable_rate_area1_1000_2000', array( | |
'commerce_order:select' => 'commerce_order', | |
'total_weight:select' => 'total_weight', | |
)) | |
->action('component_MYMODULE_action_enable_rate_area1_2000_10000', array( | |
'commerce_order:select' => 'commerce_order', | |
'total_weight:select' => 'total_weight', | |
)) | |
->action('component_MYMODULE_action_enable_rate_area1_10000+', array( | |
'commerce_order:select' => 'commerce_order', | |
'total_weight:select' => 'total_weight', | |
)); | |
$rules['MYMODULE_action_enable_rate_area1'] = $rule; | |
// custom reaction rule : when collecting rates, run the other custom rules | |
$rule = rules_reaction_rule(); | |
$rule->label = t('(MYMODULE) Enable shipping rates'); | |
$rule->active = TRUE; | |
$rule->event('commerce_shipping_collect_rates') | |
->action('component_MYMODULE_action_calculate_weight_order', array( | |
'commerce_order:select' => 'commerce_order', | |
'total_weight:var' => 'total_weight', | |
'total_weight:label' => 'Total weight', | |
)) | |
->action('component_MYMODULE_action_enable_rate_france', array( | |
'commerce_order:select' => 'commerce_order', | |
'total_weight:select' => 'total_weight', | |
)) | |
->action('component_MYMODULE_action_enable_rate_area1', array( | |
'commerce_order:select' => 'commerce_order', | |
'total_weight:select' => 'total_weight', | |
)) | |
->action('component_MYMODULE_action_enable_rate_area2', array( | |
'commerce_order:select' => 'commerce_order', | |
'total_weight:select' => 'total_weight', | |
)); | |
$rules['MYMODULE_action_enable_shipping_rates'] = $rule; | |
return $rules; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment