OrderProductCategory.php in Commerce Core 8.2
File
modules/product/src/Plugin/Commerce/Condition/OrderProductCategory.php
View source
<?php
namespace Drupal\commerce_product\Plugin\Commerce\Condition;
use Drupal\commerce\EntityUuidMapperInterface;
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OrderProductCategory extends ConditionBase implements ContainerFactoryPluginInterface {
use ProductCategoryTrait;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, EntityUuidMapperInterface $entity_uuid_mapper) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
$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_field.manager'), $container
->get('entity_type.manager'), $container
->get('commerce.entity_uuid_mapper'));
}
public function evaluate(EntityInterface $entity) {
$this
->assertEntity($entity);
$order = $entity;
$term_ids = $this
->getTermIds();
foreach ($order
->getItems() as $order_item) {
$purchased_entity = $order_item
->getPurchasedEntity();
if (!$purchased_entity || $purchased_entity
->getEntityTypeId() != 'commerce_product_variation') {
continue;
}
$referenced_ids = $this
->getReferencedIds($purchased_entity
->getProduct());
if (array_intersect($term_ids, $referenced_ids)) {
return TRUE;
}
}
return FALSE;
}
}