Skip to content

Instantly share code, notes, and snippets.

@dangoodman
dangoodman / functions.php
Last active January 12, 2017 20:37
Round WooCommerce shipping rates to upper close 0.05 (for Swiss franc)
<?php
// Paste everything below this line to your theme's functions.php file.
add_filter('woocommerce_package_rates', 'round_shipping_price_for_swiss_franc');
function round_shipping_price_for_swiss_franc($rates)
{
if (is_array($rates)) {
foreach ($rates as $rate) {
$rate->cost = ceil($rate->cost / 0.05) * 0.05;
@dangoodman
dangoodman / functions.php
Created January 12, 2017 20:34
Hide duplicate shipping options in Woocommerce
<?php
// Paste everything below this line to your theme's functions.php file.
add_filter('woocommerce_package_rates', function($rates) {
/** @var WC_Shipping_Rate[] $rates */
/** @var WC_Shipping_Rate[] $uniqueRates */
$uniqueRates = array();
foreach ($rates as $key => $rate) {
@dangoodman
dangoodman / functions.php
Created March 9, 2017 11:49
Woocommerce: Hide specified shipping options when shipping to PO boxes
<?php
// Paste everything below this line to your theme's functions.php file.
add_filter('woocommerce_package_rates', function($rates, $package) {
$destination = $package['destination'];
// Put postcode, address 1 and address 2 into an array
$check_address = array(
$destination['address'],
@dangoodman
dangoodman / functions.php
Created March 16, 2017 14:58
WooCommerce: Hide shipping option labels
<?php
// Paste everything below this line to your theme's functions.php file.
// Hide shipping option labels.
// Example:
// — Regular Shipping: $20 => $20
// — Express Shipping: $30 => $30
// — Free Shipping => Free
add_filter('woocommerce_cart_shipping_method_full_label', function($label) {
@list(, $price) = explode(':', $label);
@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;
@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 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
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 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 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;
});