Title: add_fee with variable value
Last modified: November 9, 2017

---

# add_fee with variable value

 *  Resolved [gleonp](https://wordpress.org/support/users/gleonp/)
 * (@gleonp)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add_fee-with-variable-value/)
 * Hi, I have this code:
 * function woo_add_cart_fee() {
    $name = ‘Valor de envío’; $fee = $_GET[“costoenvio”];
   WC()->cart->add_fee( $name, $fee, false ); } add_action( ‘woocommerce_cart_calculate_fees’,‘
   woo_add_cart_fee’ );
 * The value take from input text in the checkout page, in review order the value
   is add to total, but when I click in pay this value is lose. If I use a Fixed
   value in $fee work, but not with a variable value.
 * Somebody have any idea?
 * Thanks!
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fadd_fee-with-variable-value%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [superkot](https://wordpress.org/support/users/superkot/)
 * (@superkot)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add_fee-with-variable-value/#post-9667609)
 * Try
 *     ```
       function woo_add_cart_fee() {
       $name = 'Valor de envío';
       if ( isset($_GET["costoenvio"]) ) {
       $fee = $_GET["costoenvio"];
       WC()->cart->add_fee( $name, $fee, false );
       }
       }
   
       add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
       ```
   
 *  Thread Starter [gleonp](https://wordpress.org/support/users/gleonp/)
 * (@gleonp)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add_fee-with-variable-value/#post-9667802)
 * Hi [@superkot](https://wordpress.org/support/users/superkot/), thanks for your
   response but dont work. The same thing keeps happening, in the review order it
   is reflected but at the moment of paying it does not add that value.
 * I found this:
 * The fees API is not persistent. You need to add your fee everytime the woocommerce_cart_calculate_fees
   hook runs or it will be lost.
 * But I dont apply this.
 *  [superkot](https://wordpress.org/support/users/superkot/)
 * (@superkot)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add_fee-with-variable-value/#post-9667976)
 * I tried it at my test environment and it worked.
    You need to store values in
   sessions then:
 *     ```
       function woo_add_cart_fee() {
   
       $name = 'Valor de envío';
       session_start();
   
       if ( isset($_GET["costoenvio"]) ) {
       $_SESSION['costo'] = $_GET["costoenvio"];
       }
   
       if ( isset($_SESSION['costo']) ) {
       $fee = $_SESSION['costo'];
       WC()->cart->add_fee( $name, $fee, false );
       }
   
       }
   
       add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' )
       ```
   
 *  Thread Starter [gleonp](https://wordpress.org/support/users/gleonp/)
 * (@gleonp)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add_fee-with-variable-value/#post-9668230)
 * Hi, now its work fine. Thanks so so much!!!
 * I have a question, that session in which moment it expires? I made two purchases
   and loaded the same value unless it indicated a new one.
 *  [superkot](https://wordpress.org/support/users/superkot/)
 * (@superkot)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add_fee-with-variable-value/#post-9668878)
 * Default session expires after 24 minutes. It is defined within php.ini file on
   your server. Session is individual, it works only for the same device & browser.
   So if you expect scenario where your customers could be making repeating orders
   from the same device within 24 minutes, you need to clean the session after order
   is placed.
 *  Thread Starter [gleonp](https://wordpress.org/support/users/gleonp/)
 * (@gleonp)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add_fee-with-variable-value/#post-9668957)
 * Hi, you really helped me a lot. I will look over the removal session after each
   order.
 *  Plugin Support [Hannah S.L.](https://wordpress.org/support/users/fernashes/)
 * (@fernashes)
 * Automattic Happiness Engineer
 * [8 years, 6 months ago](https://wordpress.org/support/topic/add_fee-with-variable-value/#post-9693266)
 * Thanks, [@superkot](https://wordpress.org/support/users/superkot/)!
 * I’m going to mark this as resolved – if you have any further questions, you can
   start a new thread.
 *  Plugin Support [Hannah S.L.](https://wordpress.org/support/users/fernashes/)
 * (@fernashes)
 * Automattic Happiness Engineer
 * [8 years, 6 months ago](https://wordpress.org/support/topic/add_fee-with-variable-value/#post-9716356)
 * Whoops, forgot to mark this as resolved!

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘add_fee with variable value’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [add_fee](https://wordpress.org/support/topic-tag/add_fee/)

 * 8 replies
 * 3 participants
 * Last reply from: [Hannah S.L.](https://wordpress.org/support/users/fernashes/)
 * Last activity: [8 years, 6 months ago](https://wordpress.org/support/topic/add_fee-with-variable-value/#post-9716356)
 * Status: resolved