Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DonMat/ed8bc114e9f5a375ce4af12b63dcc8cd to your computer and use it in GitHub Desktop.
Save DonMat/ed8bc114e9f5a375ce4af12b63dcc8cd to your computer and use it in GitHub Desktop.
Woocommerce list shipping methods snippet
<?php
/**
* Plugin Name: Woocommerce shipping methods list
* Plugin URI: https://woocommerce.com/
* Description: Use this snippet to show all available or only enabled shipping methods
* Version: 1.0
* Author: Mateusz Utkała
* Author URI: https://utkala.pl/
* Developer: Mateusz Utkała
*
* Requires at least: 4.0
* Tested up to: 4.9.5
*
* Copyright: © 2018 Mateusz Utkała (kontakt@utkala.pl).
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// To use this snippet, download this file into your plugins directory and activate it, or copy the code under this line into the functions.php file of your (child) theme.
// Remember to add_filter( 'your_hook_name', 'list_shipping_methods', 10, 0 );
function list_all_shipping_methods(){
$zones = WC_Shipping_Zones::get_zones();
if (!empty( $zones )){
echo "<ul>";
foreach($zones as $zone){
$methods = $zone['shipping_methods'] ;
uasort( $methods, 'wc_shipping_zone_method_order_uasort_comparison' );
if ( ! empty( $methods ) ) {
foreach ( $methods as $method ) {
echo '<li class="wc-shipping-zone-method">' . esc_html( $method->get_title() ) . '</li>';
}
} else { echo '<li class="wc-shipping-zone-method">' . __( 'No shipping methods offered to this zone.', 'woocommerce' ) . '</li>'; }
}
echo "</ul></div></div>";
}
}
function list_enabled_shipping_methods(){
$zones = WC_Shipping_Zones::get_zones();
if (!empty( $zones )){
echo "<ul>";
foreach($zones as $zone){
$single = WC_Shipping_Zones::get_zone($zone['zone_id']) ;
$methods = $single->get_shipping_methods(true); // enabled_only = true, default: false
uasort( $methods, 'wc_shipping_zone_method_order_uasort_comparison' );
if ( ! empty( $methods ) ) {
foreach ( $methods as $method ) {
echo '<li class="wc-shipping-zone-method">' . esc_html( $method->get_title() ) . '</li>';
}
} else { echo '<li class="wc-shipping-zone-method">' . __( 'No shipping methods offered to this zone.', 'woocommerce' ) . '</li>'; }
}
echo "</ul></div></div>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment