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

MetaData type and queries #123

Merged
merged 3 commits into from
Aug 21, 2019
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
1 change: 1 addition & 0 deletions includes/class-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static function graphql_register_types() {
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Cart_Type::register();
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Variation_Attribute_Type::register();
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Payment_Gateway_Type::register();
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Meta_Data_Type::register();

// Object fields.
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Product_Category_Type::register_fields();
Expand Down
12 changes: 12 additions & 0 deletions includes/model/class-customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public function __construct( $id ) {
parent::__construct( $restricted_cap, $allowed_restricted_fields, $id );
}

/**
* Forwards function calls to WC_Data sub-class instance.
*
* @param string $method - function name.
* @param array $args - function call arguments.
*
* @return mixed
*/
public function __call( $method, $args ) {
return $this->data->$method( ...$args );
}

/**
* Initializes the Customer field resolvers
*
Expand Down
12 changes: 12 additions & 0 deletions includes/model/class-order-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public function __construct( $item ) {
parent::__construct( $restricted_cap, $allowed_restricted_fields, $author_id );
}

/**
* Forwards function calls to WC_Data sub-class instance.
*
* @param string $method - function name.
* @param array $args - function call arguments.
*
* @return mixed
*/
public function __call( $method, $args ) {
return $this->data->$method( ...$args );
}

/**
* Initializes the Order field resolvers
*
Expand Down
212 changes: 212 additions & 0 deletions includes/type/object/class-meta-data-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php
/**
* WPObject Type - Order_Item_Type
*
* Registers MetaData type and queries
*
* @package \WPGraphQL\Extensions\WooCommerce\Type\WPObject
* @since 0.0.2
*/

namespace WPGraphQL\Extensions\WooCommerce\Type\WPObject;

use WPGraphQL\AppContext;
use WPGraphQL\Extensions\WooCommerce\Data\Factory;

/**
* Class Meta_Data_Type
*/
class Meta_Data_Type {
/**
* Register Order type and queries to the WPGraphQL schema
*/
public static function register() {
register_graphql_object_type(
'MetaData',
array(
'description' => __( 'Extra data defined on the WC object', 'wp-graphql-woocommerce' ),
'fields' => array(
'id' => array(
'type' => array( 'non_null' => 'String' ),
'description' => __( 'Meta ID.', 'wp-graphql-woocommerce' ),
'resolve' => function ( $source ) {
return ! empty( $source->id ) ? $source->id : null;
},
),
'key' => array(
'type' => array( 'non_null' => 'String' ),
'description' => __( 'Meta key.', 'wp-graphql-woocommerce' ),
'resolve' => function ( $source ) {
return ! empty( $source->key ) ? $source->key : null;
},
),
'value' => array(
'type' => array( 'non_null' => 'String' ),
'description' => __( 'Meta value.', 'wp-graphql-woocommerce' ),
'resolve' => function ( $source ) {
return ! empty( $source->value ) ? $source->value : null;
},
),
),
)
);

// Register 'extraData' field on CartItem.
register_graphql_field(
'CartItem',
'extraData',
array(
'type' => array( 'list_of' => 'MetaData' ),
'description' => __( 'Object meta data', 'wp-graphql-woocommerce' ),
'args' => array(
'key' => array(
'type' => 'String',
'description' => __( 'Retrieve meta by key', 'wp-graphql-woocommerce' ),
),
'keysIn' => array(
'type' => array( 'list_of' => 'String' ),
'description' => __( 'Retrieve multiple metas by key', 'wp-graphql-woocommerce' ),
),
),
'resolve' => function( $source, array $args ) {
// Check if "key" argument set and assigns to target "keys" array.
if ( ! empty( $args['key'] ) && ! empty( $source[ $args['key'] ] ) ) {
$keys = array( $args['key'] );
} elseif ( ! empty( $args['keysIn'] ) ) { // Check if "keysIn" argument set and assigns to target "keys" array.
$keys = array();
foreach ( $args['keysIn'] as $key ) {
if ( ! empty( $source[ $key ] ) ) {
$keys[] = $key;
}
}
} else { // If no arguments set, all extra data keys are assigns to target "keys" array.
$keys = array_diff(
array_keys( $source ),
array(
'key',
'product_id',
'variation_id',
'variation',
'quantity',
'data',
'data_hash',
'line_tax_data',
'line_subtotal',
'line_subtotal_tax',
'line_total',
'line_tax',
)
);
}
// Create meta ID prefix.
$id_prefix = apply_filters( 'cart_meta_id_prefix', 'cart_' );

// Format meta data for resolution.
$data = array();
foreach ( $keys as $key ) {
$data[] = (object) array(
'id' => "{$id_prefix}_{$key}",
'key' => $key,
'value' => $source[ $key ],
);
}

return $data;
},
)
);

// Register 'metaData' field on WC CRUD types.
$types = array(
'Coupon',
'Customer',
'CouponLine',
'LineItem',
'FeeLine',
'Order',
'Product',
'ProductVariation',
'Refund',
'ShippingLine',
'TaxLine',
);

foreach ( $types as $type ) {
register_graphql_field(
$type,
'metaData',
array(
'type' => array( 'list_of' => 'MetaData' ),
'description' => __( 'Object meta data', 'wp-graphql-woocommerce' ),
'args' => array(
'key' => array(
'type' => 'String',
'description' => __( 'Retrieve meta by key', 'wp-graphql-woocommerce' ),
),
'keysIn' => array(
'type' => array( 'list_of' => 'String' ),
'description' => __( 'Retrieve multiple metas by key', 'wp-graphql-woocommerce' ),
),
'multiple' => array(
'type' => 'Boolean',
'description' => __( 'Retrieve meta with matching keys', 'wp-graphql-woocommerce' ),
),
),
'resolve' => function( $source, array $args ) {
// Set unique flag.
$single = ! empty( $args['multiple'] ) ? ! $args['multiple'] : true;

// Check "key" argument and format meta_data objects.
if ( ! empty( $args['key'] ) && $source->meta_exists( $args['key'] ) ) {
$data = $source->get_meta( $args['key'], $single );
if ( ! is_array( $data ) ) {
$data = array_filter(
$source->get_meta_data(),
function( $meta ) use ( $data ) {
return $meta->value === $data;
}
);
}
} elseif ( ! empty( $args['keysIn'] ) ) { // Check "keysIn" argument and format meta_data objects.
$keys = $args['keysIn'];

$found = array();
$data = array_filter(
$source->get_meta_data(),
function( $meta ) use ( $keys, $single, &$found ) {
if ( in_array( $meta->key, $keys, true ) ) {
if ( $single ) {
if ( ! in_array( $meta->key, $found, true ) ) {
$found[] = $meta->key;
return true;
}
return false;
}
return true;
}
}
);
} else { // If no arguments set return all meta (in accordance with unique flag).
$found = array();
$data = array_filter(
$source->get_meta_data(),
function( $meta ) use ( $single, &$found ) {
if ( $single ) {
if ( ! in_array( $meta->key, $found, true ) ) {
$found[] = $meta->key;
return true;
}
return false;
}
return true;
}
);
}

return ! empty( $data ) ? $data : null;
},
)
);
}
}
}
5 changes: 5 additions & 0 deletions tests/_support/Helper/crud-helpers/coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function create( $args = array(), $save = true ) {
)
);

