首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Shopware 6客户小组扩展

Shopware 6客户小组扩展
EN

Stack Overflow用户
提问于 2021-01-14 14:03:46
回答 2查看 805关注 0票数 0

我正在尝试扩展Shopware 6中的customer组,因为它有一个额外的字符串,可以分配给管理中的客户组设置中的每个客户组,以便以后使用。

  1. 首先创建了一个迁移,其中包含一个新的数据库条目到表"customer_group“->,当我”选择* FROM customer_group“时,我可以在那里看到我的额外条目,值为NULL。
  2. ,我创建了一个EntityExtension来加入SQL-条目和Shopware-条目,我还创建了一个订阅服务器,在加载产品组时,应该确保Shopware条目被加载。
  3. ,最后我创建了一个html.twig,它应该覆盖引用的html.twig,以确保在管理->中的用户组设置中有一个"sw-text-field“,并且工作正常,小枝就会被覆盖。

现在我的问题是:当我将一个测试字符串放入我的自定义sw-text字段并单击“保存”按钮时,我不会收到任何错误消息,但是我的测试字符串也不会被写入SQL-数据库。我试图将SQL表中的自定义值设置为某种测试字符串--这很有效--但我也无法输出shopware-HTML中的字符串,以便在管理中查看它,因此我的猜测是,我做了一些错误的操作--将数据库条目与购物器条目链接在一起。我也试图严格遵循Shopware 6文档,但无法找到解决方案。以前有人做过这样的事并能帮助我吗?预先感谢您的任何回答-下面是我的代码:

代码语言:javascript
复制
// Shopware Version 6.3.9999999.9999999-dev

//directory: CustomerGroupPlugin\src\Resources\config\services.xml

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="CustomerGroupPlugin\ModelExtension\CustomerGroupExtension">
            <tag name="shopware.entity.extension"/>
        </service>

        <service id="CustomerGroupPlugin\ModelExtension\Subscriber\MySubscriber">
            <tag name="kernel.event_subscriber" />
        </service>
        
        <service id="CustomerGroupPlugin\ExtendPage\Storefront\Subscriber\CustomerRegisterSubscriber">
            <tag name="kernel.event_subscriber"/>
            <argument type="service" id="customer.repository"/>
            <argument type="service" id="customer_group.repository"/>
        </service>
    </services>
</container>


//directory: CustomerGroupPlugin\src\ModelExtension\CustomerGroupExtension.php

<?php declare(strict_types=1);

namespace CustomerGroupPlugin\ModelExtension;

use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityExtension;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Runtime;
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;

class CustomerGroupExtension extends EntityExtension
{
    public function extendFields(FieldCollection $collection): void
    {
        $collection->add(
            (new StringField('test_string', 'testString'))->addFlags(new Runtime())
        );
    }

    public function getDefinitionClass(): string
    {
        return CustomerGroupDefinition::class;
    }
}


//directory: CustomerGroupPlugin\src\ModelExtension\Subscriber\MySubscriber.php

<?php declare(strict_types=1);

namespace CustomerGroupPlugin\ModelExtension\Subscriber;

use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use Shopware\Core\Content\Cms\SalesChannel\Struct\TextStruct;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
use Shopware\Core\Checkout\Customer\CustomerEvents;

class MySubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            CustomerEvents::CUSTOMER_GROUP_LOADED_EVENT => 'onCustomerGroupsLoaded'
        ];
    }

    public function onCustomerGroupsLoaded(EntityLoadedEvent $event): void
    {
        /** @var CustomerGroupEntity $customerGroupEntity */
        foreach ($event->getEntities() as $customerGroupEntity) {
            $customerGroupEntity->addExtension('testString', new TextStruct());
        }
    }
}


//directory: CustomerGroupPlugin\src\Resources\app\administration\src\module\sw-settings-customer-group\page\sw-settings-customer-group-detail\sw-settings-customer-group-detail.html.twig

{% block sw_settings_customer_group_detail_content_card_name %}
    {% parent %}
    <sw-text-field v-model="customerGroup.testString"
        label="Teststring Here">
    </sw-text-field>
{% endblock %}


//directory: CustomerGroupPlugin\src\Resources\app\administration\src\main.js

import template from './module/sw-settings-customer-group/page/sw-settings-customer-group-detail/sw-settings-customer-group-detail.html.twig';

Shopware.Component.override('sw-settings-customer-group-detail', {
    template
});


//directory: CustomerGroupPlugin\src\Migration\Migration1610627867.php

<?php declare(strict_types=1);

namespace CustomerGroupPlugin\Migration;

use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Migration\MigrationStep;

class Migration1610627867 extends MigrationStep
{
    public function getCreationTimestamp(): int
    {
        return 1610627867;
    }

    public function update(Connection $connection): void
    {
        $connection->executeUpdate('
            ALTER TABLE `customer_group`
            ADD `test_string` VARCHAR(255);
        ');
    }

    public function updateDestructive(Connection $connection): void
    {
        // implement update destructive
    }
}

//directory: CustomerGroupPlugin\src\CustomerGroupPlugin.php

<?php declare(strict_types=1);

namespace CustomerGroupPlugin;

use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;

class CustomerGroupPlugin extends Plugin
{
    public function uninstall(UninstallContext $context): void
    {
        parent::uninstall($context);

        if ($context->keepUserData()) {
            return;
        }

        $connection = $this->container->get(Connection::class);

        $connection->executeUpdate('ALTER TABLE `customer_group` DROP COLUMN `test_string`');
    }
}
EN

回答 2

Stack Overflow用户

发布于 2021-02-24 11:10:25

您确定事件是触发的吗?您可以查看Symfony控制台并单击例如最后10个。例如,将有302重定向。在里面查找并搜索echo或dump ()。

对于小枝变量的问题,您可能需要将var添加到模板中:

代码语言:javascript
复制
$this->renderView(
  'your-twig-path.html.twig', [
             'group' => your var,
              ...,
              ...
         ]
   );

如果我记得清楚,"renderView()取决于它是JsonResponse还是StorefrontController。

希望我能帮上忙。

票数 0
EN

Stack Overflow用户

发布于 2022-02-22 12:08:52

在我看来,CustomerEvents是在后端加载的。您正在尝试的是在StoreFront中注册注册事件。获取所需事件的最佳方法:

例如,提交注册表单(或打开所需的控制器)。如果您想要注册事件,请进入您的symfony调试器(单击商店/管理程序左下角的statusNumber 200 )。然后点击左上角的“最后10”按钮。输入状态"302“下的搜索栏(重定向,注册事件所在)。按下搜索。单击所需令牌链接上的列表中的右键。在侧栏中选择事件选项卡。在那里,你会有所有的解雇事件,而不是解雇事件。可能您的选项卡在“未触发”选项卡中(按ctrl+f并搜索"onCustomerGroupsLoaded“以知道这两个选项卡在哪个选项卡中)。您可以附加到列表中的一个激发事件。请记住,转储输出将在重定向。不是为帐户,也不是在登记表上。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65720462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档