Search code examples
wordpresswoocommercepluginshook-woocommerceshipping-method

WooCommerce Shipping Plugin - Checkout Shipping Cost Not Updating(AJAX)


I've searched to find any solution but nothing worked.

Here what I want to do:

  1. Add new field in checkout page called Shipping show as selectbox filled with Preorder and Non-preorder
  2. If preorder then would show Location field show as selectbox
  3. If non-preorder then would show Location field same as step 2 but different list option.
  4. When Shipping and Location filled correctly, shipping cost would show with correct shipping cost.

Problem

When I filled all required field and Shipping, Location shipping cost wound calculate again. It do AJAX request, but shipping cost keep the same.

But, when I change Name or Street Address shipping cost updated correctly, which is bad behavior. When user click Place Order immediately would POST wrong shipping cost.

Here my Youtube Video to make clear what I ask for help.


What I did to create my own plugin:

  1. Create custom field using hook something like this
    public function woocommerce_checkout_fields( $fields )
    {
        // var_dump(have_rows('location', 'option'));
        // die('test');

        $options = [];

        $options[''] = 'Select Shipping';
        $options['pre-order'] = 'Pre Order';
        if (!$this->hasPreorderItem()) {
            $options['non-pre-order'] = 'Non Pre Order';
        }

        // Add custom billing shipping
        $fields['billing']['billing_shipping'] = array (
            'type' => 'select',
            'label' => 'Shipping',
            'placeholder' => 'Select shipping',
            'class' => array ('address-field', 'update_totals_on_change' ),
            'required' => true,
            'options' => $options
            // 'options' => $this->getLocations()
        );

        $fields['billing']['billing_location_pre_order'] = array (
            'type' => 'select',
            'label' => 'Location(Pre Order)',
            'placeholder' => 'Select Preorder Location',
            'class' => array ('address-field', 'update_totals_on_change' ),
            'required' => false,
            'options' => $this->preorderLocations->getLocations()
            // 'options' => $this->getLocations()
        );

        // Add custom billing location
        $fields['billing']['billing_location'] = array (
            'type' => 'select',
            'label' => 'Location',
            'placeholder' => 'Select location',
            'class' => array ('address-field', 'update_totals_on_change' ),
            'required' => false,
            'options' => $this->locations->getLocations()
            // 'options' => $this->getLocations()
        );

        return $fields;
    }
  1. Create class to help calculate cost based on Shipping and Location.

What I have tried

  1. Using this answer but no luck. Click here for the question.
  2. Tried to make field to required, as I saw someone said it only update when all required fields filled.
  3. Tried to disable cache in wp-config.php

Solution

  • I found solution from this link

    I might help someone that has similar problem. You just need to clear the session. So, WooCommerce would re-calculate and recall your calculate_shipping() function.

    To do that add woocommerce_checkout_update_order_review hook. It would look something like this

    add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 1);
    function action_woocommerce_checkout_update_order_review( $posted_data )
    {
        global $woocommerce;
        $packages = $woocommerce->cart->get_shipping_packages();
        foreach( $packages as $package_key => $package ) {
            $session_key  = 'shipping_for_package_'.$package_key;
            $stored_rates = WC()->session->__unset( $session_key );
        }
    }