Skip to content
Advertisement

Display shipping methods to frontend as in the admin panel?

In WooCommerce, I’m doing the form of adding / changing product (at the front-end). I need to display the fields for changing the cost of delivery of the installed methods as in the admin panel.

How can I do it?

Here is my actual code:

<form>
<!-- Various input fields -->

<?php foreach ($shipping_methods as $method) { ?>
  <lebel>
    <h3><?php echo $method ?></h3>
    <input type="number" value="<?php //hook for input - output to make changes of shipping price of this method??Which one ?>">
  </lebel>
<?php } ?>

<input type="submit" value="<?php echo $edit_action ? "Save" : "Add"; ?> Product">
</form>

Advertisement

Answer

As Shipping Methods are connected to a Shipping Zone, WooCommerce need to know which is the customer shipping zone. So your script will not work in a correct way until you get that information.

To get that information, customer needs to logged in or if is not logged in it should need to add a product in cart…

Now the correct code to make your script working is:

// (temporary defining $edit_action for test purpose)
// To be removed and replaced by yours
$edit_action = true;
?>
<form>
    <!-- Various input fields -->

    <?php

    // Get the active shipping methods (Customer is logged in or cart is not empty)
    $rates = WC()->session->get("shipping_for_package_0")['rates'];
    if( ! empty($rates) ){
        foreach( $rates as $rate_key => $rate ){
            $method_id = $rate->method_id; // The method ID slug
            $instance_id = $rate->instance_id;
            $rate_id = $rate_key; // The rate ID (made of: $method_id + ':' + $instance_id)
            // Or $rate_id = $rate->id;
            $label = $rate->label; // The method ID Label
            $cost = $rate->cost;
            $taxes_array = $rate->taxes;
        ?>
        <label>
            <h3><?php echo $label ?></h3>
            <input type="number" value="<?php echo number_format($cost, 2); ?>">
        </label>
        <?php
        }
    }
    ?>

    <input type="submit" value="<?php echo $edit_action ? "Save" : "Add"; ?> Product">
</form>
    

Tested and works (only when the customer shipping zone is defined)


Now if you want to get everything like in shipping backend settings, you will need to:

  1. Get the shipping zones
  2. For each shipping zone you will get the available Shipping methods
  3. For each shipping method you can have shipping classes rates

So this is quite much more complicated…


Here is the way to start, but I didn’t find the way to get/set cost as it’s different for each shipping method type…

// Initializing variable
$zones = array();

// Rest of the World zone
$zone                                              = new WC_Shipping_Zone(0);
$zones[$zone->get_id()]                            = $zone->get_data();
$zones[$zone->get_id()]['formatted_zone_location'] = $zone->get_formatted_location();
$zones[$zone->get_id()]['shipping_methods']        = $zone->get_shipping_methods();

// Merging shipping zones
$shipping_zones = array_merge( $zones, WC_Shipping_Zones::get_zones() );

foreach ( $shipping_zones as $shipping_zone ) {
    // Row output (for testing)
    // echo '<pre>'; print_r($shipping_zone); echo '</pre>';

    $zone_id = $shipping_zone['id'];

    $zone_name = $zone_id == '0' ? __('Rest of the word', 'woocommerce') : $shipping_zone['zone_name'];
    $zone_locations = $shipping_zone['zone_locations']; // (It's an array)
    $zone_location_name = $shipping_zone['formatted_zone_location'];
    $zone_shipping_methods = $shipping_zone['shipping_methods'];

    foreach ( $zone_shipping_methods as $shipping_method_obj ) {
        // Row output (for testing)
        // echo '<pre>'; print_r($shipping_method_obj); echo '</pre>';

        echo $shipping_method_obj->id.'<br>';
        echo $shipping_method_obj->get_instance_id().'<br>';
        echo $shipping_method_obj->get_rate_id().'<br>';
        echo $shipping_method_obj->get_method_title().'<br>'; // Generic name
        echo $shipping_method_obj->get_title().'<br>'; // Customized name
    }
}

The class that make the Shipping cost is WC_Shipping_Rate


Other related recent answer: Display shipping cost on product page – WooCommerce

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement