Skip to content

Instantly share code, notes, and snippets.

@jessepearson
Last active January 11, 2021 16:46
Show Gist options
  • Save jessepearson/c0c62c5e0f3e776da99fa70901184256 to your computer and use it in GitHub Desktop.
Save jessepearson/c0c62c5e0f3e776da99fa70901184256 to your computer and use it in GitHub Desktop.

Revisions

  1. jessepearson renamed this gist Jan 11, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. jessepearson created this gist Jan 11, 2021.
    23 changes: 23 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    <?php // do not copy this line

    /**
    * Allows to change up the name of the Distance Rate label for WooCommerce Distance Rate Shipping.
    */
    add_filter( 'woocommerce_shipping_method_add_rate_args', 'jp_woocommerce_shipping_method_add_rate_args_2021_01_11', 50, 2 );
    function jp_woocommerce_shipping_method_add_rate_args_2021_01_11( $args, $package ) {

    // Check to see if it's a distance rate method.
    if ( false !== strpos( $args['id'], 'distance_rate' ) ) {

    // Get just the time and distance between the parenthesis.
    preg_match('/\(.*\)$/', $args['label'], $matches );
    $args['label'] = $matches[0]; // Contains only distance and time like (10.2 mi; 19 mins)

    // Now we change mi to baby steps, and mins to years.
    // Need to make sure to keep the ; and ) in the search and replace in order to not replace the wrong thing.
    $args['label'] = str_replace( 'mi;', 'baby steps;', $args['label'] );
    $args['label'] = str_replace( 'mins)', 'years)', $args['label'] );
    }

    return $args;
    }