Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
ChromeOrange / gist:10013862
Last active December 16, 2022 18:24
Sort shipping methods by cost in WooCommerce 2.1. Add to theme functions.php file
/**
* Sort WooCommerce shipping methods by cost, lowest to highest
*/
add_filter( 'woocommerce_package_rates' , 'sort_woocommerce_available_shipping_methods', 10, 2 );
function sort_woocommerce_available_shipping_methods( $rates, $package ) {
if ( !$rates ) return $rates;
$tmp = Array();
foreach( $rates as $ma ) {
@ChromeOrange
ChromeOrange / gist:9832001
Created March 28, 2014 12:51
Set the WooCommerce store city and zipcode / postcode
/**
* Set the store base city and postcode / zipcode
*/
add_filter( 'woocommerce_countries_base_city' , 'set_woocommerce_countries_base_city' );
function set_woocommerce_countries_base_city() {
// Replace with your store town/city
return 'Townland';
}
add_filter( 'woocommerce_countries_base_postcode' , 'set_woocommerce_countries_base_postcode' );
@ChromeOrange
ChromeOrange / functions.php
Last active May 7, 2020 22:50
Set a minimum order value for WooCommerce - requires 2.1 or higher
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// Set this variable to specify a minimum order value
$minimum = 50;
if ( WC()->cart->total < $minimum ) {
if( is_cart() ) {
@ChromeOrange
ChromeOrange / gist:9559252
Created March 14, 2014 23:33
Deactivate Flat Rate Shipping if products with specific shipping classes are in the cart
/**
* Deactivate Flat Rate Shipping if products with specific shipping
* classes are in the cart
*
* Add the shipping class slugs to the $shippingclass_array array
*/
add_filter( 'woocommerce_shipping_flat_rate_is_available', 'unset_woocommerce_shipping_methods_flat_rate', 10 ,2 );
function unset_woocommerce_shipping_methods_flat_rate ( $return, $package ) {
// Setup an array of shipping classes that do not allow Flat Rate Shipping
@ChromeOrange
ChromeOrange / gist:9518080
Last active August 29, 2015 13:57
Check the cart for specific products, remove specific USPS Shipping methods if they are present. See this screen shot for more info http://cld.wthms.co/5Ag1 for more on how to get the USPS shipping values to remove
/**
* Check the cart for specific products, remove specific USPS Shipping methods if they are present
*
* Add the code to your theme functions.php file
*/
add_filter( 'woocommerce_package_rates', 'unset_usps_shipping_methods' , 10, 2 );
function unset_usps_shipping_methods( $rates, $package ) {
/**
* Setup an array or products
@ChromeOrange
ChromeOrange / gist:9497629
Created March 11, 2014 23:50
Deactivate USPS Shipping if products with specific shipping classes are in the cart. Add the shipping class slugs to the $shippingclass_array array
/**
* Deactivate USPS Shipping if products with specific shipping
* classes are in the cart
*
* Add the shipping class slugs to the $shippingclass_array array
*/
add_filter( 'woocommerce_shipping_usps_is_available', 'unset_woocommerce_shipping_methods_usps', 10 ,2 );
function unset_woocommerce_shipping_methods_usps ( $return, $package ) {
// Setup an array of shipping classes that do not allow USPS Shipping
@ChromeOrange
ChromeOrange / gist:9486596
Last active June 17, 2016 09:34
Check the cart for specific shipping classes, remove Free Shipping if they are present
/**
* Check the cart for specific shipping classes, remove Free Shipping if they are present
*
* This code will remove free shipping if a product is in the cart that can not be delivered for free
* Assign a shipping class to those products and add the slug to the $freeshipping_array
* Multiple shipping classes can be added to the array
*
* Add the code to your theme functions.php file
*/
add_filter( 'woocommerce_package_rates', 'unset_free_shipping_when_product_in_cart' , 10, 1 );
@ChromeOrange
ChromeOrange / gist:9200450
Last active November 30, 2021 10:40
Add a surcharge if certain products are in the cart, ideal for setup fees etc
/**
* Add a fixed surcharge to your cart / checkout based on products in cart
* Taxes, shipping costs and order subtotal are all included in the surcharge amount
*
* Change $fixed to set the surcharge to a value to suit
*
* Change in_array to !in_array to EXCLUDE the $countries array from surcharges
*
* Uses the WooCommerce fees API
* Add to theme functions.php
@ChromeOrange
ChromeOrange / gist:8692924
Created January 29, 2014 17:35
Hide free shipping if certain products are in the cart
/**
* Check the cart for specific products, remove Free Shipping if they are present
*
* Free Shipping method must be enabled
*
* Add the code to your theme functions.php file
*/
add_filter( 'woocommerce_available_shipping_methods', 'free_shipping_when_product_in_cart' , 10, 1 );
function free_shipping_when_product_in_cart( $available_methods ) {
@ChromeOrange
ChromeOrange / gist:8302996
Created January 7, 2014 17:29
Add a custom field from an order to the PDF invoice
/**
* Adding a custom field to the invoice
*
* Uses template tag [[CUSTOMFIELD]]
*
* custom field name : childname
*
* 1 - Edit template.php and add the tag in the desired place
* 2 - add code to the theme functions.php file
*/