Skip to content

Cart mutations doesn't return fees #542

Open
@Robak08

Description

@Robak08

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:

  1. 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 );
  1. Fill cart with items so fee will be applied
  2. Query in Graphiql like:
query CartQ {
  cart {
    total
    feeTotal
    fees {
      amount
      name
    }
    contents {
      productCount
      nodes {
        key
      }
    }
  }
}

woograph_query_cart

  1. mutate cart with either addToCart
mutation Add {
  addToCart(input: 
    {productId: 1165090, 
      quantity: 1, 
      clientMutationId: ""}) 
  {
    cart {
      total
      feeTotal
      fees {
        amount
        name
      }
      contents {
        productCount
        nodes {
          key
        }
      }
    }
  }
}

woograph_mutation_add

or removeItemsFromCart

mutation Remove {
  removeItemsFromCart(input: {keys: "bea28a98ffb4e24bf9e154e4d4d3a8bf", clientMutationId: ""}) {
    cart {
      total
      feeTotal
      fees {
        amount
        name
      }
      contents {
        productCount
        nodes {
          key
        }
      }
    }
  }
}

woograph_mutation_remove

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfailing mutationIssue concerns a failing/broken mutation

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions