commerce_product_bundles-8.x-1.0/src/Plugin/Commerce/Condition/OrderItemBundleProduct.php
src/Plugin/Commerce/Condition/OrderItemBundleProduct.php
<?php namespace Drupal\commerce_product_bundles\Plugin\Commerce\Condition; use Drupal\commerce\EntityUuidMapperInterface; use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides the product bundle condition for order items. * * @CommerceCondition( * id = "order_bundle_item_product", * label = @Translation("Product Bundle"), * display_label = @Translation("Specific product bundles"), * category = @Translation("Commerce Product Bundles"), * entity_type = "commerce_order_item", * weight = -1, * ) */ class OrderItemBundleProduct extends ConditionBase implements ContainerFactoryPluginInterface { use ProductBundleTrait; /** * OrderBundleItemProduct constructor. * * @param array $configuration * @param $plugin_id * @param $plugin_definition * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * @param \Drupal\commerce\EntityUuidMapperInterface $entity_uuid_mapper * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ 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->productBundleStorage = $entity_type_manager->getStorage('commerce_product_bundles'); $this->entityUuidMapper = $entity_uuid_mapper; } /** * {@inheritdoc} */ 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') ); } /** * {@inheritdoc} */ public function evaluate(EntityInterface $entity) { $this->assertEntity($entity); /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */ $order_item = $entity; /** @var \Drupal\commerce_product_bundles\Entity\ProductBundleVariationInterface $purchased_entity */ $purchased_entity = $order_item->getPurchasedEntity(); if (!$purchased_entity || $purchased_entity->getEntityTypeId() != 'commerce_bundle_variation') { return FALSE; } $product_ids = $this->getProductBundleIds(); return in_array($purchased_entity->getBundleProductId(), $product_ids); } }