Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
jorpdesigns / hide-tax-label.php
Created July 15, 2021 14:39
Snippet to hide (incl. tax) suffix in WooCommerce order display
<?php
add_filter( 'woocommerce_get_order_item_totals', 'wc_get_order_item_totals', 10, 3 );
function wc_get_order_item_totals( $total_rows, $order, $tax_display ) {
$total_rows['order_total'] = array(
'label' => __( 'Total:', 'woocommerce' ),
'value' => $order->get_formatted_order_total(),
);
return $total_rows;
}
@jorpdesigns
jorpdesigns / add-cart-discount.php
Last active January 8, 2025 11:51
Snippet to add discount to WooCommerce cart
<?php
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_discount' );
function woocommerce_custom_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// ADD FLAT RATE DISCOUNT
$discount = 5; // Replace 5 with your discount
@jorpdesigns
jorpdesigns / add-cart-fee.php
Last active July 15, 2021 14:32
Snippet to add fee to WooCommerce cart
<?php
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// ADD FLAT RATE FEE
$surcharge = 5; // Replace 5 with your fee
@jorpdesigns
jorpdesigns / disable-required-fields-if-local-pickup.php
Last active December 30, 2022 14:23
Snippet to remove billing fields on WooCommerce checkout page if the selected shipping method is Local Pickup
<?php
// FUNCTION TO CHECK IF SELECTED SHIPPING METHOD IS LOCAL PICKUP
function is_local_pickup() {
$local_pickup = false;
$shipping_method = 'local_pickup:9'; // Replace 9 with your local pickup id
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ($chosen_shipping == $shipping_method) {
@jorpdesigns
jorpdesigns / hide-shipping-rates.php
Last active July 15, 2021 13:47
Snippet to hide WooCommerce shipping rates if free shipping is available
<?php
// ONLY SHOW FREE SHIPPING METHOD IF AVAILABLE
add_filter( 'woocommerce_package_rates', 'only_show_free_shipping', 100 );
function only_show_free_shipping( $rates ) {
$free_rates = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free_rates[ $rate_id ] = $rate;
break;
@jorpdesigns
jorpdesigns / add-products-to-cart.php
Last active July 15, 2021 14:19
Snippet to automatically add products to WooCommerce cart
<?php
// FUNCTION TO CHECK IF ITEM IS IN CART
function item_in_cart( $product_id ) {
$found = false;
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $value ) {
$product = $value['data'];
if ( $product->get_id() == $product_id ) {
$found = true;
@jorpdesigns
jorpdesigns / remove-shipping-labels.php
Created July 15, 2021 12:21
Snippet to remove WooCommerce shipping order labels
<?php
// CART
add_filter( 'woocommerce_cart_shipping_method_full_label', 'remove_shipping_label', 9999, 2 );
function remove_shipping_label( $label, $method ) {
$new_label = preg_replace( '/^.+:/', '', $label );
return $new_label;
}
// ORDER EMAIL
@jorpdesigns
jorpdesigns / default-billing-country.php
Created July 15, 2021 12:18
Snippet to change default billing country on WooCommerce
<?php
add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
function change_default_checkout_country() {
return 'GB';
}
?>
@jorpdesigns
jorpdesigns / add-nothern-ireland.php
Last active July 15, 2021 12:17
Snippet to add Northern Ireland to WooCommerce countries list
<?php
// ADD NEW COUNTRY
add_filter( 'woocommerce_countries', 'custom_country_nothern_ireland' );
function custom_country_nothern_ireland( $countries ) {
$new_countries = array( 'NIRE' => __( 'Northern Ireland', 'woocommerce' ), );
return array_merge( $countries, $new_countries );
}
// ADD NEW COUNTRY UNDER CONTINENT
@jorpdesigns
jorpdesigns / custom-country-dropdown.php
Created July 15, 2021 12:12
Snippet to reorder countries in dropdown on WooCommerce checkout form
<?php
// DISABLE WOOCOMMERCE SORTING
add_filter( 'woocommerce_sort_countries', '__return_false' );
add_filter( 'woocommerce_countries', 'wc_custom_countries_order', 10, 1 );
function wc_custom_countries_order( $countries ) {
// Replace with iso code of country AND country name (example: US | United States or GB | United Kingdom (UK)
$countries = ['GB' => 'United Kingdom'] + ['US' => 'United States'] + ['CA' => 'Canada'] + $countries;