Skip to content

Instantly share code, notes, and snippets.

@caschbre
Last active February 29, 2016 19:12
Show Gist options
  • Save caschbre/f0274302db1db209799f to your computer and use it in GitHub Desktop.
Save caschbre/f0274302db1db209799f to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Provides custom inline conditions.
*/
/**
* Implements hook_inline_conditions_info().
*/
function lf_discounts_inline_conditions_info() {
$conditions['lf_discounts_qty_per_line'] = array(
'label' => t('Quantity per Line'),
'entity type' => 'commerce_line_item',
'callbacks' => array(
'configure' => 'lf_discounts_qty_per_line_configure',
'build' => 'lf_discounts_qty_per_line_build',
),
);
return $conditions;
}
/**
* Inline condition configure callback: Qty per line.
*
* @param array $settings
* An array of rules condition settings.
*
* @return array
* return a form build array.
*/
function lf_discounts_qty_per_line_configure($settings) {
$form['operator'] = array(
'#type' => 'select',
'#title' => t('Operator'),
'#title_display' => 'invisible',
'#options' => _commerce_discount_operator_options(),
'#default_value' => !empty($settings['operator']) ? $settings['operator'] : '>=',
);
$form['quantity'] = array(
'#type' => 'textfield',
'#title' => t('Quantity'),
'#title_display' => 'invisible',
'#default_value' => !empty($settings['quantity']) ? $settings['quantity'] : '',
'#size' => 5,
'#required' => TRUE,
'#element_validate' => array('element_validate_integer'),
'#suffix' => '<div class="condition-instructions">' . t("The discount is active if the line item quantity matches the condition above.") . '</div>',
);
return $form;
}
<?php
/**
* @file
* Contains rules for custom discounts / inline conditions.
*/
/**
* Implements hook_rules_condition_info().
*/
function lf_discounts_rules_condition_info() {
$inline_conditions = inline_conditions_get_info();
$conditions = array();
$conditions['lf_discounts_qty_per_line'] = array(
'label' => t('Quantity per line'),
'parameter' => array(
'commerce_line_item' => array(
'type' => 'commerce_line_item',
'label' => t('Line Item'),
'description' => t('A product line item.'),
'wrapped' => TRUE,
),
'operator' => array(
'type' => 'text',
'label' => t('Operator'),
'description' => t('The operator used with the product quantity value below.'),
'default value' => '>=',
'options list' => '_commerce_discount_operator_options',
),
'quantity' => array(
'type' => 'integer',
'label' => t('Quantity'),
'description' => t('Quantity value to be compared against the line item.'),
),
),
'module' => 'lf_discounts',
'group' => t('Commerce Line Item'),
'callback' => array(
'execute' => $inline_conditions['lf_discounts_qty_per_line']['callbacks']['build'],
),
);
return $conditions;
}
/**
* Build callback for Qty per line.
*
* @param EntityDrupalWrapper $wrapper
* Wrapper entity type given by the rule.
* @param string $operator
* The operator returned by the rule condition.
* @param int $quantity
* The quantity returned by the rule condition.
*
* @return boolean
* True if condition is valid. False otherwise.
*/
function lf_discounts_qty_per_line_build(EntityDrupalWrapper $wrapper, $operator, $quantity) {
return FALSE;
$debug = array(
'op' => $operator,
'qty' => $quantity,
'entity' => $wrapper->value(),
);
watchdog(__FUNCTION__, '<pre>' . print_r($debug, TRUE) . '</pre>');
return FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment