You are here

function basic_cart_entity_presave in Basic cart 8.6

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

Implements hook_entity_presave().

File

./basic_cart.module, line 101
Basic cart module file.

Code

function basic_cart_entity_presave(EntityInterface $node) {

  // Get the cart contents for a new orders unless order already has filled data
  // to allow programmatical creation of an order.
  if ($node
    ->isNew() && Utility::isBasicCartOrder($node
    ->bundle()) && Utility::isOrderEmpty($node)) {
    $get_price = Utility::getTotalPrice();
    $cart = Utility::getCart();
    $target_ids = [];
    if (!empty($cart['cart_quantity'])) {
      foreach ($cart['cart_quantity'] as $key => $value) {
        $target_ids[] = [
          'target_id' => $key,
          'quantity' => $value,
        ];
      }
    }
    elseif (!empty($cart['cart'])) {
      $target_ids = array_keys($cart['cart']);
    }
    $node
      ->set('basic_cart_vat', $get_price->vat);
    $node
      ->set('basic_cart_total_price', $get_price->total);
    $node
      ->set('basic_cart_content', $target_ids);
  }
}