Fredrik
Fredrik

Reputation: 389

Update totals after applying or removing a coupon programmatically In WooCommerce

I have created a checkbox (It does not look like a checkbox anymore) that apply/remove a coupong on change. This works good. But the total does not update on the apply, the page has to be refreshed. I have build this function with some cut and paste from other functions, it was once a radio field, and it might not be the best practise. The coupong ads a discount for 500 SEK.

But how to do I recalculate the total after the coupong is applied?

As you can see in the end, I have tried WC()->cart->calculate_totals();.

This is the site and checkout: https://www.klubbtryck.se/nif/kassa/

This is my code:

    // Add a custom checkout field
    add_action( 'woocommerce_review_order_after_shipping', 'checkout_shipping_form_delivery_addition_nifny', 20 );

function checkout_shipping_form_delivery_addition_nifny(){
    $domain = 'wocommerce';

    if (  WC()->session->get( 'chosen_shipping_methods' )[0] == 'local_pickup:3' ) :

        echo '<tr class="delivery-radio"><th>' . __('Gift Card', $domain) . '</th><td>';

        $chosen = WC()->session->get('chosen_delivery');
        $chosen = empty($chosen) ? WC()->checkout->get_value('delivery') : $chosen;
        $chosen = empty($chosen) ? 0 : $chosen;
        if( $chosen == 1){ $chosen = true; } else { $chosen = false; }

        // Add a custom checkbox field
        woocommerce_form_field( 'radio_delivery', array(
            'type' => 'checkbox',
            'label' => '<label for="radio_delivery" class="checkbox-label"><span class="presentkortbesk">I have a gift card</span><span class="priset">-500kr</span></label>',
            'class' => array( 'form-row-wide' ),
            'required' => false,
            //'default' => false,
        ), $chosen );

        echo '</td></tr>';

    endif;
}

// jQuery - Ajax script
add_action( 'wp_footer', 'checkout_delivery_script_nifny' );
function checkout_delivery_script_nifny() {
    // Only checkout page
    if ( ! is_checkout() ) return;
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined')
            return false;

        $('form.checkout').on('change', 'input[name=radio_delivery]', function(e){
            e.preventDefault();
            var d = $(this).prop('checked') === true ? 1 : 0;
            //var d = $(this).val();
            //alert('value: '+d);
            $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action': 'delivery',
                    'delivery': d,
                },
                success: function (result) {
                    $('body').trigger('update_checkout');
                    //console.log(result); // just for testing | TO BE REMOVED
                },
                error: function(error){
                    //console.log(error); // just for testing | TO BE REMOVED
                }
            });
        });
    });
    </script>
    <?php

}

// Get Ajax request and saving to WC session
add_action( 'wp_ajax_delivery', 'wc_get_delivery_ajax_data_nifny' );
add_action( 'wp_ajax_nopriv_delivery', 'wc_get_delivery_ajax_data_nifny' );
function wc_get_delivery_ajax_data_nifny() {
    if ( isset($_POST['delivery']) ){
        WC()->session->set('chosen_delivery', sanitize_key( $_POST['delivery'] ) );
        echo json_encode( $delivery ); // Return the value to jQuery
    }
    die();
}

// Add a custom dynamic delivery fee
add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee_nifny', 20, 1 );
function add_packaging_fee_nifny( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Only for targeted shipping method
    if (  WC()->session->get( 'chosen_shipping_methods' )[0] != 'local_pickup:3' )
        return;

    if( WC()->session->get( 'chosen_delivery' ) == 1 ){
        if (!in_array('nynashamn2020', WC()->cart->get_applied_coupons())) {
                WC()->cart->apply_coupon('card2020');
                //WC()->cart->calculate_totals();
            }
    } else {
        if (in_array('nynashamn2020', WC()->cart->get_applied_coupons())) {
            WC()->cart->remove_coupon('card2020');
        }
    }
}

Upvotes: 1

Views: 2365

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253969

You should replace woocommerce_cart_calculate_fees hook that is only made for Fees with similar woocommerce_before_calculate_totals more appropriated hook as follows:

// Add a custom dynamic delivery fee
add_action( 'woocommerce_before_calculate_totals', 'add_packaging_fee_nifny' );
function add_packaging_fee_nifny( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Only for targeted shipping method
    if (  WC()->session->get( 'chosen_shipping_methods' )[0] != 'local_pickup:3' )
        return;

    if( WC()->session->get( 'chosen_delivery' ) == 1 ){
        if (!in_array('nynashamn2020', WC()->cart->get_applied_coupons())) {
                WC()->cart->apply_coupon('card2020');
                //WC()->cart->calculate_totals();
            }
    } else {
        if (in_array('nynashamn2020', WC()->cart->get_applied_coupons())) {
            WC()->cart->remove_coupon('card2020');
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme). It should better works.

Upvotes: 1

Related Questions