Skip to content

Commit 5dce39b

Browse files
committed
feat: add totals
1 parent fcc85a4 commit 5dce39b

File tree

2 files changed

+107
-40
lines changed

2 files changed

+107
-40
lines changed

Model/Api/Model/Store/Checkout.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public function requestCheckout($query, $variables) {
5555

5656
$this->_curl->addHeader('Content-type', 'application/json');
5757
$this->_curl->addHeader('Authorization', $jwt);
58+
$this->_curl->setOption(CURLOPT_SSL_VERIFYHOST, false);
59+
$this->_curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
5860
$this->_curl->post('https://api.checkout.vuefront.com/graphql', $this->_jsonSerializer->serialize($requestData));
5961

6062
$result = $this->_curl->getBody();

Model/Api/Resolver/Store/Checkout.php

Lines changed: 105 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,37 @@ class Checkout extends Resolver
2626
*/
2727
protected $checkoutSession;
2828

29+
/**
30+
* @var \Magento\Quote\Api\Data\AddressInterface
31+
*/
32+
protected $address;
33+
34+
/**
35+
* @var \Magento\Checkout\Api\ShippingInformationManagementInterface
36+
*/
37+
protected $shippingInformationManagement;
38+
39+
/**
40+
* @var \Magento\Checkout\Api\Data\ShippingInformationInterface
41+
*/
42+
protected $shippingInformation;
43+
2944
public function __construct(
3045
Config $shippingModelConfig,
3146
UrlInterface $url,
3247
Session $session,
33-
Data $currencyHelper
48+
Data $currencyHelper,
49+
\Magento\Quote\Api\Data\AddressInterface $address,
50+
\Magento\Checkout\Api\Data\ShippingInformationInterface $shippingInformation,
51+
\Magento\Checkout\Api\ShippingInformationManagementInterface $shippingInformationManagement
3452
) {
3553
$this->_currencyHelper = $currencyHelper;
3654
$this->url = $url;
3755
$this->_shippingModelConfig = $shippingModelConfig;
3856
$this->checkoutSession = $session;
57+
$this->address = $address;
58+
$this->shippingInformationManagement = $shippingInformationManagement;
59+
$this->shippingInformation = $shippingInformation;
3960
}
4061

4162
public function link()
@@ -85,8 +106,8 @@ public function shippingMethods($args)
85106
foreach ($carriers as $shippingCode => $shippingModel) {
86107
$shippingPrice = $this->_currencyHelper->currency($shippingModel->getConfigData('price'), true, false);
87108
$result[] = [
88-
'id' => $shippingModel->getCarrierCode(),
89-
'codename' => $shippingModel->getCarrierCode(),
109+
'id' => $shippingModel->getCarrierCode() . '.' . $shippingCode,
110+
'codename' => $shippingModel->getCarrierCode() . '.' . $shippingCode,
90111
"name" => $shippingModel->getConfigData('title') . ' - ' . $shippingPrice
91112
];
92113
}
@@ -109,12 +130,6 @@ public function paymentAddress()
109130
'required' => true
110131
];
111132

112-
$fields[] = [
113-
'type' => 'text',
114-
'name' => 'email',
115-
'required' => true
116-
];
117-
118133
$fields[] = [
119134
'type' => 'text',
120135
'name' => 'company',
@@ -130,11 +145,21 @@ public function paymentAddress()
130145
'name' => 'address2',
131146
'required' => false
132147
];
148+
$fields[] = [
149+
'type' => 'text',
150+
'name' => 'address3',
151+
'required' => false
152+
];
133153
$fields[] = [
134154
'type' => 'text',
135155
'name' => 'city',
136156
'required' => true
137157
];
158+
$fields[] = [
159+
'type' => 'zone',
160+
'name' => 'zone_id',
161+
'required' => true
162+
];
138163
$fields[] = [
139164
'type' => 'text',
140165
'name' => 'postcode',
@@ -147,12 +172,6 @@ public function paymentAddress()
147172
'required' => true
148173
];
149174

150-
$fields[] = [
151-
'type' => 'zone',
152-
'name' => 'zone_id',
153-
'required' => true
154-
];
155-
156175
$agree = null;
157176

158177
return [
@@ -218,11 +237,18 @@ public function shippingAddress()
218237
'required' => true
219238
];
220239

240+
$fields[] = [
241+
'type' => 'phone',
242+
'name' => 'phone',
243+
'required' => true
244+
];
245+
221246
return $fields;
222247
}
223248

