• Resolved jebachristy

    (@jebachristy)


    I have installed wordpress and woocommerce plugin and enabled flat rate and local pickup methods.

    I am using legacy checkout shortcode method.

    I have added 2 radio buttons in form-checkout template and when clicked, trigger the package rate hook but it is not working for customers.

    But it works fine for admin when checkout a product.

    Below is the code. Kindly help me to resolve.

    <script>
    jQuery( document ).ready(function( $ ) {

    jQuery(".ccdelivery_type").on("click", function () {
    const selected = jQuery(this).val();
    console.log('Selected delivery type:', selected);
    jQuery('body').trigger('update_checkout');

    });

    </script>

    <form name="checkout" method="post" class="checkout woocommerce-checkout zzzssstt" action="<?php echo esc_url( wc_get_checkout_url() ); ?>" enctype="multipart/form-data" aria-label="<?php echo esc_attr__( 'Checkout', 'woocommerce' ); ?>">
    <label><input type="radio" name="delivery_type" value="home" class="ccdelivery_type"> Home
    Delivery</label>
    <label><input type="radio" name="delivery_type" value="pickup" class="ccdelivery_type"> Store
    Pickup</label>
    //other html layout related codes as in default checkout
    </form>

    functions.php

    function update_shipping_cost_based_on_custom_field($rates, $package) {
    if (!isset($_POST['post_data'])) {
    return $rates;
    }
    parse_str($_POST['post_data'], $post_data);
    echo "testing";
    print_r($rates);
    return $rates;
    }
    add_filter('woocommerce_package_rates', 'update_shipping_cost_based_on_custom_field', 10, 2);
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @jebachristy,

    Thanks for sharing the details!

    It sounds like you’re working on a custom checkout experience using the legacy shortcode and some custom JavaScript and PHP. From what you’ve described, the logic works for admins but not for other users, which suggests there might be permission-related or context-specific conditions at play.

    That said, custom code like this falls outside the scope of our support. For advanced customization, we recommend consulting with a developer or joining our WooCommerce Community Slack where experienced developers and users often share insights and solutions.

    That said, we’d be grateful if you could share your feedback here: https://ww.wp.xz.cn/support/plugin/woocommerce/reviews/#new-post

    Thanks again for reaching out!

    Hey @jebachristy

    Here’s what’s wrong and how to fix it.

    The Issue

    Your code works for admin but not for customers, probably because
    1. The woocommerce_package_rates hook is running before $_POST[‘post_data’] is available.
    2. Your script doesn’t pass delivery_type into the request in a way WooCommerce sees on reload.
    3. You’re not saving the selected delivery type to session or order meta, so shipping logic doesn’t persist.

    Steps to Fix

    1. Save the delivery type to session (on checkout update):

    Add this to functions.php:

    add_action('woocommerce_checkout_update_order_review', function( $post_data ) {
    parse_str($post_data, $parsed_data);
    if (!empty($parsed_data['delivery_type'])) {
    WC()->session->set('selected_delivery_type', sanitize_text_field($parsed_data['delivery_type']));
    }
    });
    1. Modify shipping rates based on session value:

    Update your filter like this

    add_filter('woocommerce_package_rates', function( $rates, $package ) {
    $selected = WC()->session->get('selected_delivery_type');

    if ($selected === 'pickup') {
    // Disable flat rate if pickup selected
    foreach ($rates as $rate_id => $rate) {
    if ($rate->method_id === 'flat_rate') {
    unset($rates[$rate_id]);
    }
    }
    }

    if ($selected === 'home') {
    // Disable local pickup if home selected
    foreach ($rates as $rate_id => $rate) {
    if ($rate->method_id === 'local_pickup') {
    unset($rates[$rate_id]);
    }
    }
    }

    return $rates;
    }, 10, 2);
    1. Ensure your radio input is inside the checkout and is named delivery_type.

    Anything else let me know.

    Plugin Support LovingBro (woo-hc)

    (@lovingbro)

    Hi @jebachristy,

    As there’s been no follow-up, I’ll consider this resolved for the time being. We’re happy to jump back in if anything comes up!

    If WooCommerce has been useful for your store and you appreciate the support you have received, we’d truly appreciate it if you could leave us a quick 5-star review here: https://ww.wp.xz.cn/support/plugin/woocommerce/reviews/#new-post

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

The topic ‘Package rate hook not triggered’ is closed to new replies.