Nexcess Logo

Useful Code Snippets for WooCommerce on Your Site

Knowledge Base Home

Notice anything different?

We've enhanced the appearance of our portal and we're working on updating screenshots. Things might look different, but the functionality remains the same.
September 08, 2021

WooCommerce is a heavily-used plugin to add ecommerce features and functionality to your WordPress site. WooCommerce includes a massive amount of actions and filters, which means that you can easily customize your site.

The recommended way to add custom code to your site would be using the code snippets plugin, in the child themes functions.php file, or into a site-specific plugin.

If you wanted to remove the marketing hub in WooCommerce from wp-admin but still want to use WooCommerce analytics, then you can add this code snippet to your site:

add_filter( 'woocommerce_admin_features', function( $features ) {


* Filter list of features and remove those not needed *


return array_values(

array_filter( $features, function($feature) {

return $feature !== 'marketing';

} )


} );

To remove related cross-sell products on the cart page in WooCommerce, you can use this code snippet:

add_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display' );

To remove related products from showing on the product detail page in WooCommerce, you can add this code snippet:

remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );

To display coupons used on order detail and order notification, along with the percentage discount, you can use this code snippet:

function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {

// Exit if there is no coupons applied

if ( sizeof( $order->get_coupon_codes() ) == 0 ) return $total_rows;

$new_total_rows = []; // Initializing

foreach( $total_rows as $key => $total ) {

$new_total_rows[$key] = $total;

if ( $key == 'discount' ) {

// Get used coupon codes only

$coupon_codes = $order->get_coupon_codes();

// Loop through WC_Order_Item_Coupon objects

foreach ( $coupon_codes as $index => $coupon_code ) {

// Get an instance of the WC_Coupon Object

$coupon = new WC_Coupon( $coupon_code );

// Discount type = percent & amount NOT empty

if ( $coupon->get_discount_type() == 'percent' && ! empty ( $coupon->get_amount() ) ) {

$coupon_codes[$index] = $coupon_code . ' (' . $coupon->get_amount() . '%)';



// Insert applied coupon codes in total lines after discount line

$new_total_rows['coupon_codes'] = array(

'label' => __( 'Applied coupons:', 'woocommerce' ),

'value' => implode( ', ', $coupon_codes ),




return $new_total_rows;


add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 )

The final code snippet can be used to hide specific products from being displayed on the shop page by ID. To use the code snippet on your site, replace the product post IDs in the code snippet.

add_action( 'pre_get_posts', 'wc_shop_exclude_id' );

function custom_pre_get_posts_query( $q ) {

if ( ! $q->is_main_query() ) return;

if ( ! $q->is_post_type_archive() ) return;

if ( ! is_admin() && is_shop() ) {

$q->set( 'post__not_in', array(70, 53) ); // Replace 70 and 53 with your products IDs. Separate each ID with a comma.


remove_action( 'pre_get_posts', 'wc_shop_exclude_id' );


Using any of the provided code snippets on your WooCommerce site will help you be able to solve specific problems without having to use another plugin for WooCommerce to be able to achieve this.

Useful Links

Luke Cavanagh
Luke Cavanagh

Luke Cavanagh, Strategic Support & Accelerant at Liquid Web, brings a decade of experience working with WordPress and WooCommerce to our product team. His GitHub page offers a glimpse into his multiple areas of subject matter expertise.

"Ninja stuff with WordPress and WooCommerce," is an apropos way to describe Luke's savviness with these platforms — and his way of influencing our organization for improving to them.

Coming out of the University of Brighton with a Business and Technology Education Council (BTEC) Higher National Diploma (HND) in 2D & 3D Design, Luke's credentials prepared him well for his current role that blends both web development and design. His HND credential leveraged his foundational learning at West Kent College, where is received a National Diploma (ND) in Graphic Design.

In his personal life, Luke is a devoted husband and teen wrangler. He considers himself a Synthwave enthusiast, Jerry Goldsmith fan, and Doctor Who aficionado. He is happy to introduce his friends and teammates to essential vocabulary for life found only in British English, such as "gubbins" and similar terms.

We use cookies to understand how you interact with our site, to personalize and streamline your experience, and to tailor advertising. By continuing to use our site, you accept our use of cookies and accept our Privacy Policy.