Skip to content

Commit e637aee

Browse files
committed
Added support downloadable product
1 parent 0b8a137 commit e637aee

File tree

3 files changed

+77
-26
lines changed

3 files changed

+77
-26
lines changed

Vuefront/Vuefront/ApiGraphql/model/store/product.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ModelStoreProduct extends Model
1818
private $_productFactory;
1919
private $_categoryFactory;
2020

21+
2122
public function __construct($registry)
2223
{
2324
parent::__construct($registry);
@@ -27,6 +28,7 @@ public function __construct($registry)
2728
$this->_categoryFactory = $objectManager->get('Magento\Catalog\Model\CategoryFactory');
2829
$this->_productFactory = $objectManager->get('Magento\Catalog\Model\ProductFactory');
2930
$this->_productRepository = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface');
31+
3032
}
3133

3234
public function getProduct($product_id)

Vuefront/Vuefront/ApiGraphql/resolver/store/cart.php

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
use \Magento\Framework\App\ObjectManager;
44

5-
require_once VF_SYSTEM_DIR.'engine/resolver.php';
5+
require_once VF_SYSTEM_DIR . 'engine/resolver.php';
66

77
class ResolverStoreCart extends Resolver
88
{
99
public function add($args)
1010
{
11-
$objectManager =ObjectManager::getInstance();
11+
$objectManager = ObjectManager::getInstance();
1212

13+
/** @var Magento\Catalog\Model\ProductRepository $productRepository */
1314
$productRepository = $objectManager->get('Magento\Catalog\Model\ProductRepository');
15+
/** @var Magento\Checkout\Model\Cart $cart */
1416
$cart = $objectManager->get('Magento\Checkout\Model\Cart');
1517
$options = array();
18+
$links = array();
1619
$super_attributes = array();
1720

1821
foreach ($args['options'] as $value) {
@@ -25,58 +28,68 @@ public function add($args)
2528
});
2629
$options[str_replace('option_', '', $value['id'])] = $values;
2730
}
31+
} elseif ($value['id'] == 'links') {
32+
$values = array_filter(explode('|', $value['value']), function ($value) {
33+
return $value !== '';
34+
});
35+
$links = $values;
2836
} else {
2937
$super_attributes[$value['id']] = $value['value'];
3038
}
3139
}
3240

33-
3441
$params = array(
35-
'product' => $args['id'],
36-
'qty' => $args['quantity'],
37-
'options' => $options,
38-
'super_attribute' => $super_attributes
39-
);
42+
'product' => $args['id'],
43+
'qty' => $args['quantity'],
44+
'options' => $options,
45+
'super_attribute' => $super_attributes,
46+
'links' => $links
47+
);
4048
$product_info = $productRepository->getById($args['id']);
4149

4250
try {
4351
$cart->addProduct($product_info, $params);
4452
} catch (Exception $e) {
45-
echo '<pre>';
46-
print_r($e->getMessage());
47-
echo '</pre>';
53+
throw new Exception($e->getMessage());
4854
}
4955

5056
$cart->save();
5157

