Skip to content

addFee mutation doesn't add the extra fee to the cart summary #477

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

Open
aresrioja10 opened this issue Apr 18, 2021 · 4 comments · May be fixed by #929
Open

addFee mutation doesn't add the extra fee to the cart summary #477

aresrioja10 opened this issue Apr 18, 2021 · 4 comments · May be fixed by #929
Labels
bug Something isn't working
Milestone

Comments

@aresrioja10
Copy link

aresrioja10 commented Apr 18, 2021

Describe the bug
I'm trying to use the addFee mutation in order to add extra fees in the checkout page when a user select some optional services. The problem is that it doesn't add the fee to the cart.

To Reproduce
Steps to reproduce the behavior:

  1. Execute the following mutation (i'm using the GraphQL IDE):
mutation addFee {
  addFee(input: {name: "Extra service", amount: 1.5}) {
    clientMutationId
    cart {
      fees {
        name
        amount
      }
    }
  }
}

Expected behavior
Although the mutation result returns this:

"data": {
    "addFee": {
      "clientMutationId": null,
      "cart": {
        "fees": [
          {
            "name": "Extra service",
            "amount": 1.5
          }
        ]
      }
    }
  }

if I visit the cart page I can't see the fees being applied:

Captura de pantalla 2021-04-18 a las 12 37 53

Desktop (please complete the following information):

  • OS: macOS
  • Browser Chrome

Plugin Versions

  • WooGraphQL Version: 0.8.1
  • WPGraphQL Version: 1.3.5
  • WordPress Version: 5.7.1
  • WooCommerce Version: 5.2.2
@aresrioja10
Copy link
Author

@kidunot89 Could you check this please? I think it's a quick fix but I don't see the error:

The plugin code seems to be ok:

return function( $input, AppContext $context, ResolveInfo $info ) {
    Cart_Mutation::check_session_token();
    
    if ( ! current_user_can( 'edit_shop_orders' ) ) {
    throw new UserError( __( 'You do not have the appropriate capabilities to perform this action.', 'wp-graphql-woocommerce' ) );
    }
    
    if ( empty( $input['name'] ) ) {
    throw new UserError( __( 'No name provided for fee.', 'wp-graphql-woocommerce' ) );
    }
    
    if ( ! isset( $input['amount'] ) ) {
    throw new UserError( __( 'No amount set for the fee.', 'wp-graphql-woocommerce' ) );
    }
    
    // Get cart fee args.
    $cart_fee_args = Cart_Mutation::prepare_cart_fee( $input, $context, $info );
    
    // Add cart fee.
    \WC()->cart->add_fee( ...$cart_fee_args );
    
    // Return payload.
    return array( 'id' => \sanitize_title( $input['name'] ) );
};

@aresrioja10
Copy link
Author

I've detected that this code works:

function woo_add_cart_fee() {
    global $woocommerce;
    $woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

so it seems that the problem is that $woocommerce->cart->add_fee only works inside of woocommerce_cart_calculate_fees action.

@aresrioja10
Copy link
Author

I've solved the issue adding the following code to the mutation:

...
// Add cart fee.
\WC()->cart->add_fee( ...$cart_fee_args );

\WC()->session->set('cart_fee_args', $cart_fee_args); // <----------------

// Return payload.
return array( 'id' => \sanitize_title( $input['name'] ) );

and then adding the following action in my wp theme:

add_action('woocommerce_cart_calculate_fees', function ($args) {
    $cart_fee_args = \WC()->session->get('cart_fee_args');

    if (sizeof($cart_fee_args) > 1 && is_float($cart_fee_args[1])) {
        \WC()->cart->add_fee(...$cart_fee_args);
    }
});

@kidunot89 @jasonbahl @jacobarriola

@kidunot89 kidunot89 added the bug Something isn't working label Feb 27, 2025
@kidunot89 kidunot89 added this to the v1.0.0 milestone Feb 27, 2025
@kidunot89 kidunot89 modified the milestones: v1.0.0, v0.21.2, v0.21.3 Mar 11, 2025
@kidunot89
Copy link
Collaborator

kidunot89 commented Apr 1, 2025

@aresrioja10 The issue is due to fee not being persisted. The addFee is effectively useless and will instead be deprecation and replaced by a new fees field on the CheckoutInput and the feeLines field on the CreateOrderInput so fees can be applied during order creation. In the meantime you can utilize the woocommerce_cart_calculate_fees hook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants