I'm using:
- Drupal 9.1.7
- Commerce 2.24
I'm trying to create my custom condition (based on https://docs.drupalcommerce.org/commerce2/developer-guide/core/conditions). However parent entity is always NULL. Seems like parent entity is only working when I specify commerce_order
as entity_type
only, but I need commerce_order_item
. ParentEntityAwareInterface
and ParentEntityAwareTrait
are in use. Debugging show that:
public function evaluate(EntityInterface $entity) {
$this->assertEntity($entity);
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
$order_item = $entity;
/** @var \Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
$promotion = $this->parentEntity;
//$promotion is NULL
// rest of the code
}
$promotion
is always null.
Annotation:
* @CommerceCondition(
* id = "myproj_product_condition",
* label = @Translation("Product chooser"),
* category = @Translation("Product chooser"),
* entity_type = "commerce_order_item",
* parent_entity_type = "commerce_promotion",
* weight = 9,
* )
$this->parentEntity
would beNULL
, but the explanation could be inPromotion::getConditions()
,Promotion::apply()
, andPromotion::applies()
.Promotion::applies()
: Filter the conditions just in case there are leftover order item conditions (which have been moved to offer conditions). (Emphasis is mine.)evaluate()
method is running before\Drupal\commerce\Plugin\Commerce\Condition\ParentEntityAwareTrait::setParentEntity
, so that's why parent entity is NULL. Strange.setParentEntity()
is not always called. For what I can see, reading the code, the only classes calling that method arePromotion
, but only for commerce_order conditions, andPaymentGateway
.Promotion
class but asks to get aOrderItem
instance as argument? Is there code that assigns those conditions to an instance of theOfferItem
orOffer
class instead of thePromotion
class as the class annotation says?