Below we share WooCommerce code snippets prepared for WordPress users and developers. Add the code to your theme’s (preferably a child theme’s) functions.php file or to a code snippet plugin such as WPCode. Take a backup before adding it to a live site and test the code in a staging environment.
Add a custom currency to WooCommerce
To add a custom currency, replace the ABC code, the currency name and symbol in the code below with your own currency. After saving, the new currency becomes selectable under WooCommerce > Settings > General.
add_filter( 'woocommerce_currencies', 'add_my_currency' );
function add_my_currency( $currencies ) {
$currencies['ABC'] = __( 'Currency name', 'woocommerce' );
return $currencies;
}
add_filter( 'woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2 );
function add_my_currency_symbol( $currency_symbol, $currency ) {
if ( 'ABC' === $currency ) {
$currency_symbol = '$';
}
return $currency_symbol;
}
Remove the product description on the single product page
The first snippet removes the product description tab, and the second removes the short description below the price. Use the one you need.
// Removes the product description tab
add_filter( 'woocommerce_product_tabs', 'remove_description_tab', 98 );
function remove_description_tab( $tabs ) {
unset( $tabs['description'] );
return $tabs;
}
// Removes the short description below the price
add_action( 'init', 'remove_short_description' );
function remove_short_description() {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
}
Remove zero decimals from product prices
Prices appear as 100 TL instead of 100.00 TL.
add_filter( 'woocommerce_price_trim_zeros', '__return_true' );
Hide the quantity on the cart page
add_filter( 'woocommerce_is_sold_individually', 'remove_quantity_column', 10, 2 );
function remove_quantity_column( $sold_individually, $product ) {
if ( is_cart() ) {
return true;
}
return $sold_individually;
}
Limit the WooCommerce order note length
add_filter( 'woocommerce_checkout_fields', 'limit_order_note_length' );
function limit_order_note_length( $fields ) {
$fields['order']['order_comments']['maxlength'] = 200;
return $fields;
}
Show custom billing fields based on product ID
Adds an extra billing field to the checkout page if a specific product is in the cart.
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_field_conditional_logic' );
function custom_checkout_field_conditional_logic( $fields ) {
if ( ! WC()->cart ) {
return $fields;
}
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
// Replace 2020 with your own product ID
if ( 2020 === $product_id ) {
$fields['billing'][ 'billing_field_' . $product_id ] = array(
'label' => __( 'Product-specific field', 'woocommerce' ),
'placeholder' => __( 'Product-specific field', 'woocommerce' ),
'required' => false,
'class' => array( 'form-row-wide' ),
'clear' => true,
);
}
}
return $fields;
}
Hide other shipping methods when free shipping is available
Automatically making free shipping the only option when it’s available improves the user experience; customers won’t accidentally choose a paid method. The code below keeps the local pickup option.
add_filter( 'woocommerce_package_rates', 'only_show_free_shipping_when_available', 100, 2 );
function only_show_free_shipping_when_available( $rates, $package ) {
$new_rates = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
}
}
if ( empty( $new_rates ) ) {
return $rates;
}
// Also keep local pickup if available
foreach ( $rates as $rate_id => $rate ) {
if ( 'local_pickup' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
}
}
return $new_rates;
}
Remove the additional information tab on the single product page
add_filter( 'woocommerce_product_tabs', 'remove_product_tabs', 98 );
function remove_product_tabs( $tabs ) {
unset( $tabs['additional_information'] );
return $tabs;
}
Add a new country to the country list
add_filter( 'woocommerce_countries', 'woo_add_my_country' );
function woo_add_my_country( $countries ) {
$countries['AEDUB'] = __( 'Dubai', 'woocommerce' );
return $countries;
}
add_filter( 'woocommerce_continents', 'woo_add_my_country_to_continent' );
function woo_add_my_country_to_continent( $continents ) {
$continents['AS']['countries'][] = 'AEDUB';
return $continents;
}
Remove the breadcrumb
add_action( 'init', 'remove_wc_breadcrumbs' );
function remove_wc_breadcrumbs() {
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
}
Change the shop page title
add_filter( 'woocommerce_page_title', 'shop_page_title' );
function shop_page_title( $title ) {
if ( is_shop() ) {
return 'New shop title';
}
return $title;
}
Redirect to checkout after adding to cart
For the code to work, disable the “Enable AJAX add to cart buttons on archives” option under WooCommerce > Settings > Products.
add_filter( 'woocommerce_add_to_cart_redirect', 'add_to_cart_checkout_redirect' );
function add_to_cart_checkout_redirect( $url ) {
return wc_get_checkout_url();
}
Hide specific categories from the shop page
add_action( 'woocommerce_product_query', 'remove_categories_shop' );
function remove_categories_shop( $q ) {
if ( ! is_shop() ) {
return;
}
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'category-1', 'category-2' ), // Category slugs to hide
'operator' => 'NOT IN',
);
$q->set( 'tax_query', $tax_query );
}
Remove the company name field from checkout
add_filter( 'woocommerce_checkout_fields', 'remove_company_name' );
function remove_company_name( $fields ) {
unset( $fields['billing']['billing_company'] );
return $fields;
}
Remove the state field from checkout
add_filter( 'woocommerce_checkout_fields', 'remove_state_field' );
function remove_state_field( $fields ) {
unset( $fields['billing']['billing_state'] );
unset( $fields['shipping']['shipping_state'] );
return $fields;
}
Quickly translate any string
For permanent, comprehensive translations, we recommend using a plugin such as Loco Translate.
add_filter( 'gettext', 'translate_text', 20, 3 );
function translate_text( $translated, $text, $domain ) {
if ( 'woocommerce' === $domain && 'Choose an option' === $text ) {
$translated = 'Please select';
}
return $translated;
}
Exclude a category from the product categories widget
add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );
function woo_product_cat_widget_args( $cat_args ) {
$cat_args['exclude'] = array( 16 ); // Category ID to exclude
return $cat_args;
}
Change “Out of stock” to “Sold”
add_filter( 'woocommerce_get_availability_text', 'availability_filter_func', 10, 2 );
function availability_filter_func( $availability, $product ) {
if ( ! $product->is_in_stock() ) {
$availability = 'Sold';
}
return $availability;
}
Show “Already in cart” instead of “Add to cart” if the product is in the cart
The code works on both the single product page and product lists.
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text', 10, 2 );
function woo_custom_cart_button_text( $text, $product ) {
if ( WC()->cart ) {
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( (int) $cart_item['product_id'] === $product->get_id() ) {
return __( 'Already in cart', 'woocommerce' );
}
}
}
return $text;
}
Hide the product count in category view
add_filter( 'woocommerce_subcategory_count_html', '__return_empty_string' );
Make account fields required on checkout
add_filter( 'woocommerce_checkout_fields', 'woo_filter_account_checkout_fields' );
function woo_filter_account_checkout_fields( $fields ) {
if ( isset( $fields['account']['account_username'] ) ) {
$fields['account']['account_username']['required'] = true;
}
if ( isset( $fields['account']['account_password'] ) ) {
$fields['account']['account_password']['required'] = true;
}
return $fields;
}
Rename a product tab
add_filter( 'woocommerce_product_tabs', 'woo_rename_tab', 98 );
function woo_rename_tab( $tabs ) {
if ( isset( $tabs['description'] ) ) {
$tabs['description']['title'] = 'Daha fazla bilgi';
}
return $tabs;
}
Add a custom field to product variations
// Adds a field to the variation edit screen
add_action( 'woocommerce_product_after_variable_attributes', 'variation_custom_field', 10, 3 );
function variation_custom_field( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'variation_custom_field[' . $loop . ']',
'label' => __( 'Custom field', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_variation_custom_field', true ),
) );
}
// Saves the field
add_action( 'woocommerce_save_product_variation', 'save_variation_custom_field', 10, 2 );
function save_variation_custom_field( $variation_id, $i ) {
if ( isset( $_POST['variation_custom_field'][ $i ] ) ) {
$value = sanitize_text_field( wp_unslash( $_POST['variation_custom_field'][ $i ] ) );
update_post_meta( $variation_id, '_variation_custom_field', $value );
}
}
List WooCommerce product categories
Add this code to the theme template where you want to show the categories.
$product_categories = get_terms( array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
) );
if ( ! empty( $product_categories ) && ! is_wp_error( $product_categories ) ) {
echo '<ul>';
foreach ( $product_categories as $product_category ) {
echo '<li><a href="' . esc_url( get_term_link( $product_category ) ) . '">' . esc_html( $product_category->name ) . '</a></li>';
}
echo '</ul>';
}
Change the “From” name and email address
All WordPress emails are sent with the sender name and address in the WooCommerce settings. If you use a plugin such as WP Mail SMTP, also check the sender information in that plugin’s settings.
add_filter( 'wp_mail_from_name', 'woo_custom_wp_mail_from_name', 99 );
function woo_custom_wp_mail_from_name( $name ) {
return wp_specialchars_decode( get_option( 'woocommerce_email_from_name' ), ENT_QUOTES );
}
add_filter( 'wp_mail_from', 'woo_custom_wp_mail_from', 99 );
function woo_custom_wp_mail_from( $email ) {
return sanitize_email( get_option( 'woocommerce_email_from_address' ) );
}
Get the IDs of featured products
// WooCommerce's built-in function (cached)
$featured_ids = wc_get_featured_product_ids();
// Alternative: only published featured products
$featured_ids = wc_get_products( array(
'featured' => true,
'status' => 'publish',
'limit' => -1,
'return' => 'ids',
) );
Set a minimum order amount
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart', 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
$minimum = 50; // Minimum order amount
if ( ! WC()->cart ) {
return;
}
$total = (float) WC()->cart->get_cart_contents_total();
if ( $total < $minimum ) {
$message = sprintf(
'The minimum order amount is %s. Your cart total is: %s.',
wc_price( $minimum ),
wc_price( $total )
);
if ( is_cart() ) {
wc_print_notice( $message, 'error' );
} else {
wc_add_notice( $message, 'error' );
}
}
}
Change the default sorting on the shop page
add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' );
function custom_default_catalog_orderby( $orderby ) {
return 'date'; // Other options: 'price', 'price-desc', 'popularity', 'rating', 'menu_order'
}
Add an extra recipient to the order completed email
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'woo_extra_email_recipient', 10, 2 );
function woo_extra_email_recipient( $recipient, $order ) {
return $recipient . ', ornek@alanadiniz.com';
}
Make the phone number optional
add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 );
function wc_npr_filter_phone( $address_fields ) {
$address_fields['billing_phone']['required'] = false;
return $address_fields;
}