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.
Contact Us
Contact Us
Sign in
Sign in
Nexcess Logo

Useful Code Snippets for WooCommerce on Your Site

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

Product Operations Manager at Nexcess. A devoted husband and tween wrangler. Synthwave enthusiast. Jerry Goldsmith fan. Doctor Who fan and related gubbins.