Skip to content
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
19 changes: 16 additions & 3 deletions includes/class-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,17 +396,30 @@ public static function resolve_product_type( $value ) {
public static function resolve_product_variation_type( $value ) {
$type_registry = \WPGraphQL::get_type_registry();
$possible_types = WooGraphQL::get_enabled_product_variation_types();
$product_type = $value->get_type();

if ( isset( $possible_types[ $product_type ] ) ) {
// Normally $value is a Product_Variation model. In some setups the
// variation node is loaded through the generic post loader (e.g. a cart
// item's variation under Polylang, which doesn't manage the
// product_variation post-type) and arrives as a base
// \WPGraphQL\Model\Post with no get_type(). Fall back to resolving the
// variation's product type from its ID.
if ( is_callable( [ $value, 'get_type' ] ) ) {
$product_type = $value->get_type();
} else {
$variation_id = $value->databaseId ?? ( $value->ID ?? 0 );
$product = $variation_id ? wc_get_product( $variation_id ) : false;
$product_type = $product ? $product->get_type() : null;
}

if ( $product_type && isset( $possible_types[ $product_type ] ) ) {
return $type_registry->get_type( $possible_types[ $product_type ] );
}

throw new UserError(
sprintf(
/* translators: %s: Product type */
__( 'The "%s" product variation type is not supported by the core WPGraphQL for WooCommerce (WooGraphQL) schema.', 'wp-graphql-woocommerce' ),
$value->type
$product_type ?? ''
)
);
}
Expand Down
1 change: 0 additions & 1 deletion includes/type/interface/class-product-with-pricing.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public static function get_fields() {
// @codingStandardsIgnoreLine.
return $source->priceRaw;
} else {
graphql_debug( $source->price );
// @codingStandardsIgnoreLine.
return $source->price;
}
Expand Down
56 changes: 56 additions & 0 deletions tests/wpunit/CartQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,62 @@ public function testCartItemQuery() {
$this->assertQuerySuccessful( $response, $this->getExpectedCartItemData( 'cartItem', $key ) );
}

// Resolves a cart item's variation when its node loads as a base
// \WPGraphQL\Model\Post (no get_type()) instead of a Product_Variation —
// the case on sites where the WC model upgrade doesn't run for the
// product_variation post type (e.g. Polylang). Without the fallback in
// Post_Types::resolve_product_variation_type() this query fatals.
public function testCartItemVariationResolvesWhenLoadedAsPostModel() {
$cart = \WC()->cart;
$variations = $this->factory->product_variation->createSome();
$key = $cart->add_to_cart(
$variations['product'],
1,
$variations['variations'][0],
[ 'attribute_pa_color' => 'red' ]
);

// Drop the WC model upgrade so the variation node arrives as a base Post.
remove_filter(
'graphql_dataloader_pre_get_model',
[ '\WPGraphQL\WooCommerce\Data\Loader\WC_CPT_Loader', 'inject_post_loader_models' ],
10
);

$query = '
query ($key: ID!) {
cartItem(key: $key) {
variation {
node {
__typename
... on SimpleProductVariation {
databaseId
}
}
}
}
}
';

$response = $this->graphql( [ 'query' => $query, 'variables' => [ 'key' => $key ] ] );

// Restore the filter before asserting so its removal can't leak into other tests.
add_filter(
'graphql_dataloader_pre_get_model',
[ '\WPGraphQL\WooCommerce\Data\Loader\WC_CPT_Loader', 'inject_post_loader_models' ],
10,
3
);

$this->assertQuerySuccessful(
$response,
[
$this->expectedField( 'cartItem.variation.node.__typename', 'SimpleProductVariation' ),
$this->expectedField( 'cartItem.variation.node.databaseId', $variations['variations'][0] ),
]
);
}

public function testCartItemConnection() {
$keys = $this->factory->cart->add(
[
Expand Down
6 changes: 6 additions & 0 deletions tests/wpunit/CoreInterfaceQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public function testOrderAsNodeWithComments() {
// Create order and order note to be queried.
$order_id = $this->orders->create();
$order = \wc_get_order( $order_id );

// Adding a customer note triggers the Customer Note email, which on newer
// WooCommerce logs an extra "Email sent" order note. Disable it so the
// comment count stays deterministic.
add_filter( 'woocommerce_email_enabled_customer_note', '__return_false' );

$order->add_order_note( 'testnote' );
$order->add_order_note( 'testcustomernote', 1, true );

Expand Down
5 changes: 5 additions & 0 deletions tests/wpunit/OrderQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@ public function testOrderNotesQuery() {
$order_id = $this->factory->order->createNew();
$order = wc_get_order( $order_id );

// Adding a customer note triggers the Customer Note email, which on newer
// WooCommerce logs an extra "Email sent" order note. Disable it so the
// order's notes are exactly the ones added below.
add_filter( 'woocommerce_email_enabled_customer_note', '__return_false' );

// Add some order notes
$note1_id = $order->add_order_note( 'Test order note 1', false );
$note2_id = $order->add_order_note( 'Test customer note 2', true );
Expand Down
Loading