You are here

public static function Settings::getTotalPrice in Basic cart 8

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

* Returns the final price for the shopping cart. * *

Return value

mixed $total_price * The total price for the shopping cart.

1 call to Settings::getTotalPrice()
Utility::getCartData in src/Utility.php

File

src/Settings.php, line 122

Class

Settings

Namespace

Drupal\basic_cart

Code

public static function getTotalPrice() {
  $config = self::cartSettings();
  $vat = $config
    ->get('vat_state');

  // Building the return array.
  $return = array(
    'price' => 0,
    'vat' => 0,
    'total' => 0,
  );
  $cart = static::getCart();
  if (empty($cart)) {
    return (object) $return;
  }
  $total_price = 0;
  foreach ($cart['cart'] as $nid => $node) {
    $langcode = $node
      ->language()
      ->getId();
    $value = $node
      ->getTranslation($langcode)
      ->get('add_to_cart_price')
      ->getValue();
    if (isset($cart['cart_quantity'][$nid]) && isset($value[0]['value'])) {
      $total_price += $cart['cart_quantity'][$nid] * $value[0]['value'];
    }
    $value = 0;
  }
  $return['price'] = $total_price;

  // Checking whether to apply the VAT or not.
  $vat_is_enabled = (int) $config
    ->get('vat_state');
  if (!empty($vat_is_enabled) && $vat_is_enabled) {
    $vat_value = (double) $config
      ->get('vat_value');
    $vat_value = $total_price * $vat_value / 100;
    $total_price += $vat_value;

    // Adding VAT and total price to the return array.
    $return['vat'] = $vat_value;
  }
  $return['total'] = $total_price;
  return (object) $return;
}