Skip to main content

WooCommerce Code Snippets

WooCommerce is a free eCommerce plugin that allows you to sell anything, beautifully. Built to integrate seamlessly with WordPress, WooCommerce is the world’s favorite eCommerce solution that gives both store owners and developers complete control.

With endless flexibility and access to hundreds of free and premium WordPress extensions, WooCommerce now powers 30% of all online stores. From simple stores to complex multivendor marketplaces, WooCommerce has you covered.

WooCommerce action hooks are functions that allow you to modify the behavior of WooCommerce. For example, you can use an action hook to add extra functionality to WooCommerce or to change the default behavior of WooCommerce.

WooCommerce filter hooks are functions that allow you to modify the output of WooCommerce. For example, you can use a filter hook to change the way WooCommerce displays prices or to add extra information to the order confirmation page.

Both action and filter hooks are defined in the WooCommerce plugin code and can be used by custom code that you write or by plugins and themes that you install.

If you’re a WooCommerce developer, then you know that finding useful code snippets can be tough. That’s why we’ve put together this roundup of the best WooCommerce code snippets.

These snippets can be used to change any elements on a WooCommerce shop.

Curated List of WooCommerce Snippets

Customize the Product Image Gallery

add_action( 'after_setup_theme', 'wc_add_theme_support' );
function wc_add_theme_support() {
   add_theme_support( 'wc-product-gallery-zoom' );
}

Remove the Product Description Tab

add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
   unset( $tabs['description'] );
   return $tabs;
}

Change the Number of Related Products

add_filter( 'woocommerce_output_related_products_args', 'woo_related_products_limit' );
function woo_related_products_limit( $args ) {
   $args['posts_per_page'] = 3;
   return $args;
}

Disable Payment on Checkout Page

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//add the code to a a plugin file or template file
add_filter( ‘woocommerce_cart_needs_payment’, ‘__return_false’ );
//add the code to a a plugin file or template file add_filter( ‘woocommerce_cart_needs_payment’, ‘__return_false’ );
//add the code to a a plugin file or template file
add_filter( 'woocommerce_cart_needs_payment', '__return_false' );

Add Sort by Featured

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘custom_woocommerce_get_catalog_ordering_args’ );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET[‘orderby’] ) ? wc_clean( $_GET[‘orderby’] ) : apply_filters( ‘woocommerce_default_catalog_orderby’, get_option( ‘woocommerce_default_catalog_orderby’ ) );
if ( ‘featured’ == $orderby_value ) {
$args[‘orderby’] = ‘_featured’;
$args[‘order’] = ‘asc’;
$args[‘meta_key’] = ;
}
return $args;
}
add_filter( ‘woocommerce_default_catalog_orderby_options’, ‘custom_woocommerce_catalog_orderby’ );
add_filter( ‘woocommerce_catalog_orderby’, ‘custom_woocommerce_catalog_orderby’ );
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortbyfeatureed[‘featured’] = ‘Featured’;
$sortby = $sortbyfeatureed + $sortby;
return $sortby;
}
add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘custom_woocommerce_get_catalog_ordering_args’ ); function custom_woocommerce_get_catalog_ordering_args( $args ) { $orderby_value = isset( $_GET[‘orderby’] ) ? wc_clean( $_GET[‘orderby’] ) : apply_filters( ‘woocommerce_default_catalog_orderby’, get_option( ‘woocommerce_default_catalog_orderby’ ) ); if ( ‘featured’ == $orderby_value ) { $args[‘orderby’] = ‘_featured’; $args[‘order’] = ‘asc’; $args[‘meta_key’] = ”; } return $args; } add_filter( ‘woocommerce_default_catalog_orderby_options’, ‘custom_woocommerce_catalog_orderby’ ); add_filter( ‘woocommerce_catalog_orderby’, ‘custom_woocommerce_catalog_orderby’ ); function custom_woocommerce_catalog_orderby( $sortby ) { $sortbyfeatureed[‘featured’] = ‘Featured’; $sortby = $sortbyfeatureed + $sortby; return $sortby; }
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
  $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    if ( 'featured' == $orderby_value ) {
        $args['orderby'] = '_featured';
        $args['order'] = 'asc';
        $args['meta_key'] = '';
    }
    return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortbyfeatureed['featured'] = 'Featured';
    $sortby = $sortbyfeatureed + $sortby;
    return $sortby;
}

