OrderItemMatcher.php in Commerce Core 8.2
File
modules/cart/src/OrderItemMatcher.php
View source
<?php
namespace Drupal\commerce_cart;
use Drupal\commerce_cart\Event\CartEvents;
use Drupal\commerce_cart\Event\OrderItemComparisonFieldsEvent;
use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class OrderItemMatcher implements OrderItemMatcherInterface {
protected $entityTypeManager;
protected $eventDispatcher;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher) {
$this->entityTypeManager = $entity_type_manager;
$this->eventDispatcher = $event_dispatcher;
}
public function match(OrderItemInterface $order_item, array $order_items) {
$order_items = $this
->matchAll($order_item, $order_items);
return count($order_items) ? $order_items[0] : NULL;
}
public function matchAll(OrderItemInterface $order_item, array $order_items) {
$purchased_entity = $order_item
->getPurchasedEntity();
if (empty($purchased_entity)) {
return [];
}
$comparison_fields = [
'type',
'purchased_entity',
];
$comparison_fields = array_merge($comparison_fields, $this
->getCustomFields($order_item));
$event = new OrderItemComparisonFieldsEvent($comparison_fields, $order_item);
$this->eventDispatcher
->dispatch(CartEvents::ORDER_ITEM_COMPARISON_FIELDS, $event);
$comparison_fields = $event
->getComparisonFields();
$comparison_fields = array_unique($comparison_fields);
$matched_order_items = [];
foreach ($order_items as $existing_order_item) {
foreach ($comparison_fields as $field_name) {
if (!$existing_order_item
->hasField($field_name) || !$order_item
->hasField($field_name)) {
continue 2;
}
$existing_order_item_field = $existing_order_item
->get($field_name);
$order_item_field = $order_item
->get($field_name);
$existing_order_item_field = $existing_order_item_field
->filterEmptyItems();
$order_item_field = $order_item_field
->filterEmptyItems();
if (!$existing_order_item_field
->equals($order_item_field)) {
continue 2;
}
}
$matched_order_items[] = $existing_order_item;
}
return $matched_order_items;
}
protected function getCustomFields(OrderItemInterface $order_item) {
$field_names = [];
$storage = $this->entityTypeManager
->getStorage('entity_form_display');
$form_display = $storage
->load('commerce_order_item.' . $order_item
->bundle() . '.' . 'add_to_cart');
if ($form_display) {
$field_names = array_keys($form_display
->getComponents());
$field_names = array_diff($field_names, [
'purchased_entity',
'quantity',
'created',
]);
}
return $field_names;
}
}