• Resolved gleonp

    (@gleonp)


    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 to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • 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

    (@gleonp)

    Hi @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.

    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

    (@gleonp)

    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.

    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

    (@gleonp)

    Hi, you really helped me a lot. I will look over the removal session after each order.

    Plugin Support Hannah S.L.

    (@fernashes)

    Automattic Happiness Engineer

    Thanks, @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.

    (@fernashes)

    Automattic Happiness Engineer

    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.