D8 Commerce - pierregermain/MyDrupal GitHub Wiki

Understand:

Get publication entities

function getPublicationsEntities() {

  // Get the entities ID
  $bundle = 'publication';
  $query = \Drupal::entityQuery('commerce_product');
  $query->condition('type', $bundle);
  $entity_ids = $query->execute();
  // Load the entity
  $entities = \Drupal::entityTypeManager()->getStorage('commerce_product')->loadMultiple($entity_ids);

  return $entities;
}

Query Orders

// Get orders using entityQuery
$order_ids = \Drupal::entityQuery('commerce_order')
  ->condition('order_id', $order_id)
  ->condition('state', $state)
  ->execute();

// Now we work with the found order
foreach ($order_ids as $order_id) {

  // Load entity
  $order = \Drupal\commerce_order\Entity\Order::load($order_id);

  // Change Timestamp
  $order->setChangedTime(time());
  $order->set('state', 'fulfillment')->save();

}

Understand:

Get publication entities

function getPublicationsEntities() {

  // Get the entities ID
  $bundle = 'publication';
  $query = \Drupal::entityQuery('commerce_product');
  $query->condition('type', $bundle);
  $entity_ids = $query->execute();
  // Load the entity
  $entities = \Drupal::entityTypeManager()->getStorage('commerce_product')->loadMultiple($entity_ids);

  return $entities;
}

Query Orders

// Get orders using entityQuery
$order_ids = \Drupal::entityQuery('commerce_order')
  ->condition('order_id', $order_id)
  ->condition('state', $state)
  ->execute();

// Now we work with the found order
foreach ($order_ids as $order_id) {

  // Load entity
  $order = \Drupal\commerce_order\Entity\Order::load($order_id);

  // Change Timestamp
  $order->setChangedTime(time());
  $order->set('state', 'fulfillment')->save();

}