LineItemContextResolver
Why do resolvers exist?
When building a transmission, the connector iterates over all positions (line items) in the cart. A cart line item can have different types:
product– a regular Shopware productcustomized_product– a configurable product- custom types – e.g. pseudo-products from a third-party plugin
Each type may require a different approach to build the DataFieldResolutionContext (e.g. a product line item needs the ProductEntity to be loaded from the repository first).
The AbstractLineItemContextResolver encapsulates this logic and determines whether it is responsible for a given line item.
Structure
abstract class AbstractLineItemContextResolver
{
// Returns whether this resolver handles the given line item.
abstract public function supports(LineItem $lineItem): bool;
// Builds the DataFieldResolutionContext (or returns null to skip the line item).
public function resolve(
LineItem $lineItem,
Cart $cart,
ConnectorSystemEntity $system,
ConnectorSession $session,
SalesChannelContext $salesChannelContext
): ?DataFieldResolutionContext { ... }
}
The default implementation of resolve() creates a DataFieldResolutionContext directly with the line item. For more complex cases (e.g. repository lookups), resolve() can be overridden.
Built-in resolvers
| Class | supports() | Special behavior |
|---|---|---|
ProductLineItemContextResolver | $lineItem->getType() === 'product' | Loads the ProductEntity via sales_channel.product.repository |
CustomizedProductLineItemContextResolver | Customized product line items | Loads the base product and adds Customized Products data |
Adding a custom resolver
A custom resolver is needed when handling custom line item types (e.g. from a third-party plugin).
class MyCustomContextResolver extends AbstractLineItemContextResolver
{
public function supports(LineItem $lineItem): bool
{
return $lineItem->getType() === 'my-custom-type';
}
// Override resolve() only if the default context is not sufficient
}
Registration in services.xml:
<service id="MyPlugin\Connector\DataField\LineItemContextResolver\MyCustomContextResolver">
<tag name="agiqon_connector.line_item_context_resolver"/>
</service>
The tag agiqon_connector.line_item_context_resolver ensures the resolver is automatically discovered by the connector.