// Set meta data.
if ( ! empty( $args['meta_data'] ) ) {
$coupon->set_meta_data( $args['meta_data'] );
}

// Return instance in not saving.
if( ! $save ) {
return $coupon;
Expand Down
6 changes: 6 additions & 0 deletions tests/_support/Helper/crud-helpers/customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function create( $args = array() ) {
$email = $this->dummy->email();
$phone = $this->dummy->telephone();

// Set data.
$customer->set_props(
array_merge(
array(
Expand Down Expand Up @@ -67,6 +68,11 @@ public function create( $args = array() ) {
)
);

// Set meta data.
if ( ! empty( $args['meta_data'] ) ) {
$customer->set_meta_data( $args['meta_data'] );
}

return absint( $customer->save() );
}

Expand Down
26 changes: 23 additions & 3 deletions tests/_support/Helper/crud-helpers/order-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public function add_coupon( $order, $coupon_id = 0, $save = true ) {

public function add_fee( $order, $args = array(), $save = true ) {
// Retrieve order.
if ( ! is_a( $order, WC_Order::class ) ) {
$order = new WC_Order( $order );
}
$order = new WC_Order( $order );

// Get thre customer country code.
$country_code = $order->get_shipping_country();
Expand All @@ -76,6 +74,12 @@ public function add_fee( $order, $args = array(), $save = true ) {
if ( ! empty( $args ) ) {
$item->set_props( $args );
}

// Set meta data.
if ( ! empty( $args['meta_data'] ) ) {
$item->set_meta_data( $args['meta_data'] );
}

// Calculating Fee taxes
$item->calculate_taxes( $calculate_tax_for );

Expand Down Expand Up @@ -113,6 +117,11 @@ public function add_shipping( $order, $args = array(), $save = true ) {
)
);

// Set meta data.
if ( ! empty( $args['meta_data'] ) ) {
$item->set_meta_data( $args['meta_data'] );
}

$item_id = $item->save();

$order->add_item( $item );
Expand Down Expand Up @@ -151,6 +160,12 @@ public function add_tax( $order, $args = array(), $save = true ) {
$args
)
);

// Set meta data.
if ( ! empty( $args['meta_data'] ) ) {
$item->set_meta_data( $args['meta_data'] );
}

$item->save();

$order->add_item( $item );
Expand Down Expand Up @@ -189,6 +204,11 @@ public function add_line_item( $order, $args = array(), $save = true ) {
)
);

// Set meta data.
if ( ! empty( $args['meta_data'] ) ) {
$item->set_meta_data( $args['meta_data'] );
}

$order->add_item( $item );

if ( ! $save ) {
Expand Down
7 changes: 6 additions & 1 deletion tests/_support/Helper/crud-helpers/order.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ public function create( $args = array(), $items = array() ) {
$order->set_cart_tax( 0 );
$order->set_shipping_tax( 0 );
$order->set_total( 50 ); // 4 x $10 simple helper product


// Set meta data.
if ( ! empty( $args['meta_data'] ) ) {
$order->set_meta_data( $args['meta_data'] );
}

// Save and return ID.
return $order->save();
}
Expand Down
9 changes: 9 additions & 0 deletions tests/_support/Helper/crud-helpers/product-variation.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public function create( $product_id, $args = array() ) {
)
);
$variation_1->set_attributes( array( 'pa_size' => 'small' ) );
if ( ! empty( $args['meta_data'] ) ) {
$variation_1->set_meta_data( $args['meta_data'] );
}

// Create medium size variation with image
$image_id = \wp_insert_post(
Expand All @@ -64,6 +67,9 @@ public function create( $product_id, $args = array() ) {
)
);
$variation_2->set_attributes( array( 'pa_size' => 'medium' ) );
if ( ! empty( $args['meta_data'] ) ) {
$variation_2->set_meta_data( $args['meta_data'] );
}

// Create large size variation
$variation_3 = new WC_Product_Variation();
Expand All @@ -76,6 +82,9 @@ public function create( $product_id, $args = array() ) {
)
);
$variation_3->set_attributes( array( 'pa_size' => 'large' ) );
if ( ! empty( $args['meta_data'] ) ) {
$variation_3->set_meta_data( $args['meta_data'] );
}

return array(
'variations' => array(
Expand Down
Loading