【发布时间】:2022-08-16 23:57:19
【问题描述】:
我想在列表页面上显示变体产品的父产品编号。
但我不知道该怎么做。
谁能帮我这个?
标签: shopware shopware6 shopware6-app
我想在列表页面上显示变体产品的父产品编号。
但我不知道该怎么做。
谁能帮我这个?
标签: shopware shopware6 shopware6-app
服务定义:
<service id="MyPlugin\Subscriber\CustomListingSubscriber">
<tag name="kernel.event_subscriber"/>
<argument type="service" id="Doctrine\DBAL\Connection"/>
</service>
订户:
class CustomListingSubscriber implements EventSubscriberInterface
{
private Connection $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public static function getSubscribedEvents(): array
{
return [
ProductListingResultEvent::class => [
['handleListingResult', 0],
],
ProductSearchResultEvent::class => [
['handleListingResult', 0],
],
];
}
public function handleListingResult(ProductListingResultEvent $event): void
{
$ids = $event->getResult()->getEntities()->getIds();
$sql = 'SELECT LOWER(HEX(p.id)), pp.product_number
FROM product p
INNER JOIN product pp ON pp.id = p.parent_id AND pp.version_id = :version
WHERE p.id IN (:ids) AND p.version_id = :version';
$mapping = $this->connection->fetchAllKeyValue(
$sql,
['ids' => Uuid::fromHexToBytesList($ids), 'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
['ids' => Connection::PARAM_STR_ARRAY]
);
foreach ($mapping as $variantId => $parentProductNumber) {
$product = $event->getResult()->getEntities()->get($variantId);
if (!$product instanceof ProductEntity) {
continue;
}
$extension = new TextStruct();
$extension->setContent($parentProductNumber);
$product->addExtension('parentProductNumber', $extension);
}
}
}
树枝模板:
{{ product.extensions.parentProductNumber.content }}
【讨论】: