You are here

commerce_taxonomy_conditions.module in commerce taxonomy conditions 7.2

Same filename and directory in other branches
  1. 7 commerce_taxonomy_conditions.module

Allow to create rules for shipping based on the taxonomy terms in a product

The module allows you to create rules based on taxonomy terms, by setting different conditions and a maximum amount.

File

commerce_taxonomy_conditions.module
View source
<?php

/**
 * @file
 * Allow to create rules for shipping based on the taxonomy terms in a product
 *
 * The module allows you to create rules based on taxonomy terms, 
 * by setting different conditions and a maximum amount.
 */

/**
 * Implements hook_rules_condition_info().
 */
function commerce_taxonomy_conditions_rules_condition_info() {
  $conditions = array();
  $conditions['commerce_taxonomy_conditions_rules_contains_product'] = array(
    'label' => t('Order contains a product with a certain term(taxonomy)'),
    'parameter' => array(
      'commerce_order' => array(
        'type' => 'commerce_order',
        'label' => t('Order'),
        'description' => t('Check the order if it contains a taxonomy term that is given. If the specified order does not exist, the comparison will act as if it is against a quantity of 0.'),
      ),
      'term_field_names' => array(
        'type' => 'text',
        'label' => t('Term Reference Field'),
        'description' => t("The machine-name of the expected product's term reference field"),
        'options list' => 'commerce_taxonomy_conditions_term_list',
        'restriction' => 'input',
      ),
      'term_ids' => array(
        'type' => 'list<text>',
        'label' => t('Taxonomy Terms'),
        'description' => t('The terms being checked for'),
        'options list' => 'commerce_taxonomy_conditions_term_options_list',
      ),
      'operator' => array(
        'type' => 'text',
        'label' => t('Operator'),
        'description' => t('The operator used with the quantity value below to compare the quantity of the specified product on the order.'),
        'default value' => '>=',
        'options list' => 'commerce_numeric_comparison_operator_options_list',
        'restriction' => 'input',
      ),
      'value' => array(
        'type' => 'text',
        'label' => t('Quantity'),
        'default value' => '1',
        'description' => t('The value to compare against the quantity of the specified product on the order.'),
      ),
    ),
    'group' => t('Commerce Order'),
    'callbacks' => array(
      'execute' => 'commerce_taxonomy_conditions_rules_contains_product',
    ),
  );
  return $conditions;
}

/**
 * Implements hook_form_alter().
 */
function commerce_taxonomy_conditions_form_alter(&$form, &$form_state, $form_id) {
  if (($form_id == 'rules_ui_add_element' || $form_id == 'rules_ui_edit_element') && !empty($form['parameter']['term_field_names']['settings']['term_field_names'])) {
    $form['parameter']['term_field_names']['settings']['term_field_names']['#ajax'] = array(
      'callback' => 'commerce_taxonomy_conditions_term_options_list_form',
      'wrapper' => 'term-ids-replacement',
      'method' => 'replace',
      'effect' => 'fade',
    );
    if (!empty($form_state['values']['parameter']['term_field_names']['settings']['term_field_names'])) {
      $terms = commerce_taxonomy_conditions_term_options_list($form_state['values']['parameter']['term_field_names']['settings']['term_field_names']);
    }
    else {
      $terms = commerce_taxonomy_conditions_term_options_list($form['parameter']['term_field_names']['settings']['term_field_names']['#default_value']);
    }
    $form['parameter']['term_ids']['settings']['term_ids']['#options'] = $terms;
    $form['parameter']['term_ids']['settings']['term_ids']['#prefix'] = '<div id="term-ids-replacement">';
    $form['parameter']['term_ids']['settings']['term_ids']['#suffix'] = '</div>';
    $form['parameter']['term_ids']['settings']['term_ids']['#multiple'] = TRUE;
    $form['parameter']['term_ids']['settings']['term_ids']['#size'] = 10;
  }
}