You are here

public static function Utility::getCartData in Basic cart 8.4

Same name and namespace in other branches
  1. 8.6 src/Utility.php \Drupal\basic_cart\Utility::getCartData()
  2. 8 src/Utility.php \Drupal\basic_cart\Utility::getCartData()
  3. 8.0 src/Utility.php \Drupal\basic_cart\Utility::getCartData()
  4. 8.2 src/Utility.php \Drupal\basic_cart\Utility::getCartData()
  5. 8.3 src/Utility.php \Drupal\basic_cart\Utility::getCartData()
  6. 8.5 src/Utility.php \Drupal\basic_cart\Utility::getCartData()

Get Cart Data.

1 call to Utility::getCartData()
Utility::render in src/Utility.php
Render function.

File

src/Utility.php, line 345

Class

Utility
Utilty functions for basic cart.

Namespace

Drupal\basic_cart

Code

public static function getCartData() {
  $config = self::cartSettings();
  $cart = self::getCart();

  //$quantity_enabled = $config->get('quantity_status');
  $total_price = self::getTotalPrice();
  $cart_cart = isset($cart['cart']) ? $cart['cart'] : array();
  $basic_cart = array();
  $basic_cart['config']['quantity_enabled'] = $config
    ->get('quantity_status');
  $basic_cart['empty']['text'] = $config
    ->get('empty_cart');
  if (empty($cart_cart)) {
    $basic_cart['empty']['status'] = TRUE;
  }
  else {
    if (is_array($cart_cart) && count($cart_cart) >= 1) {
      foreach ($cart_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();
        $url = new Url('entity.node.canonical', [
          "node" => $nid,
        ]);
        $link = new Link($title[0]['value'], $url);
        $basic_cart['data']['contents'][$nid] = [
          "quantity" => $cart['cart_quantity'][$nid],
          'price_value' => isset($price_value[0]) ? self::formatPrice($price_value[0]['value']) : '',
          'link' => $link
            ->toString(),
        ];
      }
      $basic_cart['config']['total_price_label'] = $config
        ->get('total_price_label');
      $basic_cart['config']['total_price'] = self::formatPrice($total_price->total);
      $basic_cart['config']['vat_enabled'] = $config
        ->get('vat_state');
      $basic_cart['config']['vat_label'] = 'Total VAT';
      $basic_cart['config']['total_price_vat'] = self::formatPrice($total_price->vat);
      $basic_cart['config']['view_cart_button'] = $config
        ->get('view_cart_button');
      $url = new Url('basic_cart.cart');
      $basic_cart['config']['view_cart_url'] = $url
        ->toString();
      $basic_cart['empty']['status'] = FALSE;
    }
  }
  return $basic_cart;
}