You are here

public static function Utility::quantityPrefixData in Basic cart 8.2

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

Get Quantity prefix data.

Parameters

int $nid: Node id of content.

1 call to Utility::quantityPrefixData()
CartForm::buildForm in src/Form/CartForm.php
Form constructor.

File

src/Utility.php, line 415

Class

Utility
Utilty functions for basic cart.

Namespace

Drupal\basic_cart

Code

public static function quantityPrefixData($nid) {
  global $base_url;
  $langcode = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $url = new Url('basic_cart.cartremove', array(
    "nid" => $nid,
  ));
  $cart = Utility::getCart($nid);
  $basic_cart = array();
  $basic_cart['delete_url'] = $url
    ->toString();
  $basic_cart['module_url'] = $base_url . '/' . drupal_get_path('module', 'basic_cart');
  $basic_cart['notempty'] = FALSE;
  if (!empty($cart['cart'])) {
    $basic_cart['notempty'] = TRUE;
    $unit_price = $cart['cart']
      ->getTranslation($langcode)
      ->get('add_to_cart_price')
      ->getValue();
    $unit_price = isset($unit_price[0]['value']) ? $unit_price[0]['value'] : 0;
    $title = $cart['cart']
      ->getTranslation($langcode)
      ->get('title')
      ->getValue()[0]['value'];

    // Price and currency.
    $url = new Url('entity.node.canonical', array(
      "node" => $nid,
    ));
    $link = new Link($title, $url);
    $unit_price = isset($unit_price) ? $unit_price : 0;
    $unit_price = Utility::formatPrice($unit_price);
    $basic_cart['unit_price'] = $unit_price;
    $basic_cart['title_link'] = $link
      ->toString();
  }
  return $basic_cart;
}