You are here

commerce_rules_extra.module in Commerce Rules Extra 7.2

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

Main module file for Commerce Rules Extra.

File

commerce_rules_extra.module
View source
<?php

/**
 * @file
 * Main module file for Commerce Rules Extra.
 */

// Include all events, actions and conditions files.
// @TODO See if we can use hook_rules_file_info() to avoid includes on every
// call. Or try to switch to ctools plugins instead.
// @link https://www.drupal.org/node/2702975#comment-11466049
$mask = '/.*\\.inc$/';
$events = file_scan_directory(__DIR__ . '/includes/events', $mask);
$conditions = file_scan_directory(__DIR__ . '/includes/conditions', $mask);
$actions = file_scan_directory(__DIR__ . '/includes/actions', $mask);
$GLOBALS['_commerce_rules_extra_cre_includes'] = array(
  'events' => array(),
  'conditions' => array(),
  'actions' => array(),
);
foreach ($events as $item) {
  module_load_include('inc', 'commerce_rules_extra', 'includes/events/' . $item->name);
  $GLOBALS['_commerce_rules_extra_cre_includes']['events'][$item->name] = $item->filename;
}
foreach ($conditions as $item) {
  module_load_include('inc', 'commerce_rules_extra', 'includes/conditions/' . $item->name);
  $GLOBALS['_commerce_rules_extra_cre_includes']['conditions'][$item->name] = $item->filename;
}
foreach ($actions as $item) {
  module_load_include('inc', 'commerce_rules_extra', 'includes/actions/' . $item->name);
  $GLOBALS['_commerce_rules_extra_cre_includes']['actions'][$item->name] = $item->filename;
}

/**
 * Implements hook_entity_property_info_alter().
 */
function commerce_rules_extra_entity_property_info_alter(&$info) {
  $info['commerce_order']['properties']['commerce_order_quantity'] = [
    'type' => 'integer',
    'label' => t('Total quantity'),
    'description' => t('Total quantity of the order.'),
    'getter callback' => 'commerce_rules_extra_get_quantity_property',
    'computed' => TRUE,
  ];
}

/**
 * Callback function for the Total Quantity of the Order.
 */
function commerce_rules_extra_get_quantity_property($order, array $options, $name) {
  $wrapper = entity_metadata_wrapper('commerce_order', $order);
  return commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());
}

/**
 * Implements hook_theme().
 */
function commerce_rules_extra_theme($existing, $type, $theme, $path) {
  return [
    'form--cre_condition' => [
      'template' => 'form--cre_condition',
      'render element' => 'form',
      'path' => $path . '/templates',
    ],
  ];
}

/**
 * Implements hook_commerce_checkout_pane_info_alter().
 *
 * Invoke all rules for process_checkout_pane event and alter panes properties returned by rules.
 */
function commerce_rules_extra_commerce_checkout_pane_info_alter(&$checkout_panes) {
  global $_commerce_rules_extra_pane_changes;
  if (!$_commerce_rules_extra_pane_changes) {
    $_commerce_rules_extra_pane_changes = [];
  }
  $urls = [
    'checkout',
    'system',
  ];
  drupal_alter('checkout_pane_urls', $urls);
  if (in_array(arg(0), $urls)) {
    $commerce_order = menu_get_object('commerce_order');
    if (NULL == $commerce_order) {
      $commerce_order = commerce_cart_get_properties(FALSE, [], 'current_cart_order');
    }
    rules_invoke_all('commerce_rules_extra_process_checkout_pane', $commerce_order);
    foreach ($_commerce_rules_extra_pane_changes as $pane_id => $changes) {
      $checkout_panes[$pane_id]['enabled'] = $changes['enabled'];
      if ($changes['weight'] != '') {
        $checkout_panes[$pane_id]['weight'] = $changes['weight'];
      }
      if ($changes['page_id'] != '<same>') {
        $checkout_panes[$pane_id]['page'] = $changes['page_id'];
      }
    }
  }
}

/**
 * Implements hook_commerce_checkout_page_info_alter().
 *
 * Invoke all rules for process_checkout_page event and alter pages properties returned by rules
 */
function commerce_rules_extra_commerce_checkout_page_info_alter(&$checkout_pages) {
  global $_commerce_rules_extra_page_changes;
  if (!$_commerce_rules_extra_page_changes) {
    $_commerce_rules_extra_page_changes = [];
  }
  $urls = [
    'cart',
    'checkout',
    'system',
  ];
  drupal_alter('checkout_page_urls', $urls);
  if (in_array(arg(0), $urls)) {

    // As of this issue https://www.drupal.org/node/1855900 it can easily happen
    // that this hook is invoked checkout_pages definitions that don't have any
    // defaults set. To avoid notices we set the defaults on our own. The code
    // below has to exactly match what's done in commerce_checkout_pages().
    $count = 0;
    foreach ($checkout_pages as $page_id => $checkout_page) {
      $defaults = [
        'page_id' => $page_id,
        'name' => $checkout_page['title'],
        'title' => '',
        'help' => '',
        'status_cart' => TRUE,
        'buttons' => TRUE,
        'back_value' => t('Go back'),
        'submit_value' => t('Continue to next step'),
        'prev_page' => NULL,
        'next_page' => NULL,
      ];
      $checkout_pages[$page_id] += $defaults;

      // Set a weight that preserves the order of 0 weighted pages.
      if (empty($checkout_page['weight'])) {
        $checkout_pages[$page_id]['weight'] = $count++ / count($checkout_pages);
      }
    }
    rules_invoke_all('commerce_rules_extra_process_checkout_page');
    foreach ($_commerce_rules_extra_page_changes as $page_id => $changes) {
      if ($changes['title'] != '') {
        $checkout_pages[$page_id]['title'] = $changes['title'];
      }
      if ($changes['help'] != '') {
        $checkout_pages[$page_id]['help'] = $changes['help'];
      }
      if ($changes['submit_value'] != '') {
        $checkout_pages[$page_id]['submit_value'] = $changes['submit_value'];
      }
    }
  }
}