You are here

uc_order.api.php in Ubercart 8.4

Same filename and directory in other branches
  1. 7.3 uc_order/uc_order.api.php

Hooks provided by the Order module.

File

uc_order/uc_order.api.php
View source
<?php

/**
 * @file
 * Hooks provided by the Order module.
 */

/**
 * @addtogroup hooks
 * @{
 */
use Drupal\uc_order\OrderInterface;
use Drupal\uc_order\OrderProductInterface;

/**
 * Alters the line item plugin definitions.
 *
 * @param array &$items
 *   Array of line item plugin definitions passed by reference.
 */
function hook_uc_line_item_alter(array &$items) {

  // Tax amounts are added in to other line items, so the actual tax line
  // items should not be added to the order total.
  $items['tax']['calculated'] = FALSE;
}

/**
 * Adds links to local tasks for orders on the admin's list of orders.
 *
 * @param \Drupal\uc_order\OrderInterface $order
 *   An order object.
 *
 * @return array
 *   An array of operations links. Each link has the following keys:
 *   - title: The title of page being linked.
 *   - href: The link path. Do not use url(), but do use the $order's order_id.
 *   - weight: Sets the display order of operations.
 */
function hook_uc_order_actions(OrderInterface $order) {
  $account = \Drupal::currentUser();
  $actions = [];
  if ($account
    ->hasPermission('fulfill orders')) {
    $result = db_query("SELECT COUNT(nid) FROM {uc_order_products} WHERE order_id = :id AND data LIKE :data", [
      ':id' => $order
        ->id(),
      ':data' => '%s:9:\\"shippable\\";s:1:\\"1\\";%',
    ]);
    if ($result
      ->fetchField()) {
      $actions['package'] = [
        'title' => t('Package'),
        'href' => 'admin/store/orders/' . $order
          ->id() . '/packages',
        'weight' => 12,
      ];
      $result = db_query("SELECT COUNT(package_id) FROM {uc_packages} WHERE order_id = :id", [
        ':id' => $order
          ->id(),
      ]);
      if ($result
        ->fetchField()) {
        $actions['ship'] = [
          'title' => t('Ship'),
          'href' => 'admin/store/orders/' . $order
            ->id() . '/shipments',
          'weight' => 13,
        ];
      }
    }
  }
  return $actions;
}

/**
 * Allows the local task icons for orders to be altered.
 *
 * @param array &$actions
 *   A set of actions as defined in hook_uc_order_actions().
 * @param \Drupal\uc_order\OrderInterface $order
 *   An order object.
 */
function hook_uc_order_actions_alter(array &$actions, OrderInterface $order) {
  $actions['view']['title'] = t('Display');
}

/**
 * Verifies whether an order may be deleted.
 *
 * @param \Drupal\uc_order\OrderInterface $order
 *   An order object.
 *
 * @return bool
 *   FALSE if the order should not be deleted.
 */
function hook_uc_order_can_delete(OrderInterface $order) {
  if (uc_payment_load_payments($order
    ->id())) {
    return FALSE;
  }
}

/**
 * Alters order pane plugin definitions.
 *
 * @param array $panes
 *   Keys are plugin IDs. Values are plugin definitions.
 */
function hook_uc_order_pane_alter(array &$panes) {
  $panes['payment']['title'] = 'Payment information';
}

/**
 * Allows modules to alter order products when they're loaded with an order.
 *
 * Nothing should be returned. Hook implementations should receive the
 * $product object by reference and alter it directly.
 *
 * @param \Drupal\uc_order\OrderProductInterface &$product
 *   The product object as found in the $order object.
 * @param \Drupal\uc_order\OrderInterface $order
 *   The order object to which the product belongs.
 */
function hook_uc_order_product_alter(OrderProductInterface &$product, OrderInterface $order) {
  $product->model = 'SKU';
}

/**
 * Acts on order products being loaded from the database.
 *
 * This hook is invoked during order product loading, which is handled by
 * entity_load(), via the EntityCRUDController.
 *
 * @param array $order_products
 *   An array of order product entities being loaded, keyed by id.
 *
 * @see hook_entity_load()
 */
