Как расширить shopware.api.customergroup

Я пытаюсь расширить Shopware v5.4.6 \ Shopware \ Components \ Api \ Resource \ CustomerGroup, добавив к нему атрибуты, но он не отображается в ответе API.

Я попытался изменить назначение расширения примера ресурса Customer 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>

Я ожидал увидеть свойство "attribute", но оно не отображается


person Fady Elias    schedule 10.08.2019    source источник
comment
Конечная точка API групп клиентов не включает атрибуты, как вы можете видеть в документации моделей API или непосредственно на github   -  person Niklas Teich    schedule 15.08.2019


Ответы (1)


Как вы можете видеть в исходном методе 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;
}

С наилучшими пожеланиями из Шёппингена

Майкл Тельгманн

person Michael T    schedule 25.09.2019