2019-04-18 23:22:00 -04:00
|
|
|
<?php
|
|
|
|
|
|
2020-10-27 23:46:28 -04:00
|
|
|
class CartQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
|
2020-03-24 20:55:13 -04:00
|
|
|
private function key_to_cursor( $key ) {
|
2022-02-04 15:00:44 -05:00
|
|
|
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
|
2021-06-12 22:06:44 -04:00
|
|
|
return base64_encode( 'arrayconnection:' . $key );
|
2020-03-24 20:55:13 -04:00
|
|
|
}
|
|
|
|
|
|
2020-10-23 12:09:53 -04:00
|
|
|
public function getExpectedCartData() {
|
|
|
|
|
$cart = WC()->cart;
|
2022-08-26 14:53:36 -04:00
|
|
|
return [
|
2021-07-05 13:51:52 -04:00
|
|
|
$this->expectedField( 'cart.subtotal', \wc_graphql_price( $cart->get_subtotal() ) ),
|
|
|
|
|
$this->expectedField( 'cart.subtotalTax', \wc_graphql_price( $cart->get_subtotal_tax() ) ),
|
|
|
|
|
$this->expectedField( 'cart.discountTotal', \wc_graphql_price( $cart->get_discount_total() ) ),
|
|
|
|
|
$this->expectedField( 'cart.discountTax', \wc_graphql_price( $cart->get_discount_tax() ) ),
|
|
|
|
|
$this->expectedField( 'cart.shippingTotal', \wc_graphql_price( $cart->get_shipping_total() ) ),
|
|
|
|
|
$this->expectedField( 'cart.shippingTax', \wc_graphql_price( $cart->get_shipping_tax() ) ),
|
|
|
|
|
$this->expectedField( 'cart.contentsTotal', \wc_graphql_price( $cart->get_cart_contents_total() ) ),
|
|
|
|
|
$this->expectedField( 'cart.contentsTax', \wc_graphql_price( $cart->get_cart_contents_tax() ) ),
|
|
|
|
|
$this->expectedField( 'cart.feeTotal', \wc_graphql_price( $cart->get_fee_total() ) ),
|
|
|
|
|
$this->expectedField( 'cart.feeTax', \wc_graphql_price( $cart->get_fee_tax() ) ),
|
|
|
|
|
$this->expectedField( 'cart.total', \wc_graphql_price( $cart->get_totals()['total'] ) ),
|
|
|
|
|
$this->expectedField( 'cart.totalTax', \wc_graphql_price( $cart->get_total_tax() ) ),
|
|
|
|
|
$this->expectedField( 'cart.isEmpty', $cart->is_empty() ),
|
|
|
|
|
$this->expectedField( 'cart.displayPricesIncludeTax', $cart->display_prices_including_tax() ),
|
|
|
|
|
$this->expectedField( 'cart.needsShippingAddress', $cart->needs_shipping_address() ),
|
2024-08-06 16:54:18 -06:00
|
|
|
$this->expectedField( 'cart.rawSubtotal', static::NOT_NULL ),
|
|
|
|
|
$this->expectedField( 'cart.rawSubtotalTax', static::NOT_NULL ),
|
|
|
|
|
$this->expectedField( 'cart.rawDiscountTotal', static::NOT_NULL ),
|
|
|
|
|
$this->expectedField( 'cart.rawDiscountTax', static::NOT_NULL ),
|
|
|
|
|
$this->expectedField( 'cart.rawShippingTotal', static::NOT_NULL ),
|
|
|
|
|
$this->expectedField( 'cart.rawShippingTax', static::NOT_NULL ),
|
|
|
|
|
$this->expectedField( 'cart.rawContentsTotal', static::NOT_NULL ),
|
|
|
|
|
$this->expectedField( 'cart.rawContentsTax', static::NOT_NULL ),
|
|
|
|
|
$this->expectedField( 'cart.rawFeeTotal', static::NOT_NULL ),
|
|
|
|
|
$this->expectedField( 'cart.rawFeeTax', static::NOT_NULL ),
|
|
|
|
|
$this->expectedField( 'cart.rawtotal', static::NOT_NULL ),
|
|
|
|
|
$this->expectedField( 'cart.rawTotalTax', static::NOT_NULL ),
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2020-10-23 12:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
2020-10-27 23:46:28 -04:00
|
|
|
public function getExpectedCartItemData( $path, $cart_item_key ) {
|
|
|
|
|
$cart = WC()->cart;
|
|
|
|
|
$item = $cart->get_cart_item( $cart_item_key );
|
2022-08-26 14:53:36 -04:00
|
|
|
return [
|
2020-10-27 23:46:28 -04:00
|
|
|
$this->expectedObject(
|
2021-07-05 13:51:52 -04:00
|
|
|
$path,
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2022-02-04 15:00:44 -05:00
|
|
|
$this->expectedField( 'key', $item['key'] ),
|
2024-08-06 16:54:18 -06:00
|
|
|
$this->expectedField( 'product.node.id', $this->toRelayId( 'post', $item['product_id'] ) ),
|
2022-02-04 15:00:44 -05:00
|
|
|
$this->expectedField( 'product.node.databaseId', $item['product_id'] ),
|
2021-07-05 13:51:52 -04:00
|
|
|
$this->expectedField(
|
2022-02-04 15:00:44 -05:00
|
|
|
'variation.node.id',
|
2021-07-05 13:51:52 -04:00
|
|
|
! empty( $item['variation_id'] )
|
2024-08-06 16:54:18 -06:00
|
|
|
? $this->toRelayId( 'post', $item['variation_id'] )
|
2021-07-05 13:51:52 -04:00
|
|
|
: 'NULL'
|
|
|
|
|
),
|
|
|
|
|
$this->expectedField(
|
2022-02-04 15:00:44 -05:00
|
|
|
'variation.node.databaseId',
|
2021-07-05 13:51:52 -04:00
|
|
|
! empty( $item['variation_id'] ) ? $item['variation_id'] : 'NULL'
|
|
|
|
|
),
|
2022-02-04 15:00:44 -05:00
|
|
|
$this->expectedField( 'quantity', $item['quantity'] ),
|
|
|
|
|
$this->expectedField( 'subtotal', \wc_graphql_price( $item['line_subtotal'] ) ),
|
|
|
|
|
$this->expectedField( 'subtotalTax', \wc_graphql_price( $item['line_subtotal_tax'] ) ),
|
|
|
|
|
$this->expectedField( 'total', \wc_graphql_price( $item['line_total'] ) ),
|
|
|
|
|
$this->expectedField( 'tax', \wc_graphql_price( $item['line_tax'] ) ),
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2022-02-04 15:00:44 -05:00
|
|
|
),
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2020-10-23 12:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
2019-04-24 15:05:42 -04:00
|
|
|
// tests
|
|
|
|
|
public function testCartQuery() {
|
|
|
|
|
$cart = WC()->cart;
|
2020-10-27 23:46:28 -04:00
|
|
|
$this->factory->cart->add(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2020-10-27 23:46:28 -04:00
|
|
|
'product_id' => $this->factory->product->createSimple(),
|
2022-02-04 15:00:44 -05:00
|
|
|
'quantity' => 2,
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
[
|
2020-10-27 23:46:28 -04:00
|
|
|
'product_id' => $this->factory->product->createSimple(),
|
2022-02-04 15:00:44 -05:00
|
|
|
'quantity' => 1,
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2020-10-27 23:46:28 -04:00
|
|
|
);
|
2019-04-24 13:08:29 -04:00
|
|
|
|
2019-04-24 15:05:42 -04:00
|
|
|
$query = '
|
2019-10-17 21:39:38 -04:00
|
|
|
query {
|
2019-04-24 15:05:42 -04:00
|
|
|
cart {
|
|
|
|
|
subtotal
|
|
|
|
|
subtotalTax
|
|
|
|
|
discountTotal
|
|
|
|
|
discountTax
|
|
|
|
|
shippingTotal
|
|
|
|
|
shippingTax
|
|
|
|
|
contentsTotal
|
|
|
|
|
contentsTax
|
|
|
|
|
feeTotal
|
|
|
|
|
feeTax
|
|
|
|
|
total
|
|
|
|
|
totalTax
|
|
|
|
|
isEmpty
|
|
|
|
|
displayPricesIncludeTax
|
|
|
|
|
needsShippingAddress
|
2020-11-23 14:46:41 -05:00
|
|
|
totalTaxes {
|
|
|
|
|
id
|
|
|
|
|
isCompound
|
|
|
|
|
amount
|
|
|
|
|
label
|
|
|
|
|
}
|
2022-08-29 14:24:50 -03:00
|
|
|
rawSubtotal: subtotal(format: RAW)
|
|
|
|
|
rawSubtotalTax: subtotalTax(format: RAW)
|
|
|
|
|
rawDiscountTotal: discountTotal(format: RAW)
|
|
|
|
|
rawDiscountTax: discountTax(format: RAW)
|
|
|
|
|
rawShippingTotal: shippingTotal(format:RAW)
|
|
|
|
|
rawShippingTax: shippingTax(format: RAW)
|
|
|
|
|
rawContentsTotal: contentsTotal(format: RAW)
|
|
|
|
|
rawContentsTax: contentsTax(format: RAW)
|
|
|
|
|
rawFeeTotal: feeTotal(format: RAW)
|
|
|
|
|
rawFeeTax: feeTax(format: RAW)
|
|
|
|
|
rawtotal: total(format: RAW)
|
|
|
|
|
rawTotalTax: totalTax(format: RAW)
|
2019-04-24 15:05:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
2019-04-24 13:08:29 -04:00
|
|
|
|
2019-04-24 15:05:42 -04:00
|
|
|
/**
|
2019-04-24 13:08:29 -04:00
|
|
|
* Assertion One
|
|
|
|
|
*/
|
2020-10-27 23:46:28 -04:00
|
|
|
$response = $this->graphql( compact( 'query' ) );
|
2019-04-24 13:08:29 -04:00
|
|
|
|
2020-10-27 23:46:28 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $this->getExpectedCartData() );
|
2019-04-24 15:05:42 -04:00
|
|
|
}
|
2019-04-24 13:08:29 -04:00
|
|
|
|
2019-04-24 15:05:42 -04:00
|
|
|
public function testCartItemQuery() {
|
2022-02-04 15:00:44 -05:00
|
|
|
$cart = \WC()->cart;
|
2020-10-27 23:46:28 -04:00
|
|
|
$variations = $this->factory->product_variation->createSome();
|
|
|
|
|
|
2019-04-24 15:05:42 -04:00
|
|
|
$key = $cart->add_to_cart(
|
|
|
|
|
$variations['product'],
|
|
|
|
|
3,
|
2020-10-02 12:37:30 -04:00
|
|
|
$variations['variations'][0],
|
2022-08-26 14:53:36 -04:00
|
|
|
[ 'attribute_pa_color' => 'red' ]
|
2019-04-24 15:05:42 -04:00
|
|
|
);
|
2019-04-24 13:08:29 -04:00
|
|
|
|
2019-04-24 15:05:42 -04:00
|
|
|
$query = '
|
2019-10-17 21:39:38 -04:00
|
|
|
query ($key: ID!) {
|
|
|
|
|
cartItem(key: $key) {
|
2019-04-24 15:05:42 -04:00
|
|
|
key
|
|
|
|
|
product {
|
2020-11-24 00:28:20 -05:00
|
|
|
node {
|
2019-10-17 21:39:38 -04:00
|
|
|
id
|
2020-09-16 16:32:14 -04:00
|
|
|
databaseId
|
2019-10-17 21:39:38 -04:00
|
|
|
}
|
2019-04-24 15:05:42 -04:00
|
|
|
}
|
|
|
|
|
variation {
|
2020-11-24 00:28:20 -05:00
|
|
|
attributes {
|
|
|
|
|
id
|
|
|
|
|
attributeId
|
|
|
|
|
name
|
|
|
|
|
label
|
|
|
|
|
value
|
|
|
|
|
}
|
|
|
|
|
node {
|
|
|
|
|
id
|
|
|
|
|
databaseId
|
|
|
|
|
}
|
2019-04-24 15:05:42 -04:00
|
|
|
}
|
|
|
|
|
quantity
|
|
|
|
|
subtotal
|
|
|
|
|
subtotalTax
|
|
|
|
|
total
|
|
|
|
|
tax
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
2019-04-24 13:08:29 -04:00
|
|
|
|
2019-04-24 15:05:42 -04:00
|
|
|
/**
|
2019-04-24 13:08:29 -04:00
|
|
|
* Assertion One
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
$variables = [ 'key' => $key ];
|
2022-02-04 15:00:44 -05:00
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
2019-04-24 13:08:29 -04:00
|
|
|
|
2020-10-27 23:46:28 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $this->getExpectedCartItemData( 'cartItem', $key ) );
|
2019-04-24 15:05:42 -04:00
|
|
|
}
|
2019-04-24 13:08:29 -04:00
|
|
|
|
2026-06-30 10:03:48 -04:00
|
|
|
// 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] ),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 15:05:42 -04:00
|
|
|
public function testCartItemConnection() {
|
2020-10-27 23:46:28 -04:00
|
|
|
$keys = $this->factory->cart->add(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2020-10-27 23:46:28 -04:00
|
|
|
'product_id' => $this->factory->product->createSimple(
|
2022-08-26 14:53:36 -04:00
|
|
|
[ 'virtual' => true ]
|
2020-10-27 23:46:28 -04:00
|
|
|
),
|
2022-02-04 15:00:44 -05:00
|
|
|
'quantity' => 2,
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
[
|
2020-10-27 23:46:28 -04:00
|
|
|
'product_id' => $this->factory->product->createSimple(),
|
2022-02-04 15:00:44 -05:00
|
|
|
'quantity' => 1,
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
[
|
2020-10-27 23:46:28 -04:00
|
|
|
'product_id' => $this->factory->product->createSimple(),
|
2022-02-04 15:00:44 -05:00
|
|
|
'quantity' => 10,
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2020-10-27 23:46:28 -04:00
|
|
|
);
|
2019-04-24 13:08:29 -04:00
|
|
|
|
2019-04-24 15:05:42 -04:00
|
|
|
$code = \wc_get_coupon_code_by_id(
|
2020-10-27 23:46:28 -04:00
|
|
|
$this->factory->coupon->create(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2019-04-24 15:05:42 -04:00
|
|
|
'amount' => 45.50,
|
|
|
|
|
'discount_type' => 'fixed_cart',
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2019-04-24 15:05:42 -04:00
|
|
|
)
|
|
|
|
|
);
|
2020-10-27 23:46:28 -04:00
|
|
|
$cart = \WC()->cart;
|
2019-04-24 15:05:42 -04:00
|
|
|
$cart->apply_coupon( $code );
|
2019-04-24 13:08:29 -04:00
|
|
|
|
2019-04-24 15:05:42 -04:00
|
|
|
$query = '
|
2020-01-11 22:10:49 -05:00
|
|
|
query($needsShipping: Boolean) {
|
2019-04-24 15:05:42 -04:00
|
|
|
cart {
|
2020-01-11 22:10:49 -05:00
|
|
|
contents (where: {needsShipping: $needsShipping}) {
|
2019-04-24 15:05:42 -04:00
|
|
|
nodes {
|
|
|
|
|
key
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
2019-04-24 13:08:29 -04:00
|
|
|
|
2019-04-24 15:05:42 -04:00
|
|
|
/**
|
2019-04-24 13:08:29 -04:00
|
|
|
* Assertion One
|
|
|
|
|
*/
|
|
|
|
|
|
2020-10-27 23:46:28 -04:00
|
|
|
$response = $this->graphql( compact( 'query' ) );
|
2020-01-11 22:10:49 -05:00
|
|
|
|
2022-08-26 14:53:36 -04:00
|
|
|
$expected = [];
|
2022-02-04 15:00:44 -05:00
|
|
|
foreach ( $keys as $key ) {
|
2022-08-26 14:53:36 -04:00
|
|
|
$expected[] = $this->expectedNode( 'cart.contents.nodes', [ 'key' => $key ] );
|
2020-10-27 23:46:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2020-01-11 22:10:49 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assertion Two
|
2020-06-12 16:50:21 -04:00
|
|
|
*
|
2020-01-11 22:10:49 -05:00
|
|
|
* Tests "needsShipping" parameter.
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
$variables = [ 'needsShipping' => true ];
|
2020-10-27 23:46:28 -04:00
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
|
|
|
|
|
|
|
|
|
$this->assertQuerySuccessful(
|
|
|
|
|
$response,
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
|
|
|
|
$this->expectedNode( 'cart.contents.nodes', [ 'key' => $keys[1] ] ),
|
|
|
|
|
$this->expectedNode( 'cart.contents.nodes', [ 'key' => $keys[2] ] ),
|
|
|
|
|
]
|
2020-01-11 22:10:49 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assertion Three
|
2020-06-12 16:50:21 -04:00
|
|
|
*
|
2020-01-11 22:10:49 -05:00
|
|
|
* Tests "needsShipping" parameter reversed.
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
$variables = [ 'needsShipping' => false ];
|
2020-10-27 23:46:28 -04:00
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
|
|
|
|
|
|
|
|
|
$this->assertQuerySuccessful(
|
|
|
|
|
$response,
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
|
|
|
|
$this->expectedNode( 'cart.contents.nodes', [ 'key' => $keys[0] ] ),
|
|
|
|
|
]
|
2020-01-11 22:10:49 -05:00
|
|
|
);
|
2019-04-24 15:05:42 -04:00
|
|
|
}
|
2019-04-18 23:22:00 -04:00
|
|
|
|
2019-05-07 17:59:35 -04:00
|
|
|
public function testCartFeeQuery() {
|
2020-10-27 23:46:28 -04:00
|
|
|
$product_id = $this->factory->product->createSimple();
|
2019-05-07 17:59:35 -04:00
|
|
|
WC()->cart->add_to_cart( $product_id, 3 );
|
|
|
|
|
WC()->cart->add_fee( 'Test fee', 30.50 );
|
|
|
|
|
$fee_ids = array_keys( WC()->cart->get_fees() );
|
|
|
|
|
|
2022-02-04 15:00:44 -05:00
|
|
|
$query = '
|
2019-10-17 21:39:38 -04:00
|
|
|
query ($id: ID!) {
|
|
|
|
|
cartFee(id: $id) {
|
2019-05-07 17:59:35 -04:00
|
|
|
id
|
|
|
|
|
name
|
|
|
|
|
taxClass
|
|
|
|
|
taxable
|
|
|
|
|
amount
|
|
|
|
|
total
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assertion One
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
$variables = [ 'id' => $fee_ids[0] ];
|
2020-10-27 23:46:28 -04:00
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
2019-05-07 17:59:35 -04:00
|
|
|
|
2022-02-04 15:00:44 -05:00
|
|
|
$fee = ( \WC()->cart->get_fees() )[ $fee_ids[0] ];
|
2019-05-07 17:59:35 -04:00
|
|
|
|
2020-10-27 23:46:28 -04:00
|
|
|
$this->assertQuerySuccessful(
|
|
|
|
|
$response,
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2021-07-05 13:51:52 -04:00
|
|
|
$this->expectedField( 'cartFee.id', $fee->id ),
|
|
|
|
|
$this->expectedField( 'cartFee.name', $fee->name ),
|
|
|
|
|
$this->expectedField( 'cartFee.taxClass', $this->maybe( $fee->tax_class ) ),
|
|
|
|
|
$this->expectedField( 'cartFee.taxable', $fee->taxable ),
|
|
|
|
|
$this->expectedField( 'cartFee.amount', (float) $fee->amount ),
|
|
|
|
|
$this->expectedField( 'cartFee.total', (float) $fee->total ),
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2020-10-27 23:46:28 -04:00
|
|
|
);
|
2019-05-07 17:59:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCartToCartFeeQuery() {
|
2020-10-27 23:46:28 -04:00
|
|
|
$product_id = $this->factory->product->createSimple();
|
2019-05-07 17:59:35 -04:00
|
|
|
WC()->cart->add_to_cart( $product_id, 3 );
|
|
|
|
|
WC()->cart->add_fee( 'Test fee', 30.50 );
|
|
|
|
|
|
|
|
|
|
$query = '
|
2019-10-17 21:39:38 -04:00
|
|
|
query {
|
2019-05-07 17:59:35 -04:00
|
|
|
cart {
|
|
|
|
|
fees {
|
|
|
|
|
id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assertion One
|
|
|
|
|
*/
|
2022-02-04 15:00:44 -05:00
|
|
|
$response = $this->graphql( compact( 'query' ) );
|
2019-05-07 17:59:35 -04:00
|
|
|
|
2022-08-26 14:53:36 -04:00
|
|
|
$expected = [];
|
2022-02-04 15:00:44 -05:00
|
|
|
foreach ( \WC()->cart->get_fees() as $fee_id => $value ) {
|
2022-08-26 14:53:36 -04:00
|
|
|
$expected[] = $this->expectedNode( 'cart.fees', [ 'id' => $fee_id ] );
|
2020-10-27 23:46:28 -04:00
|
|
|
}
|
2019-05-07 17:59:35 -04:00
|
|
|
|
2020-10-27 23:46:28 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2019-05-07 17:59:35 -04:00
|
|
|
}
|
2020-01-11 19:43:08 -05:00
|
|
|
|
|
|
|
|
public function testCartItemPagination() {
|
2020-10-27 23:46:28 -04:00
|
|
|
$cart_items = $this->factory->cart->add(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2020-10-27 23:46:28 -04:00
|
|
|
'product_id' => $this->factory->product->createSimple(),
|
|
|
|
|
'quantity' => 2,
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
[
|
2020-10-27 23:46:28 -04:00
|
|
|
'product_id' => $this->factory->product->createSimple(),
|
|
|
|
|
'quantity' => 1,
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
[
|
2020-10-27 23:46:28 -04:00
|
|
|
'product_id' => $this->factory->product->createSimple(),
|
|
|
|
|
'quantity' => 1,
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
[
|
2020-10-27 23:46:28 -04:00
|
|
|
'product_id' => $this->factory->product->createSimple(),
|
|
|
|
|
'quantity' => 1,
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
[
|
2020-10-27 23:46:28 -04:00
|
|
|
'product_id' => $this->factory->product->createSimple(),
|
|
|
|
|
'quantity' => 1,
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2020-01-11 19:43:08 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
|
query ($first: Int, $last: Int, $before: String, $after: String) {
|
|
|
|
|
cart {
|
|
|
|
|
contents(first: $first, last: $last, before: $before, after: $after) {
|
|
|
|
|
itemCount
|
|
|
|
|
productCount
|
2021-06-12 22:06:44 -04:00
|
|
|
pageInfo {
|
|
|
|
|
hasNextPage
|
|
|
|
|
hasPreviousPage
|
|
|
|
|
}
|
2020-01-11 19:43:08 -05:00
|
|
|
edges {
|
|
|
|
|
cursor
|
|
|
|
|
node {
|
|
|
|
|
key
|
|
|
|
|
}
|
2020-03-24 20:55:13 -04:00
|
|
|
}
|
2020-01-11 19:43:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assertion One
|
2020-06-12 16:50:21 -04:00
|
|
|
*
|
2020-01-11 19:43:08 -05:00
|
|
|
* Tests "first" parameter.
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
$variables = [
|
2022-02-04 15:00:44 -05:00
|
|
|
'first' => 2,
|
|
|
|
|
'after' => '',
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2020-10-27 23:46:28 -04:00
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
2022-08-26 14:53:36 -04:00
|
|
|
$expected = [
|
2021-07-05 13:51:52 -04:00
|
|
|
$this->expectedField( 'cart.contents.itemCount', 6 ),
|
|
|
|
|
$this->expectedField( 'cart.contents.productCount', 5 ),
|
|
|
|
|
$this->expectedField( 'cart.contents.pageInfo.hasNextPage', true ),
|
|
|
|
|
$this->expectedField( 'cart.contents.pageInfo.hasPreviousPage', false ),
|
2021-06-12 22:06:44 -04:00
|
|
|
$this->expectedEdge(
|
|
|
|
|
'cart.contents.edges',
|
2022-08-26 14:53:36 -04:00
|
|
|
[ $this->expectedField( 'key', $cart_items[0] ) ],
|
2021-06-12 22:06:44 -04:00
|
|
|
0
|
2020-01-11 19:43:08 -05:00
|
|
|
),
|
2021-06-12 22:06:44 -04:00
|
|
|
$this->expectedEdge(
|
|
|
|
|
'cart.contents.edges',
|
2022-08-26 14:53:36 -04:00
|
|
|
[ $this->expectedField( 'key', $cart_items[1] ) ],
|
2021-06-12 22:06:44 -04:00
|
|
|
1
|
2022-02-04 15:00:44 -05:00
|
|
|
),
|
2022-08-26 14:53:36 -04:00
|
|
|
$this->expectedField( 'cart.contents.edges.0.cursor', $this->key_to_cursor( $cart_items[0] ) ),
|
|
|
|
|
$this->expectedField( 'cart.contents.edges.1.cursor', $this->key_to_cursor( $cart_items[1] ) ),
|
|
|
|
|
];
|
2020-01-11 19:43:08 -05:00
|
|
|
|
2020-10-27 23:46:28 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2020-01-11 19:43:08 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assertion Two
|
2020-06-12 16:50:21 -04:00
|
|
|
*
|
2020-01-11 19:43:08 -05:00
|
|
|
* Tests "after" parameter.
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
$variables = [
|
2020-03-24 20:55:13 -04:00
|
|
|
'first' => 2,
|
2022-02-04 15:00:44 -05:00
|
|
|
'after' => $this->key_to_cursor( $cart_items[1] ),
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2020-10-27 23:46:28 -04:00
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
2022-08-26 14:53:36 -04:00
|
|
|
$expected = [
|
2021-07-05 13:51:52 -04:00
|
|
|
$this->expectedField( 'cart.contents.pageInfo.hasNextPage', true ),
|
|
|
|
|
$this->expectedField( 'cart.contents.pageInfo.hasPreviousPage', true ),
|
|
|
|
|
$this->expectedField( 'cart.contents.edges.0.cursor', $this->key_to_cursor( $cart_items[2] ) ),
|
|
|
|
|
$this->expectedField( 'cart.contents.edges.1.cursor', $this->key_to_cursor( $cart_items[3] ) ),
|
2021-06-12 22:06:44 -04:00
|
|
|
$this->expectedEdge(
|
|
|
|
|
'cart.contents.edges',
|
2022-08-26 14:53:36 -04:00
|
|
|
[ $this->expectedField( 'key', $cart_items[2] ) ],
|
2021-06-12 22:06:44 -04:00
|
|
|
0
|
|
|
|
|
),
|
|
|
|
|
$this->expectedEdge(
|
|
|
|
|
'cart.contents.edges',
|
2022-08-26 14:53:36 -04:00
|
|
|
[ $this->expectedField( 'key', $cart_items[3] ) ],
|
2021-06-12 22:06:44 -04:00
|
|
|
1
|
2022-02-04 15:00:44 -05:00
|
|
|
),
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2020-01-11 19:43:08 -05:00
|
|
|
|
2021-06-12 22:06:44 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
|
|
|
|
|
2020-01-11 19:43:08 -05:00
|
|
|
/**
|
|
|
|
|
* Assertion Three
|
2020-06-12 16:50:21 -04:00
|
|
|
*
|
2020-01-11 19:43:08 -05:00
|
|
|
* Tests "last" parameter.
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
$variables = [ 'last' => 2 ];
|
2020-10-27 23:46:28 -04:00
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
2022-08-26 14:53:36 -04:00
|
|
|
$expected = [
|
2021-07-05 13:51:52 -04:00
|
|
|
$this->expectedField( 'cart.contents.pageInfo.hasNextPage', false ),
|
|
|
|
|
$this->expectedField( 'cart.contents.pageInfo.hasPreviousPage', true ),
|
2021-06-12 22:06:44 -04:00
|
|
|
$this->expectedEdge(
|
|
|
|
|
'cart.contents.edges',
|
2022-08-26 14:53:36 -04:00
|
|
|
[ $this->expectedField( 'key', $cart_items[3] ) ],
|
2021-06-12 22:06:44 -04:00
|
|
|
0
|
|
|
|
|
),
|
|
|
|
|
$this->expectedEdge(
|
|
|
|
|
'cart.contents.edges',
|
2022-08-26 14:53:36 -04:00
|
|
|
[ $this->expectedField( 'key', $cart_items[4] ) ],
|
2021-06-12 22:06:44 -04:00
|
|
|
1
|
2022-02-04 15:00:44 -05:00
|
|
|
),
|
2022-08-26 14:53:36 -04:00
|
|
|
$this->expectedField( 'cart.contents.edges.0.cursor', $this->key_to_cursor( $cart_items[3] ) ),
|
|
|
|
|
$this->expectedField( 'cart.contents.edges.1.cursor', $this->key_to_cursor( $cart_items[4] ) ),
|
|
|
|
|
];
|
2020-01-11 19:43:08 -05:00
|
|
|
|
2021-06-12 22:06:44 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
|
|
|
|
|
2020-01-11 19:43:08 -05:00
|
|
|
/**
|
|
|
|
|
* Assertion Four
|
2020-06-12 16:50:21 -04:00
|
|
|
*
|
2020-01-11 19:43:08 -05:00
|
|
|
* Tests "before" parameter.
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
$variables = [
|
2022-02-04 15:00:44 -05:00
|
|
|
'last' => 4,
|
2021-06-12 22:06:44 -04:00
|
|
|
'before' => $this->key_to_cursor( $cart_items[3] ),
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2020-10-27 23:46:28 -04:00
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
2022-08-26 14:53:36 -04:00
|
|
|
$expected = [
|
2021-07-05 13:51:52 -04:00
|
|
|
$this->expectedField( 'cart.contents.pageInfo.hasNextPage', true ),
|
|
|
|
|
$this->expectedField( 'cart.contents.pageInfo.hasPreviousPage', false ),
|
|
|
|
|
$this->expectedField( 'cart.contents.edges.0.cursor', $this->key_to_cursor( $cart_items[0] ) ),
|
|
|
|
|
$this->expectedField( 'cart.contents.edges.1.cursor', $this->key_to_cursor( $cart_items[1] ) ),
|
|
|
|
|
$this->expectedField( 'cart.contents.edges.2.cursor', $this->key_to_cursor( $cart_items[2] ) ),
|
2021-06-12 22:06:44 -04:00
|
|
|
$this->expectedEdge(
|
|
|
|
|
'cart.contents.edges',
|
2022-08-26 14:53:36 -04:00
|
|
|
[ $this->expectedField( 'key', $cart_items[0] ) ],
|
2021-06-12 22:06:44 -04:00
|
|
|
0
|
|
|
|
|
),
|
|
|
|
|
$this->expectedEdge(
|
|
|
|
|
'cart.contents.edges',
|
2022-08-26 14:53:36 -04:00
|
|
|
[ $this->expectedField( 'key', $cart_items[1] ) ],
|
2021-06-12 22:06:44 -04:00
|
|
|
1
|
|
|
|
|
),
|
|
|
|
|
$this->expectedEdge(
|
|
|
|
|
'cart.contents.edges',
|
2022-08-26 14:53:36 -04:00
|
|
|
[ $this->expectedField( 'key', $cart_items[2] ) ],
|
2021-06-12 22:06:44 -04:00
|
|
|
2
|
2022-02-04 15:00:44 -05:00
|
|
|
),
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2021-06-12 22:06:44 -04:00
|
|
|
|
|
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2020-01-11 19:43:08 -05:00
|
|
|
}
|
2026-03-18 16:59:08 -04:00
|
|
|
|
|
|
|
|
public function testCartQueryWithFees() {
|
|
|
|
|
$product_id = $this->factory->product->createSimple();
|
|
|
|
|
WC()->cart->add_to_cart( $product_id, 3 );
|
|
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
|
query($fees: [FeeInput]) {
|
|
|
|
|
cart(fees: $fees) {
|
|
|
|
|
total
|
|
|
|
|
feeTotal
|
|
|
|
|
feeTax
|
|
|
|
|
fees {
|
|
|
|
|
name
|
|
|
|
|
amount
|
|
|
|
|
total
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
$variables = [
|
|
|
|
|
'fees' => [
|
|
|
|
|
[
|
|
|
|
|
'name' => 'Test Fee',
|
|
|
|
|
'amount' => 10.50,
|
|
|
|
|
'taxable' => false,
|
|
|
|
|
'taxClass' => 'STANDARD',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
|
|
|
|
|
|
|
|
|
$this->assertQuerySuccessful(
|
|
|
|
|
$response,
|
|
|
|
|
[
|
|
|
|
|
$this->expectedField( 'cart.total', self::NOT_FALSY ),
|
|
|
|
|
$this->expectedField( 'cart.feeTotal', self::NOT_FALSY ),
|
|
|
|
|
$this->expectedField( 'cart.fees.#.name', 'Test Fee' ),
|
|
|
|
|
$this->expectedField( 'cart.fees.#.amount', floatval( 10.50 ) ),
|
|
|
|
|
$this->expectedField( 'cart.fees.#.total', floatval( 10.50 ) ),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-26 20:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test cart taxLines field returns itemized tax data.
|
|
|
|
|
*/
|
|
|
|
|
public function testCartTaxLines() {
|
|
|
|
|
// Enable taxes and set itemized display.
|
|
|
|
|
update_option( 'woocommerce_calc_taxes', 'yes' );
|
|
|
|
|
update_option( 'woocommerce_tax_total_display', 'itemized' );
|
|
|
|
|
|
|
|
|
|
// Create a tax rate.
|
|
|
|
|
$tax_rate_id = $this->factory->tax_rate->create(
|
|
|
|
|
[
|
|
|
|
|
'country' => '',
|
|
|
|
|
'rate' => '10.0000',
|
|
|
|
|
'name' => 'Test Tax',
|
|
|
|
|
'priority' => 1,
|
|
|
|
|
'compound' => 0,
|
|
|
|
|
'shipping' => 1,
|
|
|
|
|
'class' => '',
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Create a product and add to cart.
|
|
|
|
|
$product_id = $this->factory->product->createSimple( [ 'regular_price' => '100', 'virtual' => true ] );
|
|
|
|
|
WC()->cart->add_to_cart( $product_id );
|
|
|
|
|
WC()->cart->calculate_totals();
|
|
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
|
query {
|
|
|
|
|
cart {
|
|
|
|
|
taxLines {
|
|
|
|
|
name
|
|
|
|
|
price
|
|
|
|
|
rate
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
$response = $this->graphql( compact( 'query' ) );
|
|
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
|
$this->expectedNode(
|
|
|
|
|
'cart.taxLines',
|
|
|
|
|
[
|
|
|
|
|
$this->expectedField( 'name', 'Test Tax' ),
|
|
|
|
|
$this->expectedField( 'price', '$10.00' ),
|
|
|
|
|
$this->expectedField( 'rate', '10%' ),
|
|
|
|
|
]
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test cart taxLines returns empty when display is not itemized.
|
|
|
|
|
*/
|
|
|
|
|
public function testCartTaxLinesEmptyWhenNotItemized() {
|
|
|
|
|
update_option( 'woocommerce_calc_taxes', 'yes' );
|
|
|
|
|
update_option( 'woocommerce_tax_total_display', 'single' );
|
|
|
|
|
|
|
|
|
|
$tax_rate_id = $this->factory->tax_rate->create(
|
|
|
|
|
[
|
|
|
|
|
'country' => '',
|
|
|
|
|
'rate' => '10.0000',
|
|
|
|
|
'name' => 'Test Tax',
|
|
|
|
|
'priority' => 1,
|
|
|
|
|
'compound' => 0,
|
|
|
|
|
'shipping' => 1,
|
|
|
|
|
'class' => '',
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$product_id = $this->factory->product->createSimple( [ 'regular_price' => '100', 'virtual' => true ] );
|
|
|
|
|
WC()->cart->add_to_cart( $product_id );
|
|
|
|
|
WC()->cart->calculate_totals();
|
|
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
|
query {
|
|
|
|
|
cart {
|
|
|
|
|
taxLines {
|
|
|
|
|
name
|
|
|
|
|
price
|
|
|
|
|
rate
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
$response = $this->graphql( compact( 'query' ) );
|
|
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
|
$this->expectedField( 'cart.taxLines', static::IS_FALSY ),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test shipping rate cost includes tax when woocommerce_tax_display_cart is incl.
|
|
|
|
|
*/
|
|
|
|
|
public function testShippingRateCostIncludesTax() {
|
|
|
|
|
// Enable taxes and set display to include tax.
|
|
|
|
|
update_option( 'woocommerce_calc_taxes', 'yes' );
|
|
|
|
|
update_option( 'woocommerce_tax_display_cart', 'incl' );
|
|
|
|
|
|
|
|
|
|
// Create a tax rate that applies to shipping.
|
|
|
|
|
$this->factory->tax_rate->create(
|
|
|
|
|
[
|
|
|
|
|
'country' => '',
|
|
|
|
|
'rate' => '10.0000',
|
|
|
|
|
'name' => 'Shipping Tax',
|
|
|
|
|
'priority' => 1,
|
|
|
|
|
'compound' => 0,
|
|
|
|
|
'shipping' => 1,
|
|
|
|
|
'class' => '',
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Create a physical product and add to cart.
|
|
|
|
|
$product_id = $this->factory->product->createSimple( [ 'regular_price' => '50' ] );
|
|
|
|
|
WC()->cart->add_to_cart( $product_id );
|
|
|
|
|
|
|
|
|
|
// Set shipping address so rates are calculated.
|
|
|
|
|
WC()->customer->set_shipping_country( 'GB' );
|
|
|
|
|
WC()->customer->set_shipping_postcode( 'CB23 1AB' );
|
|
|
|
|
WC()->cart->calculate_totals();
|
|
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
|
query {
|
|
|
|
|
cart {
|
|
|
|
|
availableShippingMethods {
|
|
|
|
|
rates {
|
|
|
|
|
id
|
|
|
|
|
label
|
|
|
|
|
cost
|
|
|
|
|
subtotal
|
|
|
|
|
taxTotal
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
$response = $this->graphql( compact( 'query' ) );
|
|
|
|
|
|
|
|
|
|
// Flat rate is $10, tax is 10% = $1. Cost should be $11 (incl tax), subtotal $10.
|
|
|
|
|
$expected = [
|
2026-03-30 21:42:00 -04:00
|
|
|
$this->expectedObject(
|
|
|
|
|
'cart.availableShippingMethods.0.rates.#',
|
|
|
|
|
[
|
|
|
|
|
$this->expectedField( 'cost', '$11.00' ),
|
|
|
|
|
$this->expectedField( 'subtotal', '$10.00' ),
|
|
|
|
|
$this->expectedField( 'taxTotal', '$1.00' ),
|
|
|
|
|
]
|
|
|
|
|
),
|
2026-03-26 20:09:53 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test shipping rate cost excludes tax when woocommerce_tax_display_cart is excl.
|
|
|
|
|
*/
|
|
|
|
|
public function testShippingRateCostExcludesTax() {
|
|
|
|
|
// Enable taxes and set display to exclude tax.
|
|
|
|
|
update_option( 'woocommerce_calc_taxes', 'yes' );
|
|
|
|
|
update_option( 'woocommerce_tax_display_cart', 'excl' );
|
|
|
|
|
|
|
|
|
|
$this->factory->tax_rate->create(
|
|
|
|
|
[
|
|
|
|
|
'country' => '',
|
|
|
|
|
'rate' => '10.0000',
|
|
|
|
|
'name' => 'Shipping Tax',
|
|
|
|
|
'priority' => 1,
|
|
|
|
|
'compound' => 0,
|
|
|
|
|
'shipping' => 1,
|
|
|
|
|
'class' => '',
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$product_id = $this->factory->product->createSimple( [ 'regular_price' => '50' ] );
|
|
|
|
|
WC()->cart->add_to_cart( $product_id );
|
|
|
|
|
|
|
|
|
|
WC()->customer->set_shipping_country( 'GB' );
|
|
|
|
|
WC()->customer->set_shipping_postcode( 'CB23 1AB' );
|
|
|
|
|
WC()->cart->calculate_totals();
|
|
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
|
query {
|
|
|
|
|
cart {
|
|
|
|
|
availableShippingMethods {
|
|
|
|
|
rates {
|
|
|
|
|
id
|
|
|
|
|
label
|
|
|
|
|
cost
|
|
|
|
|
subtotal
|
|
|
|
|
taxTotal
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
$response = $this->graphql( compact( 'query' ) );
|
|
|
|
|
|
|
|
|
|
// Cost and subtotal should both be $10 (excl tax), taxTotal still $1.
|
|
|
|
|
$expected = [
|
|
|
|
|
$this->expectedObject(
|
2026-03-30 21:42:00 -04:00
|
|
|
'cart.availableShippingMethods.0.rates.#',
|
2026-03-26 20:09:53 -04:00
|
|
|
[
|
2026-03-30 21:42:00 -04:00
|
|
|
$this->expectedField( 'cost', '$10.00' ),
|
|
|
|
|
$this->expectedField( 'subtotal', '$10.00' ),
|
|
|
|
|
$this->expectedField( 'taxTotal', '$1.00' ),
|
2026-03-26 20:09:53 -04:00
|
|
|
]
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2026-03-18 16:59:08 -04:00
|
|
|
}
|
2020-06-12 16:50:21 -04:00
|
|
|
}
|