Skip to content

Instantly share code, notes, and snippets.

@goliver79
Last active April 1, 2020 20:14
Show Gist options
  • Save goliver79/3bc8dba5fbde33213e072bdd642f587b to your computer and use it in GitHub Desktop.
Save goliver79/3bc8dba5fbde33213e072bdd642f587b to your computer and use it in GitHub Desktop.
Woocommerce hide other shipments if free shipment
<?php
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( !class_exists( 'HideShipmentsIfFree' ) ) {
class HideShipmentsIfFree {
function activate() {
add_action( 'woocommerce_package_rates', array( $this, 'hide_shipments_if_free' ), 100 );
}
function hide_shipments_if_free( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
}
$hide_shipments = new HideShipmentsIfFree();
$hide_shipments->activate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment