Episode five of the Shopware plugin series. Video here, and the code is in the GitHub repo.
Not everyone loads the product page
So far the questions only exist on the product page. A mobile app or a headless frontend never renders that page. They talk to Shopware through the Store API. If the questions should reach them too, they need a route there.
Every Store API request carries a sales channel access key, and Shopware answers from that sales channel's point of view: its language, its prices, and only the products it actually shows. Our route will live at:
/store-api/runio/product/{productId}/faq
Three PHP classes go into a SalesChannel folder next to the entity, plus a routes file, a service entry and a schema file for the docs.
Start with an abstract class
abstract class AbstractProductFaqRoute
{
abstract public function getDecorated(): AbstractProductFaqRoute;
abstract public function load(
string $productId,
Criteria $criteria,
SalesChannelContext $context,
): ProductFaqRouteResponse;
}
This looks like ceremony, but it's how Shopware keeps routes extensible. Other plugins type against the abstract class, so they can decorate our route: wrap it and change what it returns without touching our code. All of Shopware's own Store API routes follow this pattern.
The response class extends StoreApiResponse and just hands back the questions it found.
The route
#[Route(defaults: [PlatformRequest::ATTRIBUTE_ROUTE_SCOPE => [StoreApiRouteScope::ID]])]
class ProductFaqRoute extends AbstractProductFaqRoute
{
#[Route(
path: '/store-api/runio/product/{productId}/faq',
name: 'store-api.runio.product-faq',
methods: ['GET', 'POST'],
defaults: ['_entity' => 'runio_product_faq'],
)]
public function load(string $productId, Criteria $criteria, SalesChannelContext $context): ProductFaqRouteResponse
{
// The sales channel repository only finds products this sales channel shows
$productCriteria = (new Criteria([$productId]))->addFields(['parentId']);
$product = $this->productRepository->search($productCriteria, $context)->getEntities()->first();
if ($product === null) {
throw ProductException::productNotFound($productId);
}
// Variants share the questions of their parent product
$criteria
->addFilter(new EqualsFilter('productId', $product->get('parentId') ?? $productId))
->addFilter(new EqualsFilter('active', true))
->addSorting(new FieldSorting('position'));
return new ProductFaqRouteResponse($this->faqRepository->search($criteria, $context->getContext()));
}
}
There's a lot going on in a small method, so piece by piece.
The class carries the store-api scope. Shopware fills in the arguments for us: the ID from the URL, a Criteria built from the request body, and the sales channel context.
The product lookup goes through the sales channel repository, not the normal one. That repository only finds products this sales channel shows. So asking for the questions of a hidden product gets a 404, not its questions. I only load the parentId field, because that's all I need.
Then our filters go on top of whatever criteria the client sent. Clients can add their own filters, but they can't remove ours, so an inactive question never leaks out.
Wiring it up
In services.php the route has to be public, because Symfony calls it as a controller. And a small routes.php tells Shopware to read the route attributes from our classes:
return static function (RoutingConfigurator $routes): void {
$routes->import('../../Core/**/*Route.php', 'attribute');
};
Why are the results empty?
First test: four results, and every one of them is empty. That's not a bug. The Store API only returns fields that are flagged ApiAware, so internal data stays private unless you opt in. Add the flag to the fields you want to share:
(new StringField('question', 'question'))->addFlags(new ApiAware(), new Required()),
(new LongTextField('answer', 'answer'))->addFlags(new ApiAware(), new Required()),
Now the answers come through, and the inactive question is still hidden.
Letting clients choose
Because the route accepts a Criteria, clients get the usual Store API features for free. With includes and a limit, the response shrinks to only the question and answer fields:
{
"limit": 2,
"includes": { "runio_product_faq": ["question", "answer"] }
}
An unknown or hidden product returns a clean 404.
The last file is an OpenAPI schema in src/Resources/Schema/StoreApi. It adds the route to Shopware's API reference so frontend developers can actually find it. And the product page subscriber now calls this same route, so the storefront and every other frontend get the same answers from one place.
Recap
An abstract class others can decorate, a public route in the store-api scope, the sales channel repository for visibility, our filters on top of the client's criteria, and ApiAware on every field we want to share.
Next episode turns the questions into a CMS element, so shop owners can place them anywhere in Shopping Experiences.
Author Amer Malik Mohammed
Full-Stack Developer with 2+ years of experience in object-oriented PHP development (Symfony), JavaScript and MySQL. Specialized in e-commerce solutions (Shopware 5/6), REST API development and process automation in agile teams.
Contact author