Skip to content

Instantly share code, notes, and snippets.

@dangoodman
dangoodman / functions.php
Created May 19, 2021 12:33
"Squash" all delivery options into a single one
<?php
// Paste everything below this line to your child-theme's functions.php file.
// "Squashes" all delivery options into a single one.
// After pasting this snippet, reset the WooCommerce shipping cache, e.g. add an item to the cart.
add_filter('woocommerce_package_rates', function ($rates, $package) {
if (count($rates) > 1) {
$firstRate = null;
$firstRateId = null;
foreach ($rates as $id => $rate) {
@dangoodman
dangoodman / functions.php
Created August 4, 2020 09:32
WooCommerce: Replace free shipping label
<?php
// Paste everything below this line to your child-theme's functions.php file.
// Change the free shipping label to 'Free shipping'.
add_filter('woocommerce_cart_shipping_method_full_label', static function ($label, $method) {
if ($method->cost == 0) {
$label = 'Free shipping';
}
return $label;
}, 10, 2);
@dangoodman
dangoodman / functions.php
Created April 10, 2020 17:46
WooCommerce: allow HTML in shipping option labels
<?php
// Paste everything below this line to your child-theme's functions.php file.
// Cancel sanitizing for all shipping option labels starting with 'HTML:'.
remove_filter('woocommerce_shipping_rate_label', 'sanitize_text_field');
add_filter('woocommerce_shipping_rate_label', function($label) {
if (substr_compare($label, 'HTML:', 0, 5) === 0) {
$label = ltrim(substr($label, 5));
}
@dangoodman
dangoodman / functions.php
Created April 8, 2020 13:40
WooCommerce: round up shipping prices
<?php
// Paste everything below this line to your child-theme's functions.php file.
// Rounds up all shipping rates by the $roundUpBy value.
// After pasting this snippet, reset the WooCommerce shipping cache, e.g. add an item to the cart.
add_filter('woocommerce_package_rates', function ($rates, $package) {
$roundUpBy = 5;
foreach ($rates as $rate) {
$rate->cost = ceil($rate->cost / $roundUpBy) * $roundUpBy;
}
@dangoodman
dangoodman / functions.php
Created May 26, 2019 11:10
WooCommerce: replace default shipping option title
<?php
// Paste everything below this line to your child-theme's functions.php file.
// Renames any shipping option titled exactly 'Weight Based Shipping' to 'Shipping'.
add_filter('woocommerce_shipping_method_add_rate_args', function($rateArgs) {
if (isset($rateArgs['label']) && $rateArgs['label'] === 'Weight Based Shipping') {
$rateArgs['label'] = 'Shipping';
}
return $rateArgs;
});
@dangoodman
dangoodman / functions.php
Created April 5, 2019 21:06
WooCommerce, Tree Table Rate Shipping: include non-shippable (virtual, etc) items.
<?php
// Paste everything below this line to your child-theme's functions.php file.
// Tree Table Rate Shipping: pass non-shippable (virtual, etc) items as well to shipping rules.
add_filter('trs_include_non_shippable_items', '__return_true');
@dangoodman
dangoodman / functions.php
Created March 27, 2019 22:41
WooCommerce, Tree Table Rate Shipping: Exclude product attributes from the 'Contains' condition.
<?php
// Paste everything below this line to your child-theme's functions.php file.
// Exclude product attributes from the 'Contains' condition.
add_filter('woocommerce_attribute_taxonomies', function($taxs) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10);
foreach ($backtrace as $call) {
if (isset($call['file']) && preg_match('#wc-tree-table-rate-shipping(/|\\\\)tpl.php#', $call['file'])) {
return [];
@dangoodman
dangoodman / functions.php
Created September 12, 2018 10:45
WooCommerce: shipping option description
<?php
// Paste everything below this line to your child-theme's functions.php file.
// Transforms
// >> Shipping Option Name (shipping option description): $99
// to
// >> Shipping Option Name: $99<br>
// >> <small>shipping option description</small>
add_filter('woocommerce_cart_shipping_method_full_label', function($label, $method) {
if (($open = strpos($label, '(')) !== false &&
@dangoodman
dangoodman / functions.php
Last active October 12, 2018 17:39
Hide free shipping options' labels
<?php
// Paste everything below this line to your theme's functions.php file.
// Hide free shipping options prices.
// Example:
// — Free Shipping: $0.00 => Free Shipping
// — Regular Shipping: $20 => Regular Shipping: $20
add_filter('woocommerce_cart_shipping_method_full_label', function($label, WC_Shipping_Rate $rate) {
if (isset($rate->cost) && (float)$rate->cost === 0.0) {
list($label,) = explode(':', $label, 2);
@dangoodman
dangoodman / functions.php
Created January 4, 2018 15:05
WooCommerce: cut shipping option ids if they exceed 50-chars length
<?php
// Paste everything below this line to your child-theme's functions.php file.
// Cut shipping option ids if they exceed 50-chars length.
add_filter('woocommerce_package_rates', function ($rates) {
$shortened = array();
foreach ($rates as $rate_id => $rate) {
$rate_id = substr($rate_id, 0, 50);
$rate->id = $rate_id;
$shortened[$rate_id] = $rate;