You are here

public function SchedulerCommerceProductSetupTrait::getProduct in Scheduler 2.x

Gets a product from storage.

For nodes, there is drupalGetNodeByTitle() but nothing similar exists to help Product testing. See getMediaItem for more details.

Parameters

string $name: Optional name text to match on. If given and no match, returns NULL. If no $name is given then returns the product with the highest id value.

Return value

\Drupal\commerce_product\Entity\ProductInterface The commerce product object.

File

tests/src/Traits/SchedulerCommerceProductSetupTrait.php, line 178

Class

SchedulerCommerceProductSetupTrait
Additional setup trait for Scheduler tests that use Commerce Product.

Namespace

Drupal\Tests\scheduler\Traits

Code

public function getProduct(string $name = NULL) {
  $query = $this->productStorage
    ->getQuery()
    ->accessCheck(FALSE)
    ->sort('product_id', 'DESC');
  if (!empty($name)) {
    $query
      ->condition('title', $name);
  }
  $result = $query
    ->execute();
  if (count($result)) {
    $id = reset($result);
    return $this->productStorage
      ->load($id);
  }
  else {
    return NULL;
  }
}