forked from wp-graphql/wp-graphql-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,587 additions
and
951 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Connection - Cart_Items | ||
* | ||
* Registers connections to CartItem | ||
* | ||
* @package WPGraphQL\Extensions\WooCommerce\Connection | ||
* @since 0.0.3 | ||
*/ | ||
|
||
namespace WPGraphQL\Extensions\WooCommerce\Connection; | ||
|
||
use WPGraphQL\Extensions\WooCommerce\Data\Factory; | ||
|
||
/** | ||
* Class - Cart_Items | ||
*/ | ||
class Cart_Items { | ||
/** | ||
* Registers the various connections from other Types to CartItem | ||
*/ | ||
public static function register_connections() { | ||
// From Cart. | ||
register_graphql_connection( self::get_connection_config() ); | ||
} | ||
|
||
/** | ||
* Given an array of $args, this returns the connection config, merging the provided args | ||
* with the defaults | ||
* | ||
* @access public | ||
* @param array $args - Connection configuration. | ||
* | ||
* @return array | ||
*/ | ||
public static function get_connection_config( $args = [] ) { | ||
$defaults = array( | ||
'fromType' => 'Cart', | ||
'toType' => 'CartItem', | ||
'fromFieldName' => 'contents', | ||
'connectionArgs' => self::get_connection_args(), | ||
'resolve' => function ( $source, $args, $context, $info ) { | ||
return Factory::resolve_cart_item_connection( $source, $args, $context, $info ); | ||
}, | ||
); | ||
return array_merge( $defaults, $args ); | ||
} | ||
|
||
/** | ||
* Returns array of where args | ||
* | ||
* @return array | ||
*/ | ||
public static function get_connection_args() { | ||
return array( | ||
'needShipping' => array( | ||
'type' => 'Boolean', | ||
'description' => __( 'Limit results to cart item that require shipping', 'wp-graphql-woocommerce' ), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/data/connection/class-cart-item-connection-resolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* ConnectionResolver - Cart_Item_Connection_Resolver | ||
* | ||
* Resolves connections to CartItem | ||
* | ||
* @package WPGraphQL\Extensions\WooCommerce\Data\Connection | ||
* @since 0.0.3 | ||
*/ | ||
|
||
namespace WPGraphQL\Extensions\WooCommerce\Data\Connection; | ||
|
||
use GraphQL\Type\Definition\ResolveInfo; | ||
use GraphQLRelay\Relay; | ||
use WPGraphQL\AppContext; | ||
|
||
/** | ||
* Class Cart_Item_Connection_Resolver | ||
*/ | ||
class Cart_Item_Connection_Resolver { | ||
/** | ||
* Returns an array of cart items filtered based upon query arguments | ||
* | ||
* @param array $items - Cart items. | ||
* @param array $args - Query arguments. | ||
* | ||
* @return array | ||
*/ | ||
public function filter( $items, $args = array() ) { | ||
$filter_items = array_values( $items ); | ||
|
||
usort( | ||
$filter_items, | ||
function( $item_a, $item_b ) { | ||
return strcmp( $item_a['key'], $item_b['key'] ); | ||
} | ||
); | ||
|
||
return $filter_items; | ||
} | ||
|
||
/** | ||
* Creates connection | ||
* | ||
* @param mixed $source - Connection source Model instance. | ||
* @param array $args - Connection arguments. | ||
* @param AppContext $context - AppContext object. | ||
* @param ResolveInfo $info - ResolveInfo object. | ||
*/ | ||
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) { | ||
$items = $this->filter( $source->get_cart(), $args ); | ||
|
||
$connection = Relay::connectionFromArray( $items, $args ); | ||
$nodes = array(); | ||
if ( ! empty( $connection['edges'] ) && is_array( $connection['edges'] ) ) { | ||
foreach ( $connection['edges'] as $edge ) { | ||
$nodes[] = ! empty( $edge['node'] ) ? $edge['node'] : null; | ||
} | ||
} | ||
$connection['nodes'] = ! empty( $nodes ) ? $nodes : null; | ||
return ! empty( $items ) ? $connection : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.