You are here

function commerce_pricelist_entity_update in Commerce Pricelist 8

Implements hook_entity_update().

File

./commerce_pricelist.module, line 60
Contains commerce_pricelist.module..

Code

function commerce_pricelist_entity_update(EntityInterface $entity) {
  if ($entity instanceof PurchasableEntityInterface) {

    /** @var \Drupal\commerce\PurchasableEntityInterface $entity */
    foreach ($entity
      ->get('field_price_list_item') as $item) {
      $entityAdaper = $item
        ->get('entity')
        ->getTarget();
      if ($entityAdaper) {

        /** @var \Drupal\commerce_pricelist\Entity\PriceListItem $itemEntity */
        $itemEntity = $entityAdaper
          ->getValue();
        $itemEntity
          ->setPurchasedEntityId($entity
          ->id());
        if (!$itemEntity
          ->getName() && $entity
          ->getTitle()) {
          $itemEntity
            ->setName($entity
            ->getTitle());
        }
        if (!$itemEntity
          ->getPrice() && $entity
          ->getPrice()) {
          $itemEntity
            ->setPrice($entity
            ->getPrice());
        }
        $itemEntity
          ->save();
      }
    }
  }
}