Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add orderSummary to entry #295

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Registry/TypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ public static function objects() : array {
// Conditional Logic.
WPObject\ConditionalLogic\ConditionalLogic::class,
WPObject\ConditionalLogic\ConditionalLogicRule::class,
// Orders.
WPObject\Order\OrderItemOption::class,
WPObject\Order\OrderItem::class,
WPObject\Order\OrderSummary::class,
// Entries.
WPObject\Entry\DraftEntry::class,
WPObject\Entry\SubmittedEntry::class,
Expand Down
47 changes: 46 additions & 1 deletion src/Type/WPInterface/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace WPGraphQL\GF\Type\WPInterface;

use GFCommon;
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
Expand All @@ -22,6 +23,7 @@
use WPGraphQL\GF\Model\Form;
use WPGraphQL\GF\Type\Enum\EntryIdTypeEnum;
use WPGraphQL\GF\Type\WPInterface\AbstractInterface;
use WPGraphQL\GF\Type\WPObject\Order\OrderSummary;
use WPGraphQL\GF\Utils\GFUtils;
use WPGraphQL\GF\Utils\Utils;
use WPGraphQL\Registry\TypeRegistry;
Expand Down Expand Up @@ -106,7 +108,7 @@ public static function get_description() : string {
* {@inheritDoc}
*/
public static function get_fields() : array {
return [
$fields = [
'createdBy' => [
'type' => 'User',
'description' => __( 'The user who created the entry.', 'wp-graphql-gravity-forms' ),
Expand Down Expand Up @@ -168,6 +170,49 @@ public static function get_fields() : array {
* https://docs.gravityforms.com/entry-object/#pricing-properties
*/
];

// Order Summaries are only available in GF 2.6+.
if ( version_compare( GFCommon::$version, '2.6.0', '>=' ) ) {
$fields['orderSummary'] = [
'type' => OrderSummary::$type,
'description' => __( 'The entry order summary. Null if the entry has no pricing fields', 'wp-graphql-gravity-forms' ),
'resolve' => function( $source, array $args, AppContext $context ) {
if ( ! class_exists( 'Gravity_Forms\Gravity_Forms\Orders\Factories\GF_Order_Factory' ) ) {
return null;
}

if ( empty( $context->gfForm ) ) {
$context->gfForm = new Form( GFUtils::get_form( $source->formDatabaseId, false ) );
}

$order = \Gravity_Forms\Gravity_Forms\Orders\Factories\GF_Order_Factory::create_from_entry( $context->gfForm->form, $source->entry );

/** @var array $items */
$items = $order->get_items();
if ( empty( $items ) ) {
return null;
}

// Convert order items to array.
$items = array_map(
fn( $item) => $item->to_array(),
$items,
);

$totals = $order->get_totals();
$currency = ! empty( $order->currency ) ? $order->currency : null;

return [
'currency' => $currency,
'items' => $items,
'subtotal' => $totals['sub_total'] ?? null,
'total' => $totals['total'] ?? null,
];
},
];
}

return $fields;
}

/**
Expand Down
115 changes: 115 additions & 0 deletions src/Type/WPObject/Order/OrderItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* GraphQL Object Type - Gravity Forms Entry Order Item
*
* @package WPGraphQL\GF\Type\WPObject\Order
* @since @todo
*/

namespace WPGraphQL\GF\Type\WPObject\Order;

use WPGraphQL\AppContext;
use WPGraphQL\GF\Type\Enum\CurrencyEnum;
use WPGraphQL\GF\Type\WPInterface\FormField;
use WPGraphQL\GF\Type\WPObject\AbstractObject;
use WPGraphQL\GF\Utils\GFUtils;

/**
* Class - OrderItem
*/
class OrderItem extends AbstractObject {
/**
* Type registered in WPGraphQL.
*
* @var string
*/
public static string $type = 'GfOrderItem';

/**
* {@inheritDoc}
*/
public static function get_description() : string {
return __( 'The entry order item.', 'wp-graphql-gravity-forms' );
}

/**
* {@inheritDoc}
*/
public static function get_fields() : array {
return [
'section' => [
'type' => 'String',
'description' => __( 'The section this order item belongs to.', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => $source['belongs_to'] ?? null,
],
'currency' => [
'type' => CurrencyEnum::$type,
'description' => __( 'The currency used for the order item', 'wp-graphql-gravity-forms' ),
],
'description' => [
'type' => 'String',
'description' => __( 'The item description', 'wp-graphql-gravity-forms' ),
],
'isDiscount' => [
'type' => 'Boolean',
'description' => __( 'Whether this is a discount item', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source) => ! empty( $source['is_discount'] ),
],
'isLineItem' => [
'type' => 'Boolean',
'description' => __( 'Whether this is a line item', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => ! empty( $source['is_line_item'] ),
],
'isRecurring' => [
'type' => 'Boolean',
'description' => __( 'Whether this is a recurring item', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => ! empty( $source['is_recurring'] ),
],
'isSetupFee' => [
'type' => 'Boolean',
'description' => __( 'Whether this is a setup fee', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => ! empty( $source['is_setup'] ),
],
'isShipping' => [
'type' => 'Boolean',
'description' => __( 'Whether this is a shipping fee', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => ! empty( $source['is_shipping'] ),
],
'isTrial' => [
'type' => 'Boolean',
'description' => __( 'Whether this is a trial item', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => ! empty( $source['is_trial'] ),
],
'name' => [
'type' => 'String',
'description' => __( 'The item name', 'wp-graphql-gravity-forms' ),
],
'options' => [
'type' => [ 'list_of' => OrderItemOption::$type ],
'description' => __( 'The item options', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => ! empty( $source['options'] ) ? $source['options'] : null,
],
'price' => [
'type' => 'Float',
'description' => __( 'The item price', 'wp-graphql-gravity-forms' ),
],
'quantity' => [
'type' => 'Float',
'description' => __( 'The item quantity', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => isset( $source['quantity'] ) ? (float) $source['quantity'] : null,
],
'subtotal' => [
'type' => 'Float',
'description' => __( 'The item subtotal', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => isset( $source['sub_total'] ) ? (float) $source['sub_total'] : null,
],
'connectedFormField' => [
'type' => FormField::$type,
'description' => __( 'The form field that the order item is connected to', 'wp-graphql-gravity-forms' ),
'resolve' => function( $source, array $args, AppContext $context ) {
return GFUtils::get_field_by_id( $context->gfForm->form, $source['id'] );
},
],
];
}
}
68 changes: 68 additions & 0 deletions src/Type/WPObject/Order/OrderItemOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* GraphQL Object Type - Gravity Forms Entry Order Item option.
*
* @package WPGraphQL\GF\Type\WPObject\Order
* @since @todo
*/

namespace WPGraphQL\GF\Type\WPObject\Order;

use WPGraphQL\AppContext;
use WPGraphQL\GF\Type\WPInterface\FormField;
use WPGraphQL\GF\Type\WPObject\AbstractObject;
use WPGraphQL\GF\Utils\GFUtils;

/**
* Class - OrderItemOption
*/
class OrderItemOption extends AbstractObject {
/**
* Type registered in WPGraphQL.
*
* @var string
*/
public static string $type = 'GfOrderItemOption';

/**
* {@inheritDoc}
*/
public static function get_description() : string {
return __( 'An option on an Order item.', 'wp-graphql-gravity-forms' );
}

/**
* {@inheritDoc}
*/
public static function get_fields() : array {
return [
'connectedFormField' => [
'type' => FormField::$type,
'description' => __( 'The form field that the order item is connected to', 'wp-graphql-gravity-forms' ),
'resolve' => function( $source, array $args, AppContext $context ) {
return GFUtils::get_field_by_id( $context->gfForm->form, $source['id'] );
},
],
'fieldLabel' => [
'type' => 'String',
'description' => __( 'The option\'s field label.', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => $source['field_label'] ?? null,
],
'name' => [
'type' => 'String',
'description' => __( 'The option name.', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => $source['option_name'] ?? null,
],
'optionLabel' => [
'type' => 'String',
'description' => __( 'The option label.', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => $source['option_label'] ?? null,
],
'price' => [
'type' => 'Float',
'description' => __( 'The option price.', 'wp-graphql-gravity-forms' ),
'resolve' => fn( $source ) => $source['price'] ?? null,
],
];
}
}
58 changes: 58 additions & 0 deletions src/Type/WPObject/Order/OrderSummary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* GraphQL Object Type - Gravity Forms Entry Order
*
* @see https://docs.gravityforms.com/confirmation/
*
* @package WPGraphQL\GF\Type\WPObject\Order
* @since @todo
*/

namespace WPGraphQL\GF\Type\WPObject\Order;

use WPGraphQL\AppContext;
use WPGraphQL\GF\Type\Enum\CurrencyEnum;
use WPGraphQL\GF\Type\WPObject\AbstractObject;

/**
* Class - OrderSummary
*/
class OrderSummary extends AbstractObject {
/**
* Type registered in WPGraphQL.
*
* @var string
*/
public static string $type = 'GfOrderSummary';

/**
* {@inheritDoc}
*/
public static function get_description() : string {
return __( 'The entry order information.', 'wp-graphql-gravity-forms' );
}

/**
* {@inheritDoc}
*/
public static function get_fields() : array {
return [
'currency' => [
'type' => CurrencyEnum::$type,
'description' => __( 'The currency used for the order', 'wp-graphql-gravity-forms' ),
],
'items' => [
'type' => [ 'list_of' => OrderItem::$type ],
'description' => __( 'The order item details.', 'wp-graphql-gravity-forms' ),
],
'subtotal' => [
'type' => 'Float',
'description' => __( 'The order subtotal.', 'wp-graphql-gravity-forms' ),
],
'total' => [
'type' => 'Float',
'description' => __( 'The order total', 'wp-graphql-gravity-forms' ),
],
];
}
}
3 changes: 3 additions & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@
'WPGraphQL\\GF\\Type\\WPObject\\Form\\FormSchedule' => $baseDir . '/src/Type/WPObject/Form/FormSchedule.php',
'WPGraphQL\\GF\\Type\\WPObject\\Form\\FormScheduleDetails' => $baseDir . '/src/Type/WPObject/Form/FormScheduleDetails.php',
'WPGraphQL\\GF\\Type\\WPObject\\Form\\FormSubmitButton' => $baseDir . '/src/Type/WPObject/Form/FormSubmitButton.php',
'WPGraphQL\\GF\\Type\\WPObject\\Order\\OrderItem' => $baseDir . '/src/Type/WPObject/Order/OrderItem.php',
'WPGraphQL\\GF\\Type\\WPObject\\Order\\OrderItemOption' => $baseDir . '/src/Type/WPObject/Order/OrderItemOption.php',
'WPGraphQL\\GF\\Type\\WPObject\\Order\\OrderSummary' => $baseDir . '/src/Type/WPObject/Order/OrderSummary.php',
'WPGraphQL\\GF\\Type\\WPObject\\Settings\\Logger' => $baseDir . '/src/Type/WPObject/Settings/Logger.php',
'WPGraphQL\\GF\\Type\\WPObject\\Settings\\Settings' => $baseDir . '/src/Type/WPObject/Settings/Settings.php',
'WPGraphQL\\GF\\Type\\WPObject\\Settings\\SettingsLogging' => $baseDir . '/src/Type/WPObject/Settings/SettingsLogging.php',
Expand Down
3 changes: 3 additions & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ class ComposerStaticInit76119eb71b081cd33ea3524d54e1ff40
'WPGraphQL\\GF\\Type\\WPObject\\Form\\FormSchedule' => __DIR__ . '/../..' . '/src/Type/WPObject/Form/FormSchedule.php',
'WPGraphQL\\GF\\Type\\WPObject\\Form\\FormScheduleDetails' => __DIR__ . '/../..' . '/src/Type/WPObject/Form/FormScheduleDetails.php',
'WPGraphQL\\GF\\Type\\WPObject\\Form\\FormSubmitButton' => __DIR__ . '/../..' . '/src/Type/WPObject/Form/FormSubmitButton.php',
'WPGraphQL\\GF\\Type\\WPObject\\Order\\OrderItem' => __DIR__ . '/../..' . '/src/Type/WPObject/Order/OrderItem.php',
'WPGraphQL\\GF\\Type\\WPObject\\Order\\OrderItemOption' => __DIR__ . '/../..' . '/src/Type/WPObject/Order/OrderItemOption.php',
'WPGraphQL\\GF\\Type\\WPObject\\Order\\OrderSummary' => __DIR__ . '/../..' . '/src/Type/WPObject/Order/OrderSummary.php',
'WPGraphQL\\GF\\Type\\WPObject\\Settings\\Logger' => __DIR__ . '/../..' . '/src/Type/WPObject/Settings/Logger.php',
'WPGraphQL\\GF\\Type\\WPObject\\Settings\\Settings' => __DIR__ . '/../..' . '/src/Type/WPObject/Settings/Settings.php',
'WPGraphQL\\GF\\Type\\WPObject\\Settings\\SettingsLogging' => __DIR__ . '/../..' . '/src/Type/WPObject/Settings/SettingsLogging.php',
Expand Down
4 changes: 2 additions & 2 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => '9051541fac98597020e03920c274d304827f04e7',
'reference' => 'fe69948c26d7969850adfb7f3f2e9ca15482eb7d',
'name' => 'harness-software/wp-graphql-gravity-forms',
'dev' => false,
),
Expand All @@ -16,7 +16,7 @@
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => '9051541fac98597020e03920c274d304827f04e7',
'reference' => 'fe69948c26d7969850adfb7f3f2e9ca15482eb7d',
'dev_requirement' => false,
),
'yahnis-elsts/plugin-update-checker' => array(
Expand Down