Skip to main content

Adding a Custom DataField

The easiest way to add a custom DataField is to extend one of the built-in abstract classes. These handle the null check on the context and provide a type-safe method signature.


Example: Custom product field

The following example creates a DataField that returns the Shopware custom field my_plugin_part_number of a product.

1. Create the class

<?php

declare(strict_types=1);

namespace MyPlugin\Connector\DataField\Field;

use AgiqonConnector\Connector\DataField\Field\Product\AbstractProductDataField;
use AgiqonConnector\Connector\System\ConnectorSystemType;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\Context;

readonly class MyPartNumberField extends AbstractProductDataField
{
public function __construct()
{
parent::__construct(
'myPartNumber', // technicalName
[ConnectorSystemType::OCI, ConnectorSystemType::cXML] // available for both system types
);
}

public function getValue(ProductEntity $product, Context $context): ?string
{
$customFields = $product->getCustomFields();

return $customFields['my_plugin_part_number'] ?? null;
}
}

AbstractProductDataField already carries #[DataFieldTypeAttribute(DataFieldType::Product)] and checks that a ProductEntity is present in the context. getValue() receives the product directly.

2. Register in services.xml

<service id="MyPlugin\Connector\DataField\Field\MyPartNumberField">
<tag name="agiqon_connector.data_field"/>
</service>

The tag agiqon_connector.data_field automatically registers the field in the DataFieldRegistry. It is then selectable in the connector configuration under the name myPartNumber.


Available abstract classes

A ready-made abstract class exists for every built-in type. Each one handles the null check on the context and provides a type-safe getValue() signature:

ClassTypegetValue() signature
AbstractContextDataFieldcontextgetValue(SalesChannelContext): ?string
AbstractCustomizedProductDataFieldcustomized_productgetValue(LineItem, Context): ?string
AbstractLineItemDataFieldline_itemgetValue(LineItem, Context): ?string
AbstractProductDataFieldproductgetValue(ProductEntity, Context): ?string
AbstractProductCustomFieldDataFieldproductgetValue(ProductEntity, string $customField, Context): ?string
AbstractShippingDataFieldshippinggetValue(ShippingMethodEntity, Cart): ?string
AbstractSystemFieldsystemgetValue(ConnectorSystemEntity): ?string
AbstractTransmissionFieldsessiongetValue(ConnectorSession): ?string
tip

AbstractProductCustomFieldDataField is designed specifically for Shopware Custom Fields — it provides both the ProductEntity and the configured custom field key ($customField) directly.