Change “Add to Cart” button text

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// To change add to cart text on single product page
add_filter( ‘woocommerce_product_single_add_to_cart_text’, ‘woocommerce_custom_single_add_to_cart_text’ );
function woocommerce_custom_single_add_to_cart_text() {
return __( ‘Add to Basket’, ‘woocommerce’ );
}
// To change add to cart text on single product page add_filter( ‘woocommerce_product_single_add_to_cart_text’, ‘woocommerce_custom_single_add_to_cart_text’ ); function woocommerce_custom_single_add_to_cart_text() { return __( ‘Add to Basket’, ‘woocommerce’ ); }
// To change add to cart text on single product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' ); 
function woocommerce_custom_single_add_to_cart_text() {
    return __( 'Add to Basket', 'woocommerce' ); 
}

Modify breadcrumb: remove Home item

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
add_filter(‘woocommerce_breadcrumb_defaults’, function( $defaults ) {
unset($defaults[‘home’]); //removes home link.
return $defaults;
});
add_filter(‘woocommerce_breadcrumb_defaults’, function( $defaults ) { unset($defaults[‘home’]); //removes home link. return $defaults; });
add_filter('woocommerce_breadcrumb_defaults', function( $defaults ) {
    unset($defaults['home']); //removes home link.    
    return $defaults;
});

Add product attributes to archive pages

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
add_action(‘woocommerce_after_shop_loop_item’, ‘my_theme_display_product_category’, 10);
function my_theme_display_product_category(){
global $product;
if ( $product->has_attributes() ) {
$colors = $product->get_attribute(‘pa_color’);
echo $colors;
}
}
add_action(‘woocommerce_after_shop_loop_item’, ‘my_theme_display_product_category’, 10); function my_theme_display_product_category(){ global $product; if ( $product->has_attributes() ) { $colors = $product->get_attribute(‘pa_color’); echo $colors; } }
add_action('woocommerce_after_shop_loop_item', 'my_theme_display_product_category', 10);
function my_theme_display_product_category(){
    global $product;
    if ( $product->has_attributes() ) {
        $colors = $product->get_attribute('pa_color');            
        echo $colors;
    }    
}

Check if current page is checkout or cart

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//check out page
if(is_checkout()){ }
//cart page
if(is_cart()){ }
//store page
if(is_shop()){ }
//check out page if(is_checkout()){ } //cart page if(is_cart()){ } //store page if(is_shop()){ }
//check out page
if(is_checkout()){ }
//cart page
if(is_cart()){ }
//store page
if(is_shop()){ }

Get WooCommerce Thumbnail Size

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//get all size names with their dimensions
global $_wp_additional_image_sizes;
//get width and height of woocommerce_thumbnail
$woo_image_size = $_wp_additional_image_sizes[‘woocommerce_thumbnail’][‘width’].‘x’.$_wp_additional_image_sizes[‘woocommerce_thumbnail’][‘height’];
//get all size names with their dimensions global $_wp_additional_image_sizes;

//get width and height of woocommerce_thumbnail $woo_image_size = $_wp_additional_image_sizes[‘woocommerce_thumbnail’][‘width’].’x’.$_wp_additional_image_sizes[‘woocommerce_thumbnail’][‘height’];

//get all size names with their dimensions
global $_wp_additional_image_sizes;

//get width and height of woocommerce_thumbnail $woo_image_size = $_wp_additional_image_sizes['woocommerce_thumbnail']['width'].'x'.$_wp_additional_image_sizes['woocommerce_thumbnail']['height'];

Remove or edit “[Product] has been added to your cart” message

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//remove
add_filter( ‘wc_add_to_cart_message_html’, ‘__return_null’ );
//remove add_filter( ‘wc_add_to_cart_message_html’, ‘__return_null’ );
//remove
add_filter( 'wc_add_to_cart_message_html', '__return_null' );

By continuing to use the site, you agree to the use of cookies.