Updating WooCommerce cart shipping totals when Dokan vendor change shipping rates

64 Views Asked by At

I am using WooCommerce and Dokan, I have a custom cart template which has this section in the totals:

<div class="kraftdeck-delivery kraftdeck-cart-totals-items">
        <span class="kraftdeck-cart-totals-items-text">Delivery</span>
        <span>
            <?php
            $packages = WC()->shipping()->get_packages();

            foreach ($packages as $i => $package) {
                $chosen_method = isset(WC()->session->chosen_shipping_methods[$i]) ? WC()->session->chosen_shipping_methods[$i] : '';

                if (isset($package['rates'][$chosen_method])) {
                    $chosen_rate  = $package['rates'][$chosen_method];
                    $shipping_cost = $chosen_rate->cost;

                    // Fetch the vendor ID from the first product in the package.
                    $first_product_in_package = reset($package['contents']);
                    $vendor_id = get_post_field('post_author', $first_product_in_package['product_id']);

                    // Fetch vendor store information.
                    $store_info = dokan_get_store_info($vendor_id);
                    $store_name = isset($store_info['store_name']) ? esc_html($store_info['store_name']) : '';

                    // Get vendor store logo
                    if (!empty($store_info['gravatar_id'])) {
                        $store_logo = wp_get_attachment_url($store_info['gravatar_id']);
                    } else {
                        $store_logo = get_avatar_url($vendor_id, array('size' => 150));
                    }

                    // Output vendor's shipping info
                    echo '<div class="kraftdeck-individual-shipping">';
                    echo '<div class="kraftdeck-individual-shipping-seller">';
                    echo '<img src="' . esc_url($store_logo) . '" alt="' . esc_attr($store_name) . ' Logo" class="kraftdeck-vendor-cart-logo">';
                    echo '<span class="kraftdeck-vendor-cart-name">' . $store_name . '</span>';
                    echo '</div>';
                    echo '<span class="kraftdeck-shipping-cost">' . wc_price($shipping_cost) . '</span>';
                    echo '</div>';
                }
            }
            ?>


        </span>

    </div>

It displays the correct vendor shipping rates like this: enter image description here

However if the vendor updates their rates while the user has products in the cart it doesn't update when the user returned to the cart.

I should add that all users will have an account with the shipping address already set.

Is there a simpler way of getting the shipping?

0

There are 0 best solutions below