shopware - 如何扩展 shopware.api.customergroup
问题描述
我正在尝试通过向其添加属性来扩展 Shopware v5.4.6 的 \Shopware\Components\Api\Resource\CustomerGroup 但它没有显示在 API 响应中。
我试图重新调整扩展客户 API 资源示例的用途,但它不起作用。
“SwagExtendCustomerGroupResource\Components\Api\Resource\CustomerGroup.php”
class CustomerGroup extends \Shopware\Components\Api\Resource\CustomerGroup
{
/**
* @inheritdoc
*/
public function getOne($id)
{
$result = parent::getOne($id);
$result ['attribute'] = $result->getAttribute();
return $result;
}
}
“SwagExtendCustomerGroupResource\Resources\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="swag_extend_customer_group_resource.customer_group_resource"
class="SwagExtendCustomerGroupResource\Components\Api\Resource\CustomerGroup"
decorates="shopware.api.customergroup"
public="false"
shared="false">
</service>
</services>
</container>
我期待看到“属性”属性但它没有显示
解决方案
正如您在原始getOne
方法中看到的那样,查询构建器不会选择客户组的属性。
所以如果你想选择属性,你需要完全覆盖这个方法:
public function getOne($id)
{
$this->checkPrivilege('read');
if (empty($id)) {
throw new ApiException\ParameterMissingException('id');
}
$builder = $this->getRepository()->createQueryBuilder('customerGroup')
->select('customerGroup', 'd', 'attr') // <-- add select
->leftJoin('customerGroup.discounts', 'd')
->leftJoin('customerGroup.attribute', 'attr') // <-- join attributes
->where('customerGroup.id = :id')
->setParameter(':id', $id);
$query = $builder->getQuery();
$query->setHydrationMode($this->getResultMode());
/** @var \Shopware\Models\Customer\Group $category */
$result = $query->getOneOrNullResult($this->getResultMode());
if (!$result) {
throw new ApiException\NotFoundException(sprintf('CustomerGroup by id %d not found', $id));
}
return $result;
}
来自 Schöppingen 的问候
迈克尔·泰格曼
推荐阅读
- python - RegEx 在 (Number-Number)、(Number-char)、(Char-Special char) 和 (special char- char) 之间插入空格,除了 '\'
- ruby-on-rails - 在服务器之间迁移会破坏旧的操作文本附件,新附件可以工作
- google-calendar-api - 日历循环规则 - 连续天:每个第一个和第三个星期五和星期六
- android - 使用 ARCore 的示例点云
- vba - 注销 Windows Server 2016 时触发规则时 Outlook vba 打开代码窗口
- reactjs - 如何在 React Data Grid 中选择所需的输入字段进行过滤
- python - Python - 如何从派生类更新基类变量
- android-studio - 片段无法转换为 java.util.concurrent.Executor 错误
- javascript - 反应 state.map 返回空数组
- c++ - 编译器可以用双打简化表达式吗?