You are here

function basic_cart_tokens in Basic cart 8

Same name and namespace in other branches
  1. 8.6 basic_cart.module \basic_cart_tokens()
  2. 8.0 basic_cart.module \basic_cart_tokens()
  3. 8.2 basic_cart.module \basic_cart_tokens()
  4. 8.3 basic_cart.module \basic_cart_tokens()
  5. 8.4 basic_cart.module \basic_cart_tokens()
  6. 8.5 basic_cart.module \basic_cart_tokens()

Implements hook_tokens().

File

./basic_cart.module, line 266

Code

function basic_cart_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  $utility = new Utility();
  $order = isset($data["node"]) ? $data["node"] : array();

  // The first thing that we're going to check for is the type of token - node,
  // user etc...
  if ($type == 'basic_cart_order') {

    // Loop through each of the available tokens.
    foreach ($tokens as $name => $original) {

      // Find the desired token by name
      switch ($name) {
        case 'products':
          $new = '';
          $get_cart = $utility::getCart();
          $cart = $get_cart['cart'];

          // Building the order details.
          $i = 0;
          $products = '';
          foreach ($cart as $nid => $node) {
            $langcode = $node
              ->language()
              ->getId();
            $price_value = $node
              ->getTranslation($langcode)
              ->get('add_to_cart_price')
              ->getValue();
            $title = $node
              ->getTranslation($langcode)
              ->get('title')
              ->getValue();
            $unit_price = $utility::formatPrice($price_value[0]['value']);
            $new .= ++$i . '. ' . $title[0]['value'] . "\t" . $get_cart['cart_quantity'][$nid] . ' x ' . $unit_price . "\n";
          }

          // Add the new value into the replacements array.
          $replacements[$original] = $new;
          break;
        case 'basic_cart_total_price':
          $total = Utility::getTotalPrice();
          $langcode = $order
            ->language()
            ->getId();
          $title = $order
            ->getTranslation($langcode)
            ->get('title')
            ->getValue();
          $replacements[$original] = !empty($total) && isset($total->total) ? $total->total : 0;
          break;
        case 'basic_cart_email':
          $langcode = $order
            ->language()
            ->getId();
          $email = $order
            ->getTranslation($langcode)
            ->get('basic_cart_email')
            ->getValue();
          $replacements[$original] = isset($email[0]['value']) ? $email[0]['value'] : "";
          break;
        case 'basic_cart_vat':
          $order = $data["node"];
          $total = Utility::getTotalPrice();
          $langcode = $order
            ->language()
            ->getId();
          $vat = $order
            ->getTranslation($langcode)
            ->get('basic_cart_vat')
            ->getValue();
          $replacements[$original] = isset($vat[0]['value']) ? $vat[0]['value'] : 0;
          break;
      }
    }
  }

  // Return the replacements.
  return $replacements;
}