/
Variant.php
179 lines (170 loc) · 6.06 KB
/
Variant.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\commerce\gql\interfaces\elements;
use craft\commerce\elements\Variant as VariantElement;
use craft\commerce\gql\types\generators\VariantType;
use craft\commerce\gql\types\SaleType;
use craft\gql\GqlEntityRegistry;
use craft\gql\interfaces\Element;
use craft\gql\TypeManager;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\Type;
/**
* Class Variant
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.1
*/
class Variant extends Element
{
/**
* @inheritdoc
*/
public static function getTypeGenerator(): string
{
return VariantType::class;
}
/**
* @inheritdoc
*/
public static function getType($fields = null): Type
{
if ($type = GqlEntityRegistry::getEntity(self::getName())) {
return $type;
}
$type = GqlEntityRegistry::createEntity(self::getName(), new InterfaceType([
'name' => static::getName(),
'fields' => self::class . '::getFieldDefinitions',
'description' => 'This is the interface implemented by all variants.',
'resolveType' => function(VariantElement $value) {
return $value->getGqlTypeName();
},
]));
VariantType::generateTypes();
return $type;
}
/**
* @inheritdoc
*/
public static function getName(): string
{
return 'VariantInterface';
}
/**
* @inheritdoc
*/
public static function getFieldDefinitions(): array
{
return TypeManager::prepareFieldDefinitions(array_merge(parent::getFieldDefinitions(), [
'isDefault' => [
'name' => 'isDefault',
'type' => Type::boolean(),
'description' => 'If the variant is the default for the product.',
],
'isAvailable' => [
'name' => 'isAvailable',
'type' => Type::boolean(),
'description' => 'If the variant is available to be purchased.',
],
'price' => [
'name' => 'price',
'type' => Type::float(),
'description' => 'The price of the variant.',
],
'priceAsCurrency' => [
'name' => 'priceAsCurrency',
'type' => Type::string(),
'description' => 'The formatted price of the variant.',
],
'salePrice' => [
'name' => 'salePrice',
'type' => Type::float(),
'description' => 'The sale price of the variant. CAUTION: This will not take into account sales that utilize user group conditions.',
],
'salePriceAsCurrency' => [
'name' => 'salePriceAsCurrency',
'type' => Type::string(),
'description' => 'The formatted sale price of the variant. CAUTION: This will not take into account sales that utilize user group conditions.',
],
'sales' => [
'name' => 'sales',
'type' => Type::listOf(SaleType::getType()),
'description' => 'The sales that apply to the variant. CAUTION: This will not take into account sales that utilize user group conditions.',
],
'sortOrder' => [
'name' => 'sortOrder',
'type' => Type::int(),
'description' => 'The sort order of the variant.',
],
'width' => [
'name' => 'width',
'type' => Type::float(),
'description' => 'The width of the variant.',
],
'height' => [
'name' => 'height',
'type' => Type::float(),
'description' => 'The height of the variant.',
],
'length' => [
'name' => 'length',
'type' => Type::float(),
'description' => 'The length of the variant.',
],
'weight' => [
'name' => 'weight',
'type' => Type::float(),
'description' => 'The weight of the variant.',
],
'stock' => [
'name' => 'stock',
'type' => Type::int(),
'description' => 'The stock level of the variant.',
],
'hasUnlimitedStock' => [
'name' => 'hasUnlimitedStock',
'type' => Type::boolean(),
'description' => 'If the variant has unlimited stock.',
],
'minQty' => [
'name' => 'minQty',
'type' => Type::int(),
'description' => 'The minimum allowed quantity to be purchased.',
],
'maxQty' => [
'name' => 'maxQty',
'type' => Type::int(),
'description' => 'The maximum allowed quantity to be purchased.',
],
'productId' => [
'name' => 'productId',
'type' => Type::int(),
'description' => 'The ID of the variant’s parent product.',
],
'product' => [
'name' => 'product',
'type' => Product::getType(),
'description' => 'The variant’s parent product.',
],
'productTitle' => [
'name' => 'productTitle',
'type' => Type::string(),
'description' => 'The title of the variant’s parent product.',
],
'productTypeId' => [
'name' => 'productTypeId',
'type' => Type::int(),
'description' => 'The product type ID of the variant’s parent product.',
],
'sku' => [
'name' => 'sku',
'type' => Type::string(),
'description' => 'The SKU of the variant.',
],
]), self::getName());
}
}