0

So, I am adding my own drop downs on the shipping section in the checkout of woocommerce. They will filter the options via jquery to only show and hide the valid options of the user (they have to pick time/day and area in the city) eg: Sunday Afternoon / Area A

The problem I have is that whenever a new option is selected from the radio buttons in Woocommerce, the whole this gets loaded again. I have found the AJAX call "shipping_method_selected" in "cart.js" and have successfully overrided this template in order to add custom data to the AJAX call itself

var data = {
    security: wc_cart_params.update_shipping_method_nonce,
    shipping_method: shipping_methods
};

Has been made into

var data = {
    security: wc_cart_params.update_shipping_method_nonce,
    shipping_method: shipping_methods,
    area: area,
    day: day
};

The problem I have is that the "wc_get_template" code for retrieving and outputting the html in "cart/cart-shipping.php" is located in the wc-cart-functions.php includes core file and I have NO idea how to properly edit it. I've tried to override things and nothing works. This is the code I essentially need to add more onto (such as the $chosen_method)

wc_get_template(
        'cart/cart-shipping.php', array(
            'package'                  => $package,
            'available_methods'        => $package['rates'],
            'show_package_details'     => count( $packages ) > 1,
            'show_shipping_calculator' => is_cart() && $first,
            'package_details'          => implode( ', ', $product_names ),
            /* translators: %d: shipping package number */
            'package_name'             => apply_filters( 'woocommerce_shipping_package_name', ( ( $i + 1 ) > 1 ) ? sprintf( _x( 'Shipping %d', 'shipping packages', 'woocommerce' ), ( $i + 1 ) ) : _x( 'Shipping', 'shipping packages', 'woocommerce' ), $i, $package ),
            'index'                    => $i,
            'chosen_method'            => $chosen_method,
            'formatted_destination'    => WC()->countries->get_formatted_address( $package['destination'], ', ' ),
            'has_calculated_shipping'  => WC()->customer->has_calculated_shipping(),
        )
    );

But since it's located in a core file (and it doesn't seem to have a filter or action attached to it, it's just a basic public function), I have no idea what to do.

My current situation, is I have extended the WC-Ajax class and created my own function for the AJAX to send to. Originally, in the 'cart.js' file, it was sending the AJAX to

$.ajax( {
    type:     'post',
    url:      get_url( 'update_shipping_method' ),
    data:     data,
    dataType: 'html',
    success:  function( response ) {
        update_cart_totals_div( response );
    },
    complete: function() {
        unblock( $( 'div.cart_totals' ) );
        $( document.body ).trigger( 'update_shipping_method' );
    }
} );

I have updated it to send to

    url:      get_url( 'custom_update_shipping_method' ),

As such, I am able to manipulate the data I send through the AJAX call in the JS, but since I can't change the wc_get_template... I have no idea what to do.

Please please help me. I am at a loss for what to do. I don't usually edit WooCommerce like this.

Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.