Skip to content

Commit 383ee73

Browse files
authored
ENGCOM-5603: GraphQl-146: [Checkout] Add downloadable product to Cart #412
2 parents c6427d1 + 191ed5c commit 383ee73

24 files changed

+1024
-213
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\DownloadableGraphQl\Model\Cart\BuyRequest;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
13+
use Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestDataProviderInterface;
14+
15+
/**
16+
* DataProvider for building downloadable product links in buy requests
17+
*/
18+
class DownloadableLinksDataProvider implements BuyRequestDataProviderInterface
19+
{
20+
/**
21+
* @var ProductRepositoryInterface
22+
*/
23+
private $productRepository;
24+
25+
/**
26+
* @param ProductRepositoryInterface $productRepository
27+
*/
28+
public function __construct(
29+
ProductRepositoryInterface $productRepository
30+
) {
31+
$this->productRepository = $productRepository;
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function execute(array $cartItemData): array
38+
{
39+
$linksData = [];
40+
41+
if (isset($cartItemData['data']) && isset($cartItemData['data']['sku'])) {
42+
$sku = $cartItemData['data']['sku'];
43+
44+
try {
45+
$product = $this->productRepository->get($sku);
46+
} catch (NoSuchEntityException $e) {
47+
throw new GraphQlNoSuchEntityException(__('Could not find specified product.'));
48+
}
49+
50+
if ($product->getLinksPurchasedSeparately() && isset($cartItemData['downloadable_product_links'])) {
51+
$downloadableLinks = $cartItemData['downloadable_product_links'];
52+
$linksData = array_unique(array_column($downloadableLinks, 'link_id'));
53+
}
54+
}
55+
56+
return (count($linksData) > 0 ? ['links' => $linksData] : []);
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\DownloadableGraphQl\Model;
9+
10+
use Magento\Downloadable\Api\Data\LinkInterface;
11+
use Magento\Framework\UrlInterface;
12+
13+
/**
14+
* Convert links to array
15+
*/
16+
class ConvertLinksToArray
17+
{
18+
/**
19+
* @var UrlInterface
20+
*/
21+
private $urlBuilder;
22+
23+
/**
24+
* @param UrlInterface $urlBuilder
25+
*/
26+
public function __construct(
27+
UrlInterface $urlBuilder
28+
) {
29+
$this->urlBuilder = $urlBuilder;
30+
}
31+
32+
/**
33+
* Format links from collection as array
34+
*
35+
* @param LinkInterface[] $links
36+
* @return array
37+
*/
38+
public function execute(array $links): array
39+
{
40+
$data = [];
41+
foreach ($links as $key => $link) {
42+
$data[$key] = [
43+
'id' => $link->getId(),
44+
'sort_order' => $link->getSortOrder(),
45+
'title' => $link->getTitle(),
46+
'sample_url' => $this->urlBuilder->getUrl(
47+
'downloadable/download/linkSample',
48+
['link_id' => $link->getId()]
49+
),
50+
'price' => $link->getPrice(),
51+
];
52+
}
53+
return $data;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\DownloadableGraphQl\Model;
9+
10+
use Magento\Downloadable\Api\Data\SampleInterface;
11+
use Magento\Framework\UrlInterface;
12+
13+
/**
14+
* Convert samples to array
15+
*/
16+
class ConvertSamplesToArray
17+
{
18+
/**
19+
* @var UrlInterface
20+
*/
21+
private $urlBuilder;
22+
23+
/**
24+
* @param UrlInterface $urlBuilder
25+
*/
26+
public function __construct(
27+
UrlInterface $urlBuilder
28+
) {
29+
$this->urlBuilder = $urlBuilder;
30+
}
31+
32+
/**
33+
* Format samples from collection as array
34+
*
35+
* @param SampleInterface[] $samples
36+
* @return array
37+
*/
38+
public function execute(array $samples): array
39+
{
40+
$data = [];
41+
foreach ($samples as $key => $sample) {
42+
$data[$key] = [
43+
'id' => $sample->getId(),
44+
'sort_order' => $sample->getSortOrder(),
45+
'title' => $sample->getTitle(),
46+
'sample_url' => $this->urlBuilder->getUrl(
47+
'downloadable/download/sample',
48+
['sample_id' => $sample->getId()]
49+
),
50+
];
51+
}
52+
return $data;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\DownloadableGraphQl\Model;
9+
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Downloadable\Api\Data\LinkInterface;
12+
use Magento\Downloadable\Model\ResourceModel\Link\Collection;
13+
use Magento\Downloadable\Model\ResourceModel\Link\CollectionFactory;
14+
15+
/**
16+
* Returns links of a particular downloadable product
17+
*/
18+
class GetDownloadableProductLinks
19+
{
20+
/**
21+
* @var CollectionFactory
22+
*/
23+
private $linkCollectionFactory;
24+
25+
/**
26+
* @param CollectionFactory $linkCollectionFactory
27+
*/
28+
public function __construct(
29+
CollectionFactory $linkCollectionFactory
30+
) {
31+
$this->linkCollectionFactory = $linkCollectionFactory;
32+
}
33+
34+
/**
35+
* Returns downloadable product links
36+
*
37+
* @param Product $product
38+
* @param array $selectedLinksIds
39+
* @return LinkInterface[]
40+
* @throws \Magento\Framework\Exception\LocalizedException
41+
*/
42+
public function execute(Product $product, array $selectedLinksIds = []): array
43+
{
44+
/** @var Collection */
45+
$links = $this->linkCollectionFactory->create();
46+
$links->addTitleToResult($product->getStoreId())
47+
->addPriceToResult($product->getStore()->getWebsiteId())
48+
->addProductToFilter($product->getId());
49+
50+
if (count($selectedLinksIds) > 0) {
51+
$links->addFieldToFilter('main_table.link_id', ['in' => $selectedLinksIds]);
52+
}
53+
return $links->getItems();
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\DownloadableGraphQl\Model;
9+
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Downloadable\Api\Data\SampleInterface;
12+
use Magento\Downloadable\Model\ResourceModel\Sample\CollectionFactory;
13+
14+
/**
15+
* Returns samples of a particular downloadable product
16+
*/
17+
class GetDownloadableProductSamples
18+
{
19+
/**
20+
* @var CollectionFactory
21+
*/
22+
private $sampleCollectionFactory;
23+
24+
/**
25+
* @param CollectionFactory $sampleCollectionFactory
26+
*/
27+
public function __construct(
28+
CollectionFactory $sampleCollectionFactory
29+
) {
30+
$this->sampleCollectionFactory = $sampleCollectionFactory;
31+
}
32+
33+
/**
34+
* Returns downloadable product samples
35+
*
36+
* @param Product $product
37+
* @return SampleInterface[]
38+
*/
39+
public function execute(Product $product): array
40+
{
41+
$samples = $this->sampleCollectionFactory->create()
42+
->addTitleToResult($product->getStoreId())
43+
->addProductToFilter($product->getId());
44+
return $samples->getItems();
45+
}
46+
}

0 commit comments

Comments
 (0)