function hook_uc_order_product_load(array $order_products) {
  $result = db_query('SELECT pid, foo FROM {mytable} WHERE pid IN(:ids[])', [
    ':ids[]' => array_keys($entities),
  ]);
  foreach ($result as $record) {
    $entities[$record->pid]->foo = $record->foo;
  }
}

/**
 * Responds when an order product is inserted.
 *
 * This hook is invoked after the order product is inserted into the database.
 *
 * @param \Drupal\uc_order\OrderProductInterface $order_product
 *   The order product that is being inserted.
 *
 * @see hook_entity_insert()
 */
function hook_uc_order_product_insert(OrderProductInterface $order_product) {
  db_insert('mytable')
    ->fields([
    'id' => entity_id('uc_order_product', $order_product),
    'extra' => print_r($order_product, TRUE),
  ])
    ->execute();
}

/**
 * Acts on an order product being inserted or updated.
 *
 * This hook is invoked before the order product is saved to the database.
 *
 * @param \Drupal\uc_order\OrderProductInterface $order_product
 *   The order product that is being inserted or updated.
 *
 * @see hook_entity_presave()
 */
function hook_uc_order_product_presave(OrderProductInterface $order_product) {
  $order_product->name = 'foo';
}

/**
 * Hook for responding to an order product being updated.
 *
 * Invoked after the order product has been updated in the database.
 *
 * @param \Drupal\uc_order\OrderProductInterface $order_product
 *   The order product that is being updated.
 *
 * @see hook_entity_update()
 */
function hook_uc_order_product_update(OrderProductInterface $order_product) {
  db_update('mytable')
    ->fields([
    'extra' => print_r($order_product, TRUE),
  ])
    ->condition('opid', entity_id('uc_order_product', $order_product))
    ->execute();
}

/**
 * Responds after order product deletion.
 *
 * This hook is invoked after the order product has been removed from the
 * database.
 *
 * @param \Drupal\uc_order\OrderProductInterface $order_product
 *   The order product that is being deleted.
 *
 * @see hook_entity_delete()
 * @see hook_uc_order_edit_form_product_remove()
 */
function hook_uc_order_product_delete(OrderProductInterface $order_product) {
  $connection = \Drupal::database();
  $connection
    ->delete('mytable')
    ->condition('opid', entity_id('uc_order_product', $order_product))
    ->execute();
}

/**
 * Allow modules to specify whether a product is shippable.
 *
 * @param \Drupal\uc_order\OrderProductInterface|\Drupal\uc_cart\CartItemInterface $product
 *   The product to check. May be a cart item or an order product.
 *
 * @return bool
 *   TRUE to specify that this product is shippable.
 */
function hook_uc_order_product_can_ship($product) {
  $roles = db_query('SELECT * FROM {uc_roles_products} WHERE nid = :nid', [
    ':nid' => $product->nid->target_id,
  ]);
  foreach ($roles as $role) {

    // If the model is empty, keep looking. (Everyone needs a role model...)
    if (empty($role->model)) {
      continue;
    }

    // If there's an adjusted SKU, use it... otherwise use the node SKU.
    $sku = empty($product->data['model']) ? $product->model->value : $product->data['model'];

    // Keep looking if it doesn't match.
    if ($sku != $role->model) {
      continue;
    }
    return $role->shippable;
  }
}

/**
 * @} End of "addtogroup hooks".
 */

Functions

Namesort descending Description
hook_uc_line_item_alter Alters the line item plugin definitions.
hook_uc_order_actions Adds links to local tasks for orders on the admin's list of orders.
hook_uc_order_actions_alter Allows the local task icons for orders to be altered.
hook_uc_order_can_delete Verifies whether an order may be deleted.
hook_uc_order_pane_alter Alters order pane plugin definitions.
hook_uc_order_product_alter Allows modules to alter order products when they're loaded with an order.
hook_uc_order_product_can_ship Allow modules to specify whether a product is shippable.
hook_uc_order_product_delete Responds after order product deletion.
hook_uc_order_product_insert Responds when an order product is inserted.
hook_uc_order_product_load Acts on order products being loaded from the database.
hook_uc_order_product_presave Acts on an order product being inserted or updated.
hook_uc_order_product_update Hook for responding to an order product being updated.