Steve
Steve

Reputation: 33

Woocommerce How to get Shipping address and Billing before checkout to calculate the shipping fees then add to the total amount

I just need very simple hook that suitable for this kind of feature whichI saw many times.

I want to get the billing data and shipping data of woocommerce once the customer fill or change it to calculate the shipping fees then add to the total amount before checkout becuae CA shipping fees different from NY for example

I use woocommerce_checkout_order_review to review the order before checkout so I need something similar to work on input billing data fields and shipping fields.

function getcartitems()
{

    echo WC()->customer->get_billing_address_1();
    echo WC()->customer->get_billing_city();
    echo WC()->customer->get_billing_state();

    ?>
    <script>
        alert("Cart");
    </script>
<?php

    global $woocommerce;
    $items = WC()->cart->get_cart();
    
    $items1 = json_encode($items, true);

    $items1 = json_decode($items1, true);
    echo count($items1);

    file_put_contents(ABSPATH . 'debug.txt', print_r($items, true));
    //file_put_contents(ABSPATH . 'debug1.txt', print_r($order_data, true));
}
add_action('woocommerce_checkout_order_review', 'getcartitems', 12);

wc

Upvotes: 0

Views: 1483

Answers (1)

DavidBirkin
DavidBirkin

Reputation: 63

One option might be to use an ajax call from the checkout page like so;

The hook that you should use for this is if you want to calculate shipping on the checkout pahe would be; woocommerce_before_checkout_billing_form

Source: https://www.businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/

// This should be added to your primary plugin file.
// Sometimes called functions.php

add_action( 'wp_ajax_check_country_fees', 'check_country_fees' ); 

Action Name (second parameter above) is what is passed below.

let data = {
    'action': 'check_country_fees',
    'country': document.getElementById('country_field').value,,
    'zip_code': document.getElementById('zip_code_field').value,
};

jQuery.post('<?php echo admin_url( 'admin-ajax.php' ); ?>', data, function (response) {
     if (response.success === true) {
         // Handle Success
         let resp = JSON.parse(response.data.json_response);
         if (resp.success) {
             // Hanle Success Response
         }
    } else {
        alert(response.data);
    }
}).fail(function (response) {
    // Handle Error
    let resp = JSON.parse(response.responseText);
    alert('Error: ' + resp.data);
});

Finally, you would need to add an event listener on the page. You can likely call this on page load

function addEvtListenerToCheckoutPage() {
    ?>
    <script>
        document.addEventListener('DomContentLoaded', (event) => {
            document.addEventListener('focusout', (event) => {
                if (document.getElementById('country_field') !== "" &&
                    document.getElementById('zip_code_field) !== "") {
                    
                    // Insert jQuery.Post function here
                }
            })
        })
    <script>
    <?php
}

Upvotes: 1

Related Questions