OrderItemProduct.php in Commerce Core 8.2
File
modules/product/src/Plugin/Commerce/Condition/OrderItemProduct.php
View source
<?php
namespace Drupal\commerce_product\Plugin\Commerce\Condition;
use Drupal\commerce\EntityUuidMapperInterface;
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase;
use Drupal\commerce\Plugin\Commerce\Condition\PurchasableEntityConditionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OrderItemProduct extends ConditionBase implements PurchasableEntityConditionInterface, ContainerFactoryPluginInterface {
use ProductTrait;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityUuidMapperInterface $entity_uuid_mapper) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->productStorage = $entity_type_manager
->getStorage('commerce_product');
$this->entityUuidMapper = $entity_uuid_mapper;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('commerce.entity_uuid_mapper'));
}
public function evaluate(EntityInterface $entity) {
$this
->assertEntity($entity);
$order_item = $entity;
$purchased_entity = $order_item
->getPurchasedEntity();
if (!$purchased_entity || $purchased_entity
->getEntityTypeId() != 'commerce_product_variation') {
return FALSE;
}
$product_ids = $this
->getProductIds();
return in_array($purchased_entity
->getProductId(), $product_ids);
}
public function getPurchasableEntityIds() {
$variation_ids = [];
$product_ids = $this
->getProductIds();
if (!empty($product_ids)) {
foreach ($this->productStorage
->loadMultiple($product_ids) as $product) {
$variation_ids += $product
->getVariationIds();
}
}
return array_values($variation_ids);
}
public function getPurchasableEntities() {
if ($entity_ids = $this
->getPurchasableEntityIds()) {
$storage = $this->entityTypeManager
->getStorage('commerce_product_variation');
$entities = $storage
->loadMultiple($entity_ids);
}
return $entities ?? [];
}
}