You are here

function hook_uc_cart_display in Ubercart 8.4

Same name and namespace in other branches
  1. 7.3 uc_cart/uc_cart.api.php \hook_uc_cart_display()

Controls the display of an item in the cart.

Product type modules allow the creation of nodes that can be added to the cart. The cart determines how they are displayed through this hook. This is especially important for product kits, because it may be displayed as a single unit in the cart even though it is represented as several items.

This hook is only called for the module that owns the cart item in question, as set in $item->module.

Parameters

\Drupal\uc_cart\CartItemInterface $item: The item in the cart to display.

Return value

array A form array containing the following elements:

  • "nid"

    • #type: value
    • #value: The node id of the $item.
  • "module"
    • #type: value
    • #value: The module implementing this hook and the node represented by $item.
  • "remove"
    • #type: submit
    • #value: t('Remove'); when clicked, will remove $item from the cart.
  • "description"
    • #type: markup
    • #value: Themed markup (usually an unordered list) displaying extra information.
  • "title"
    • #type: markup
    • #value: The displayed title of the $item.
  • "#total"
    • type: float
    • value: Numeric price of $item. Notice the '#' signifying that this is not a form element but just a value stored in the form array.
  • "data"
    • #type: hidden
    • #value: The serialized $item->data.
  • "qty"
    • #type: textfield
    • #value: The quantity of $item in the cart. When "Update cart" is clicked, the customer's input is saved to the cart.
2 functions implement hook_uc_cart_display()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

uc_product_kit_uc_cart_display in uc_product_kit/uc_product_kit.module
Implements hook_uc_cart_display().
uc_product_uc_cart_display in uc_product/uc_product.module
Implements hook_uc_cart_display().
2 invocations of hook_uc_cart_display()
CartBlock::build in uc_cart/src/Plugin/Block/CartBlock.php
Builds and returns the renderable array for this block plugin.
CartForm::buildForm in uc_cart/src/Form/CartForm.php
Form constructor.

File

uc_cart/uc_cart.api.php, line 125
Hooks provided by the Cart module.

Code

function hook_uc_cart_display(CartItemInterface $item) {
  $node = $item->nid->entity;
  $element = [];
  $element['nid'] = [
    '#type' => 'value',
    '#value' => $node
      ->id(),
  ];
  $element['module'] = [
    '#type' => 'value',
    '#value' => 'uc_product',
  ];
  $element['remove'] = [
    '#type' => 'submit',
    '#value' => t('Remove'),
  ];
  if ($node
    ->access('view')) {
    $element['title'] = [
      '#type' => 'link',
      '#title' => $item->title,
      '#url' => $node
        ->toUrl(),
    ];
  }
  else {
    $element['title'] = [
      '#markup' => $item->title,
    ];
  }
  $element['#total'] = $item->price->value * $item->qty->value;
  $element['#suffixes'] = [];
  $element['data'] = [
    '#type' => 'hidden',
    '#value' => serialize($item->data
      ->first()
      ->toArray()),
  ];
  $element['qty'] = [
    '#type' => 'uc_quantity',
    '#title' => t('Quantity'),
    '#title_display' => 'invisible',
    '#default_value' => $item->qty->value,
    '#allow_zero' => TRUE,
  ];
  $element['description'] = [
    '#markup' => '',
  ];
  if ($description = uc_product_get_description($item)) {
    $element['description']['#markup'] = $description;
  }
  return $element;
}