symfony - 商店用品 6 : How to add Products with variation as childrens to Parent Products in LineItems

标签 symfony symfony5 shopware

我正在尝试在 shopware 6 中构建此页面:

Product with Variation in Warencorb

但由于在 shopware 中 6 种具有变异的产品是分开的(下图),我无法做到这一点。

Symfony Shopware6 LineItems

我需要将具有变体的产品分组到父产品下。 有人有想法吗?

我现在正在处理的订阅者:

use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class AddDataToPage implements EventSubscriberInterface

{   

public static function getSubscribedEvents()
{
    return [BeforeLineItemAddedEvent::class => 'onLineItemAdded'];
}

/**
 * @param onLineItemAdded $event
 * @throws \Shopware\Core\Checkout\Cart\Exception\InvalidPayloadException
 */
public function onLineItemAdded(BeforeLineItemAddedEvent $event)
{
   
    $lineitems = $event->getLineItem();
    

    // I need a [IF] here: if product has a variation and parent product id is the same add the code below
    $lineitems->setPayloadValue("myVar", "test2");
}

最佳答案

我建议采取以下步骤:

  1. 装饰AbstractCartItemAddRoute
  2. 获取将要添加的产品的父产品,并为其添加广告
  3. 将实际变体添加为子变体
  4. 添加一个 CartProcessor 或修饰 ProductProcessor 以使用父产品计算第一级项目的价格。这应该是所有变体位置的总和

当然,您应该在第 2 步中检查父项或变体子项是否存在并更新它。

关于symfony - 商店用品 6 : How to add Products with variation as childrens to Parent Products in LineItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68127239/

相关文章:

php - 如何在 Shopware 6 插件 ZIP 中包含重叠的 Composer 依赖项?

symfony表单构建器更新选项字段

php - Symfony httplug 包句柄缺少 EventSubscriberInterface

symfony - 如何在 Symfony EasyAdmin 4 中使用关联字段

symfony - Shopware 6 后端 Controller 路径

php - 轻松管理 : How to customize a field template in the form view?

php - 带有 PHP 和 SQLite 的数据库应用程序在数据库中返回空值

php - 在 Symfony2 中生成具有特定方案的绝对 URL

forms - Symfony 5.2 中的文件名未显示在表单中