API Route zu erstellen sehr schwer, trotz Docu

Mit shopware.entity.definition habe ich ausprobiert, wie es funktioniert (ich habe es irgendwo in der Dokumentation gefunden).

Ich habe es entfernt und habe jetzt eine Schleife:
ERR_TOO_MANY_REDIRECTS

Was ist denn dein ProductCountRoute für ein Service? Wenn es keine Custom Entity ist, dann hat der Tag da nichts verloren.

Mit dem wenigen was man an Information von dir hat, lässt sich auch nur wenig dazu sagen.

1 „Gefällt mir“

Hier ist es:

<?php declare(strict_types=1);

namespace CustomFilter\Core\Content\Example\SalesChannel;

use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingResult;
use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
use Shopware\Core\Content\Product\SalesChannel\Listing\Filter;


abstract class AbstractProductCountRoute
{
    abstract public function getDecorated(): AbstractProductCountRoute;

    abstract public function load(Criteria $criteria, SalesChannelContext $context): ProductCountRouteResponse;
}

und:

<?php declare(strict_types=1);

namespace CustomFilter\Core\Content\Example\SalesChannel;

use OpenApi\Annotations as OA;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
use Shopware\Core\Framework\Routing\Annotation\Entity;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route(defaults={"_routeScope"={"store-api"}})
 */
class ProductCountRoute extends AbstractProductCountRoute
{
    protected EntityRepository $productRepository;

    public function __construct(EntityRepository $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    public function getDecorated(): AbstractProductCountRoute
    {
        throw new DecorationPatternException(self::class);
    }

    /**
     * @Entity("swag_get_active_product_count")
     * @OA\Post(
     *      path="/get-active-product-count",
     *      summary="This route can be used to get the count of all active products",
     *      operationId="readProductCount",
     *      tags={"Store API", "productCount"},
     *      @OA\Parameter(name="Api-Basic-Parameters"),
     *      @OA\Response(
     *          response="200",
     *          description="",
     *          @OA\JsonContent(type="object",
     *              @OA\Property(
     *                  property="productCount",
     *                  type="integer",
     *                  description="Total amount"
     *              )
     *          )
     *     )
     * )
     * @Route("/store-api/get-active-product-count", name="store-api.product-count.get", methods={"GET", "POST"})
     */
    public function load(Criteria $criteria, SalesChannelContext $context): ProductCountRouteResponse
    {
        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('product.active', true));
        $criteria->addAggregation(new CountAggregation('productCount', 'product.id'));

        /** @var CountResult $productCountResult */
        $productCountResult = $this->productRepository
            ->aggregate($criteria, $event->getContext())
            ->get('productCount');
            
        return new ProductCountRouteResponse($productCountResult);
    }
}

Und:

<?php declare(strict_types=1);

namespace CustomFilter\Core\Content\Example\SalesChannel;

use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\System\SalesChannel\StoreApiResponse;
use ADFilter\Core\Content\Example\ExampleCollection;

use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingResult;

use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
use Shopware\Core\Content\Product\SalesChannel\Listing\Filter;

/**
 * Class ProductCountRouteResponse
 * @property EntitySearchResult $object
 */
class ProductCountRouteResponse extends StoreApiResponse
{
    public function getExamples(): ExampleCollection
    {
        /** @var ExampleCollection $collection */
        $collection = $this->object->getEntities();

        return $collection;
    }
}

Und:

<services>
    <service id="CustomFilter\Service\AddDataToPage" >
        <argument type="service" id="CustomFilter\Core\Content\Example\SalesChannel\ProductCountRoute"/>
        <tag name="kernel.event_subscriber"/>
    </service>

    <service id="CustomFilter\Core\Content\Example\SalesChannel\ProductCountRoute">
        <argument type="service" id="product.repository"/>
    </service>
</services>

Dieser Teil meines Plugins basiert auf dieser Dokumentation:

Wo ProductCountRoute extends AbstractProductCountRoute

Es gibt auch ProductCountRouteResponse.php
Aber es funktioniert nicht alles zusammen :frowning:

1 „Gefällt mir“