5258
return $this->get($args);
5359
}
60+
5461
public function update($args)
5562
{
5663
$objectManager = ObjectManager::getInstance();
64+
/** @var Magento\Checkout\Model\Cart $cart */
5765
$cart = $objectManager->get('Magento\Checkout\Model\Cart');
58-
66+
5967
$item = $cart->getQuote()->getItemById($args['key']);
6068
$item->setQty($args['quantity']);
6169
$cart->save();
6270

6371
return $this->get($args);
6472
}
73+
6574
public function remove($args)
6675
{
67-
$objectManager =ObjectManager::getInstance();
68-
76+
$objectManager = ObjectManager::getInstance();
77+
/** @var Magento\Checkout\Model\Cart $modelCart */
6978
$modelCart = $objectManager->get('Magento\Checkout\Model\Cart');
7079

7180
$modelCart->removeItem($args['key'])->save();
7281

7382
return $this->get($args);
7483
}
84+
7585
public function get($args)
7686
{
77-
$objectManager =ObjectManager::getInstance();
87+
$objectManager = ObjectManager::getInstance();
7888

89+
/** @var Magento\Checkout\Model\Cart $modelCart */
7990
$modelCart = $objectManager->get('Magento\Checkout\Model\Cart');
91+
/** @var Magento\Downloadable\Api\LinkRepositoryInterface $linkRepository */
92+
$linkRepository = $objectManager->get('Magento\Downloadable\Api\LinkRepositoryInterface');
8093

8194
$modelCart->getQuote()->collectTotals();
8295
$cart = array(
@@ -87,18 +100,35 @@ public function get($args)
87100
$results = $modelCart->getItems();
88101

89102
foreach ($results as $value) {
103+
/** @var Magento\Catalog\Model\Product $product */
104+
$product = $value->getProduct();
90105
if (!$value->isDeleted() && !$value->getParentItemId() && !$value->getParentItem()) {
91106
$options = array();
92-
93-
$result_options = $value->getProduct()->getTypeInstance(true)->getOrderOptions($value->getProduct());
94107

108+
$result_options = $product->getTypeInstance(true)->getOrderOptions($product);
109+
if (!empty($result_options['links'])) {
110+
$values = array();
111+
$productLinks = $linkRepository->getLinksByProduct($product);
112+
113+
foreach ($productLinks as $link) {
114+
if (in_array($link->getId(), $result_options['links'])) {
115+
$values[] = $link->getTitle();
116+
}
117+
}
118+
119+
$options[] = array(
120+
'name' => $value->getProduct()->getData('links_title'),
121+
'type' => 'checkbox',
122+
'value' => implode(', ', $values)
123+
);
124+
}
95125
if (!empty($result_options['attributes_info'])) {
96126
foreach ($result_options['attributes_info'] as $option) {
97127
$options[] = array(
98-
'name' => $option['label'],
99-
'type' => 'radio',
100-
'value' => $option['value']
101-
);
128+
'name' => $option['label'],
129+
'type' => 'radio',
130+
'value' => $option['value']
131+
);
102132
}
103133
}
104134
if (!empty($result_options['options'])) {
@@ -135,11 +165,11 @@ public function get($args)
135165
}
136166

137167
$cart['products'][] = array(
138-
'key' => $value->getId(),
139-
'product' => $this->load->resolver('store/product/get', array( 'product' => $value->getProduct() )),
168+
'key' => $value->getId(),
169+
'product' => $this->load->resolver('store/product/get', array('product' => $value->getProduct())),
140170
'quantity' => $value->getQty(),
141-
'option' => $options,
142-
'total' => $this->currency->format($value->getPrice() * $value->getQty())
171+
'option' => $options,
172+
'total' => $this->currency->format($value->getPrice() * $value->getQty())
143173
);
144174
}
145175
}

Vuefront/Vuefront/ApiGraphql/resolver/store/product.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function getOptions($data)
235235
$product = $data['product'];
236236
$options = array();
237237

238-
if (!$product->getData('has_options')) {
238+
if (!$product->getData('has_options') && $product->getTypeId() !== 'downloadable') {
239239
return $options;
240240
}
241241
/** @var \Magento\Catalog\Model\ResourceModel\Product\Option\Collection $collection */
@@ -281,6 +281,25 @@ public function getOptions($data)
281281
);
282282
}
283283

284+
if ($product->getTypeId() == 'downloadable') {
285+
$links = $product->getTypeInstance(true)->getLinks($product);
286+
if (count($links) > 0) {
287+
$options['links'] = array(
288+
'id' => 'links',
289+
'name' => $product->getData('links_title'),
290+
'type' => 'checkbox',
291+
'values' => array()
292+
);
293+
}
294+
foreach ($links as $link) {
295+
$options['links']['values'][] = array(
296+
'id' => $link->getId(),
297+
'name' => $link->getTitle()
298+
);
299+
}
300+
}
301+
302+
284303
if ($product->getTypeId() == 'configurable') {
285304
$results = $product->getTypeInstance()->getConfigurableOptions($product);
286305
foreach ($results as $attribute_id => $values) {

0 commit comments

Comments
 (0)