Skip to content

Commit

Permalink
Added salePrice and sales to variant gql queries fixes #1525
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Jun 19, 2020
1 parent b311d0b commit 764480c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added
- Added the `salePrice` and `sales` fields to GraphQL variant queries. ([#1525](https://github.com/craftcms/commerce/issues/1525))
- Added `craft\commerce\gql\types\SaleType`.

### Changed
- Selected shipping method now shows both name and handle for completed orders on the Order Edit page. ([#1472](https://github.com/craftcms/commerce/issues/1472))

Expand Down
11 changes: 11 additions & 0 deletions src/gql/interfaces/elements/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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;
Expand Down Expand Up @@ -83,6 +84,16 @@ public static function getFieldDefinitions(): array
'type' => Type::float(),
'description' => 'The 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 use 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 use user group conditions.',
],
'sortOrder' => [
'name' => 'sortOrder',
'type' => Type::int(),
Expand Down
67 changes: 67 additions & 0 deletions src/gql/types/SaleType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\commerce\gql\types;

use craft\gql\base\ObjectType;
use craft\gql\GqlEntityRegistry;
use craft\gql\TypeManager;
use GraphQL\Type\Definition\Type;

/**
* Class SaleType
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.x
*/
class SaleType extends ObjectType
{
/**
* @return string|null
*/
public static function getName(): string
{
return 'Sale';
}

/**
* @return Type
*/
public static function getType(): Type
{
if ($type = GqlEntityRegistry::getEntity(self::getName())) {
return $type;
}

$type = GqlEntityRegistry::createEntity(self::getName(), new self([
'name' => static::getName(),
'fields' => self::class . '::getFieldDefinitions',
'description' => '',
]));

return $type;
}

/**
* @return array
*/
public static function getFieldDefinitions(): array
{
return TypeManager::prepareFieldDefinitions([
'name' => [
'name' => 'name',
'type' => Type::string(),
'description' => 'The name of the sale as described in the control panel.',
],
'description' => [
'name' => 'description',
'type' => Type::string(),
'description' => 'Description of the sale.',
],
], self::getName());
}
}

0 comments on commit 764480c

Please sign in to comment.