Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit 035baf6

Browse files
author
Roman Glushko
committed
#141 Enables date option type for graphql entry point
1 parent 1012b3c commit 035baf6

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogGraphQl\Model\Product\Option;
8+
9+
use Magento\Catalog\Model\Product\Option\Type\Date as ProductDateOptionType;
10+
use Magento\Framework\Stdlib\DateTime;
11+
12+
/**
13+
* Catalog product option date validator
14+
*/
15+
class DateType extends ProductDateOptionType
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function validateUserValue($values)
21+
{
22+
if ($this->_dateExists() || $this->_timeExists()) {
23+
return parent::validateUserValue($this->formatValues($values));
24+
}
25+
26+
return $this;
27+
}
28+
29+
/**
30+
* @param array $values
31+
* @return array mixed
32+
*/
33+
protected function formatValues($values)
34+
{
35+
if (isset($values[$this->getOption()->getId()])) {
36+
$value = $values[$this->getOption()->getId()];
37+
$dateTime = \DateTime::createFromFormat(DateTime::DATETIME_PHP_FORMAT, $value);
38+
$values[$this->getOption()->getId()] = [
39+
'date' => $value,
40+
'year' => $dateTime->format('Y'),
41+
'month' => $dateTime->format('m'),
42+
'day' => $dateTime->format('d'),
43+
'hour' => $dateTime->format('H'),
44+
'minute' => $dateTime->format('i'),
45+
'day_part' => $dateTime->format('a'),
46+
];
47+
}
48+
49+
return $values;
50+
}
51+
52+
/**
53+
* @return bool
54+
*/
55+
public function useCalendar()
56+
{
57+
return false;
58+
}
59+
}

app/code/Magento/CatalogGraphQl/etc/graphql/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\Catalog\Model\Product\Option\Type\Date" type="Magento\CatalogGraphQl\Model\Product\Option\DateType" />
910
<type name="Magento\CatalogGraphQl\Model\ProductInterfaceTypeResolverComposite">
1011
<arguments>
1112
<argument name="productTypeNameResolvers" xsi:type="array">

app/code/Magento/QuoteGraphQl/Model/Resolver/CartItem/CustomizableOptions.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,44 @@ private function getOptionData($cartItem, int $optionId): array
135135
'units' => '$',
136136
'value' => $optionValue->getPrice(),
137137
];
138+
139+
$selectedOptionValueData = [$selectedOptionValueData];
138140
}
139141

140-
if ('field' == $option->getType()) {
142+
if ('field' == $option->getType() || 'date' == $option->getType()) {
141143
$selectedOptionValueData['price'] = [
142144
'type' => strtoupper($option->getPriceType()),
143145
'units' => '$',
144146
'value' => $option->getPrice(),
145147
];
148+
149+
$selectedOptionValueData = [$selectedOptionValueData];
150+
}
151+
152+
if ('multiple' == $option->getType()) {
153+
$selectedOptionValueData = [];
154+
$optionIds = explode(',', $itemOption->getValue());
155+
156+
foreach ($optionIds as $optionId) {
157+
$optionValue = $option->getValueById($optionId);
158+
159+
$selectedOptionValueData[] = [
160+
'id' => $itemOption->getId(),
161+
'label' => $optionValue->getTitle(),
162+
'price' => [
163+
'type' => strtoupper($optionValue->getPriceType()),
164+
'units' => '$',
165+
'value' => $optionValue->getPrice(),
166+
],
167+
];
168+
}
146169
}
147170

148171
return [
149172
'id' => $option->getId(),
150173
'label' => $option->getTitle(),
151174
'type' => $option->getType(),
152-
'values' => [$selectedOptionValueData],
175+
'values' => $selectedOptionValueData,
153176
'sort_order' => $option->getSortOrder(),
154177
];
155178
}

app/code/Magento/SimpleProductGraphQl/Model/Resolver/Cart/AddSimpleProductsToCart.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
120120
);
121121
}
122122

123-
// todo: add security checking
124-
125123
/** @var CartInterface|Quote $cart */
126124
$cart = $this->guestCartRepository->get($cartId);
127125

0 commit comments

Comments
 (0)