You are here

class Settings in Basic cart 8

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

Hierarchy

Expanded class hierarchy of Settings

3 files declare their use of Settings
CartSession.php in src/CartSession.php
CartTable.php in src/CartTable.php
Utility.php in src/Utility.php
Contains \Drupal\basic_cart\Utility
1 string reference to 'Settings'
basic_cart.links.task.yml in ./basic_cart.links.task.yml
basic_cart.links.task.yml

File

src/Settings.php, line 10

Namespace

Drupal\basic_cart
View source
class Settings {
  protected $checkout_settings;
  protected $cart_settings;
  const FIELD_ADDTOCART = 'addtocart';
  const FIELD_ORDERCONNECT = 'orderconnect';
  const BASICCART_ORDER = 'basic_cart_order';

  // Force Extending class to define this method
  public function checkoutSettings() {
    $return = \Drupal::config('basic_cart.checkout');
    return $return;
  }
  public static function cartSettings() {
    $return = \Drupal::config('basic_cart.settings');
    return $return;
  }
  public static function cartUpdatedMessage() {
    $config = static::cartSettings();
    drupal_set_message(t($config
      ->get('cart_updated_message')));
  }

  /**
   * Formats the input $price in the desired format.
   *
   * @param float $price
   *   The price in the raw format.
   * @return $price
   *   The price in the custom format.
   */
  public static function formatPrice($price) {
    $config = self::cartSettings();
    $format = $config
      ->get('price_format');
    $currency = $config
      ->get('currency');
    $price = (double) $price;
    switch ($format) {
      case 0:
        $price = number_format($price, 2, ',', ' ') . ' ' . $currency;
        break;
      case 1:
        $price = number_format($price, 2, '.', ' ') . ' ' . $currency;
        break;
      case 2:
        $price = number_format($price, 2, '.', ',') . ' ' . $currency;
        break;
      case 3:
        $price = number_format($price, 2, ',', '.') . ' ' . $currency;
        break;
      case 4:
        $price = $currency . ' ' . number_format($price, 2, ',', ' ');
        break;
      case 5:
        $price = $currency . ' ' . number_format($price, 2, '.', ' ');
        break;
      case 6:
        $price = $currency . ' ' . number_format($price, 2, '.', ',');
        break;
      case 7:
        $price = $currency . ' ' . number_format($price, 2, ',', '.');
        break;
      default:
        $price = number_format($price, 2, ',', ' ') . ' ' . $currency;
        break;
    }
    return $price;
  }

  /**
   * Returns the available price formats.
   *
   * @return $formats
   *   A list with the available price formats.
   */
  public static function listPriceFormats() {
    $config = self::cartSettings();
    $currency = $config
      ->get('currency');
    return array(
      0 => t('1 234,00 @currency', array(
        '@currency' => $currency,
      )),
      1 => t('1 234.00 @currency', array(
        '@currency' => $currency,
      )),
      2 => t('1,234.00 @currency', array(
        '@currency' => $currency,
      )),
      3 => t('1.234,00 @currency', array(
        '@currency' => $currency,
      )),
      4 => t('@currency 1 234,00', array(
        '@currency' => $currency,
      )),
      5 => t('@currency 1 234.00', array(
        '@currency' => $currency,
      )),
      6 => t('@currency 1,234.00', array(
        '@currency' => $currency,
      )),
      7 => t('@currency 1.234,00', array(
        '@currency' => $currency,
      )),
    );
  }

  /**
   * Returns the final price for the shopping cart.
   *
   * @return mixed $total_price
   *   The total price for the shopping cart.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Settings::$cart_settings protected property
Settings::$checkout_settings protected property
Settings::BASICCART_ORDER constant 1
Settings::cartSettings public static function
Settings::cartUpdatedMessage public static function
Settings::checkoutSettings public function
Settings::FIELD_ADDTOCART constant 1
Settings::FIELD_ORDERCONNECT constant 1
Settings::formatPrice public static function Formats the input $price in the desired format.
Settings::getTotalPrice public static function * Returns the final price for the shopping cart. * *
Settings::listPriceFormats public static function Returns the available price formats.