Description
Describe the bug
In functions.php i got simple cart discount function using 'woocommerce_before_calculate_totals' hook. It applies negative fee to cart when specific criteria are met within cart.
Querying cart returns those fees when front end cart meets those critiria, but when mutating - ex. adding item which should make such fee occur - doesn't (mutations returns fees: null). Subsequent querying cart properly fetches fees property.
To Reproduce
Steps to reproduce the behavior:
- Add discount function to theme functions.php file. Simplified example -> 20% discount when 4 or more products in cart:
function action_woocommerce_before_calculate_totals( $cart ) {
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$books_quantity = 0;
$total_amount = 0;
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$total_amount += $current_price;
$books_quantity++;
}
if ($books_quantity >= 4) {
$discount = -0.2 * $total_amount;
$cart->add_fee( 'Discount', $discount);
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
- Fill cart with items so fee will be applied
- Query in Graphiql like:
query CartQ {
cart {
total
feeTotal
fees {
amount
name
}
contents {
productCount
nodes {
key
}
}
}
}
- mutate cart with either addToCart
mutation Add {
addToCart(input:
{productId: 1165090,
quantity: 1,
clientMutationId: ""})
{
cart {
total
feeTotal
fees {
amount
name
}
contents {
productCount
nodes {
key
}
}
}
}
}
or removeItemsFromCart
mutation Remove {
removeItemsFromCart(input: {keys: "bea28a98ffb4e24bf9e154e4d4d3a8bf", clientMutationId: ""}) {
cart {
total
feeTotal
fees {
amount
name
}
contents {
productCount
nodes {
key
}
}
}
}
}
Expected behavior
Mutation response fees array as query.
Additional Info
I don't think it has anything to do with used woocommerce hook -> meaning that the fee calculation happens after response of mutation. Mostly because where I have more than enough products in cart with calculated fee and removing item while still maintaining minimal productCount the response still nulls fees array.
Desktop (please complete the following information):
OS: Windows 11
Browser MS Edge
Version 92
Plugin Versions
**WooGraphQL Version: 0.10.3
**WPGraphQL Version: 1.6.3
**WordPress Version: 5.8
**WooCommerce Version: 5.5.2