Claudiu Stoica
Claudiu Stoica

Reputation: 241

Filter products by category in a Shopware 6 Import/Export Profile

I created a Profile in shopware 6 with the api. The body of the request is:

{
  "id": "my99id2cb2784069938089a8a77ctest",
  "label": "Label test",
  "name": "name test",
  "sourceEntity": "product",
  "fileType": "text/csv",
  "delimiter": ";",
  "enclosure": "\"",
  "createdAt": "2022-11-10T10:15:22Z",
  "mapping": [{"key":"id","mappedKey":"id","position":0,"id":"someid4f7c6c478b8041cfd5fe0ae0c5"},{"key":"cover.media.url","mappedKey":"cover","position":1,"id":"someid0ffa7f48e6b135f6e754d6b93c"}],
  "config": {"createEntities": false, "updateEntities": true}
}

Is there a way to specify that i want only product from certain categories? Not all of them

Upvotes: 1

Views: 354

Answers (1)

dneustadt
dneustadt

Reputation: 13161

No, this currently isn't possible with just the profile and existing endpoints. You'll have to implement a custom api endpoint.

If you are able to do that follow the steps from this answer until you first retrieve a logId. Then instead of starting the process using the existing endpoint, request your new custom endpoint. The implementation could look similar to this:

/**
 * @Route(defaults={"_routeScope"={"api"}})
 */
class CustomExportApiController extends AbstractController
{
    private ImportExportFactory $importExportFactory;

    public function __construct(ImportExportFactory $importExportFactory)
    {
        $this->importExportFactory = $importExportFactory;
    }

    /**
     * @Route("/api/_action/custom/export/{logId}/{categoryId}", name="api.action.custom.export", methods={"POST"})
     */
    public function customProductExport(string $logId, string $categoryId, Context $context): JsonResponse
    {
        $importExport = $this->importExportFactory->create($logId, 50, 50);
        $logEntity = $importExport->getLogEntity();

        if ($logEntity->getState() === Progress::STATE_ABORTED) {
            return new JsonResponse(['success' => false]);
        }

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsAnyFilter('categoryTree', [$categoryId]));
        $offset = 0;

        do {
            $progress = $importExport->export($context, $criteria, $offset);
            $offset = $progress->getOffset();
        } while (!$progress->isFinished());

        return new JsonResponse(['success' => true]);
    }
}

After calling your custom endpoint proceed with the steps as described in the answer linked above.

Upvotes: 1

Related Questions