如何扩展shopware.api.customergroup

时间:2019-08-10 11:02:06

标签: shopware

我正试图通过向其添加属性来扩展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>

我期望看到“属性”属性,但未显示

1 个答案:

答案 0 :(得分:0)

正如您在原始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的问候

Michael Telgmann