224249
public function createOrder($args)
225250
{
251+
$this->checkoutSession->regenerateId();
226252
// $this->session->data['shipping_address'] = array();
227253

228254
// foreach ($this->shippingAddress() as $value) {
@@ -262,33 +288,52 @@ public function updateOrder($args)
262288
// }
263289
// }
264290

265-
// foreach ($args['shippingAddress'] as $value) {
266-
// if (strpos($value['name'], "vfCustomField-") !== false) {
267-
// if ($value['value']) {
268-
// $field_name = str_replace("vfCustomField-", "", $value['name']);
269-
// $field_name = explode('-', $field_name);
270-
// if (!isset($this->session->data['shipping_address']['custom_field'][$field_name[0]])) {
271-
// $this->session->data['shipping_address']['custom_field'][$field_name[0]] = array();
272-
// }
273-
// $this->session->data['shipping_address']['custom_field'][$field_name[0]][$field_name[1]] = $value['value'];
274-
// }
275-
// } else {
276-
// if ($value['value']) {
277-
// $this->session->data['shipping_address'][$value['name']] = $value['value'];
278-
// }
279-
// }
280-
// }
291+
$shippingAddress = [];
281292

282-
// if (!empty($args['shippingMethod'])) {
283-
// $shipping = explode('.', $args['shippingMethod']);
293+
foreach ($this->shippingAddress() as $value) {
294+
$shippingAddress[$value['name']] = '';
295+
}
284296

285-
// $this->load->model('extension/shipping/'.$shipping[0]);
297+
foreach ($args['shippingAddress'] as $value) {
298+
if ($value['value']) {
299+
$shippingAddress[$value['name']] = $value['value'];
300+
}
301+
}
286302

287-
// $quote = $this->{'model_extension_shipping_' . $shipping[0]}->getQuote($this->session->data['shipping_address']);
288-
// if ($quote) {
289-
// $this->session->data['shipping_method'] = $quote['quote'][$shipping[1]];
290-
// }
291-
// }
303+
$shipping_address = $this->address
304+
->setFirstname($shippingAddress['firstName'])
305+
->setLastname($shippingAddress['lastName'])
306+
->setStreet($shippingAddress['address1'])
307+
->setCity($shippingAddress['city'])
308+
->setCountryId($shippingAddress['country_id'])
309+
->setRegionId($shippingAddress['zone_id'])
310+
// ->setRegion($region)
311+
->setPostcode($shippingAddress['postcode'])
312+
->setTelephone($shippingAddress['phone'])
313+
->setSaveInAddressBook(0)
314+
->setSameAsBilling(0);
315+
316+
$shipping_information = $this->shippingInformation->setShippingAddress($shipping_address);
317+
318+
if (!empty($args['shippingMethod'])) {
319+
320+
321+
$methodInfo = explode('.', $args['shippingMethod']);
322+
323+
$shipping_information = $shipping_information->setShippingCarrierCode($methodInfo[0])
324+
->setShippingMethodCode($methodInfo[1]);
325+
326+
if ($this->checkoutSession->getQuote()) {
327+
$cartId = $this->checkoutSession->getQuote()->getId();
328+
$cartSkuArray = $this->getCartItemsSkus();
329+
if ($cartSkuArray) {
330+
try {
331+
$this->shippingInformationManagement->saveAddressInformation($cartId, $shipping_information);
332+
} catch (\Exception $e) {
333+
}
334+
}
335+
}
336+
}
292337

293338
// $this->session->data['payment_method'] = $args['paymentMethod'];
294339
$that = $this;
@@ -314,6 +359,15 @@ public function updateOrder($args)
314359
];
315360
}
316361

362+
public function getCartItemsSkus() {
363+
$cartSkuArray = [];
364+
$cartItems = $this->checkoutSession->getQuote()->getAllVisibleItems();
365+
foreach ($cartItems as $product) {
366+
$cartSkuArray[] = $product->getSku();
367+
}
368+
return $cartSkuArray;
369+
}
370+
317371
public function confirmOrder()
318372
{
319373
return [
@@ -326,6 +380,17 @@ public function confirmOrder()
326380

327381
public function totals()
328382
{
329-
return [];
383+
$totals = $this->checkoutSession->getQuote()->getTotals();
384+
385+
$result = [];
386+
387+
foreach ($totals as $key => $value) {
388+
$result[] = [
389+
"title" => $value->toArray()['title'],
390+
"text" => $this->_currencyHelper->currency($value->toArray()['value'], true, false)
391+
];
392+
}
393+
394+
return $result;
330395
}
331396
}

0 commit comments

Comments
 (0)