Skip to content

Commit 373a987

Browse files
Merge pull request #63 from riha112/2269
#2269 Support for downloadable product links
2 parents 0ce259b + 46210be commit 373a987

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

src/Model/Resolver/GetCartForCustomer.php

+42-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
use ScandiPWA\Performance\Model\Resolver\Products\DataPostProcessor;
3636
use \Magento\Quote\Api\Data\AddressInterface;
3737
use Magento\Tax\Model\Config;
38+
use Magento\Downloadable\Model\LinkRepository;
39+
use Magento\Downloadable\Model\Link;
3840

3941
class GetCartForCustomer extends CartResolver
4042
{
@@ -62,6 +64,9 @@ class GetCartForCustomer extends CartResolver
6264
/** @var Config */
6365
private $config;
6466

67+
/** @var LinkRepository */
68+
protected $linkRepository;
69+
6570
/**
6671
* GetCartForCustomer constructor.
6772
* @param ParamOverriderCustomerId $overriderCustomerId
@@ -73,6 +78,7 @@ class GetCartForCustomer extends CartResolver
7378
* @param CustomizableOption $customizableOption
7479
* @param BundleOptionDataProvider $bundleOptions
7580
* @param Config $config
81+
* @param LinkRepository $linkRepository
7682
*/
7783
public function __construct(
7884
ParamOverriderCustomerId $overriderCustomerId,
@@ -84,7 +90,8 @@ public function __construct(
8490
CustomizableOption $customizableOption,
8591
BundleOptionDataProvider $bundleOptions,
8692
Json $serializer,
87-
Config $config
93+
Config $config,
94+
LinkRepository $linkRepository
8895
) {
8996
parent::__construct(
9097
$guestCartRepository,
@@ -99,6 +106,7 @@ public function __construct(
99106
$this->bundleOptions = $bundleOptions;
100107
$this->serializer = $serializer;
101108
$this->config = $config;
109+
$this->linkRepository = $linkRepository;
102110
}
103111

104112
/**
@@ -114,10 +122,42 @@ protected function mergeQuoteItemData(
114122
return [
115123
'product' => $this->productsData[$product->getId()],
116124
'customizable_options' => $this->getCustomizableOptions($item),
117-
'bundle_options' => $this->bundleOptions->getData($item)
125+
'bundle_options' => $this->bundleOptions->getData($item),
126+
'downloadable_links' => $this->getDownloadableLinks($item, $product)
118127
] + $item->getData();
119128
}
120129

130+
/**
131+
* @param $item
132+
* @param $product
133+
* @return array
134+
*/
135+
private function getDownloadableLinks($item, $product): array
136+
{
137+
$quoteItemLinks= $item->getOptionByCode('downloadable_link_ids');
138+
139+
if (null === $quoteItemLinks) {
140+
return [];
141+
}
142+
143+
$downloadableLinks = [];
144+
$downloadableLinkIds = explode(',', $quoteItemLinks->getValue());
145+
$productLinks = $this->linkRepository->getLinksByProduct($product);
146+
147+
/** @var Link $productLink */
148+
foreach ($productLinks as $productLink) {
149+
if (in_array($productLink->getId(), $downloadableLinkIds)){
150+
$downloadableLinks[] = [
151+
'label' => $productLink->getTitle(),
152+
'id' => $productLink->getId()
153+
];
154+
}
155+
}
156+
157+
return $downloadableLinks;
158+
}
159+
160+
121161
/**
122162
* @param $item
123163
* @return array

src/Model/Resolver/SaveCartItem.php

+19
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
4343
use Magento\InventoryReservationsApi\Model\GetReservationsQuantityInterface;
4444
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
45+
use Magento\Downloadable\Model\Product\Type as DownloadableType;
4546

4647
/**
4748
* Class SaveCartItem
@@ -183,6 +184,9 @@ private function prepareAddItem(Product $product, array $options): DataObject
183184
case Type::TYPE_CODE:
184185
$data = $this->setBundleRequestOptions($options, $data);
185186
break;
187+
case DownloadableType::TYPE_DOWNLOADABLE:
188+
$data = $this->setDownloadableRequestLinks($options, $data);
189+
break;
186190
}
187191

188192
$request = new DataObject();
@@ -276,6 +280,21 @@ private function setBundleRequestOptions(array $options, array $data): array
276280
return $data;
277281
}
278282

283+
/**
284+
* @param array $options
285+
* @param array $data
286+
* @return array
287+
*/
288+
private function setDownloadableRequestLinks(array $options, array $data): array
289+
{
290+
$data['links'] = [];
291+
$linkOptions = $options['product_option']['extension_attributes']['downloadable_product_links'] ?? [];
292+
foreach ($linkOptions as $link) {
293+
$data['links'][] = $link['link_id'];
294+
}
295+
return $data;
296+
}
297+
279298
/**
280299
* @param string $guestCardId
281300
* @return string

src/etc/schema.graphqls

+11
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ input ExtensionsAttributeInput {
6868
configurable_item_options: [ConfigurableItemOptionsInput]
6969
customizable_options: [CustomizableOptionsInput]
7070
customizable_options_multi: [CustomizableOptionsInput]
71+
downloadable_product_links: [DownloadableProductLinksInput]
7172
bundle_options: [BundleOptionInput!]
7273
}
7374

@@ -81,6 +82,10 @@ input CustomizableOptionsInput {
8182
option_value: String
8283
}
8384

85+
input DownloadableProductLinksInput {
86+
link_id: Int
87+
}
88+
8489
input PaymentInformation {
8590
billing_address: AddressInput!
8691
paymentMethod: PaymentMethodInput!
@@ -218,9 +223,15 @@ type TotalsItem {
218223
sku: String
219224
customizable_options: [SelectedCustomizableOption]
220225
bundle_options: [SelectedBundleOption]
226+
downloadable_links: [SelectedDownloadableLinks]
221227
product: ProductInterface
222228
}
223229

230+
type SelectedDownloadableLinks {
231+
label: String
232+
id: Int
233+
}
234+
224235
type TotalsSegment {
225236
code: String
226237
title: String

0 commit comments

Comments
 (0)