【问题标题】:How to show parent product number on listing page in Shopware 6?如何在 Shopware 6 的列表页面上显示父产品编号?
【发布时间】:2022-08-16 23:57:19
【问题描述】:

我想在列表页面上显示变体产品的父产品编号。
但我不知道该怎么做。
谁能帮我这个?

    标签: shopware shopware6 shopware6-app


    【解决方案1】:
    • 为列表结果事件添加订阅者。
    • 从变体 ID 获取键值对到父产品编号。
    • 使用键值对设置受影响产品对象的扩展。
    • 在您的产品框模板中打印扩展的内容。

    服务定义:

    <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 }}
    

    【讨论】:

      猜你喜欢
      • 2022-09-26
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      相关资源
      最近更新 更多