1

I am facing issue with accessing the title of downloadable products in magento2 graphql.

customerDownloadableProducts {
                items {
                  date
                  download_url
                  order_increment_id
                  remaining_downloads
                  status
                }
              }

I also want to access title of downloadable products to display. But cant find a way to do so. Please modify the above query to display title.

1

2 Answers 2

0

You can access the title of the downloadable product in graphql as below query.

  products(filter: { sku: { eq: "240-LV04" } }) {
    items {
      sku
      name
      ... on DownloadableProduct {
        links_title
        downloadable_product_links {
          title
          sample_url
        }
      }
    }
  }
}```
0

Try Below Code

app/code/VendorName/ModuleName/etc

schema.graphqls

type CustomerDownloadableProduct @doc(description: "Contains details about a single downloadable product.") {
    product_name: String @doc(description: "The Product Name")
    link_title: String @doc(description: "The Product Link Name")
}

app/code/VendorName/ModuleName/etc/graphql

di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\CustomerDownloadableGraphQl\Model\Resolver\CustomerDownloadableProducts"
                type="VendorName\ModuleName\Model\Resolver\CustomerDownloadableProducts" />
</config>

app/code/VendorName/ModuleName/Model/Resolver

CustomerDownloadableProducts.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace VendorName\ModuleName\Model\Resolver;

use Magento\DownloadableGraphQl\Model\ResourceModel\GetPurchasedDownloadableProducts;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\UrlInterface;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\CustomerDownloadableGraphQl\Model\Resolver\CustomerDownloadableProducts as ParentClass;

/**
 * @inheritdoc
 *
 * Returns available downloadable products for customer
 */
class CustomerDownloadableProducts extends ParentClass
{
    /**
     * @var GetPurchasedDownloadableProducts
     */
    private $getPurchasedDownloadableProducts;

    /**
     * @var UrlInterface
     */
    private $urlBuilder;

    /**
     * @param GetPurchasedDownloadableProducts $getPurchasedDownloadableProducts
     * @param UrlInterface $urlBuilder
     */
    public function __construct(
        GetPurchasedDownloadableProducts $getPurchasedDownloadableProducts,
        UrlInterface $urlBuilder
    ) {
        $this->getPurchasedDownloadableProducts = $getPurchasedDownloadableProducts;
        $this->urlBuilder = $urlBuilder;
        parent::__construct(
            $this->getPurchasedDownloadableProducts,
            $this->urlBuilder = $urlBuilder
        );
    }

    /**
     * @inheritdoc
     */
    public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        /** @var ContextInterface $context */
        if (false === $context->getExtensionAttributes()->getIsCustomer()) {
            throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
        }

        $purchasedProducts = $this->getPurchasedDownloadableProducts->execute($context->getUserId());
        $productsData = [];

        /* The fields names are hardcoded since there's no existing name reference in the code */
        foreach ($purchasedProducts as $purchasedProduct) {
            if ($purchasedProduct['number_of_downloads_bought']) {
                $remainingDownloads = $purchasedProduct['number_of_downloads_bought'] -
                    $purchasedProduct['number_of_downloads_used'];
            } else {
                $remainingDownloads = __('Unlimited');
            }

            $productsData[] = [
                'product_name' => $purchasedProduct['product_name'],
                'link_title' => $purchasedProduct['link_title'],
                'order_increment_id' => $purchasedProduct['order_increment_id'],
                'date' => $purchasedProduct['created_at'],
                'status' => $purchasedProduct['status'],
                'download_url' => $this->urlBuilder->getUrl(
                    'downloadable/download/link',
                    ['id' => $purchasedProduct['link_hash'], '_secure' => true]
                ),
                'remaining_downloads' => $remainingDownloads
            ];
        }

        return ['items' => $productsData];
    }
}

Query:

{
  customerDownloadableProducts {
    items {
      product_name
      link_title
      date
      download_url
      order_increment_id
      remaining_downloads
      status
    }
  }
}

OUTPUT:

enter image description here

Note: Before running the updated query above, please execute the Magento upgrade command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.