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 high-quality 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

Customization

//add the code to a a plugin file or template file
add_filter( 'woocommerce_cart_needs_payment', '__return_false' );
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;
}
// 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' ); 
}
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;
    }    
}
//remove
add_filter( 'wc_add_to_cart_message_html', '__return_null' );

Image

//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'];

Menu & Navigation

add_filter('woocommerce_breadcrumb_defaults', function( $defaults ) {
    unset($defaults['home']); //removes home link.    
    return $defaults;
});
//check out page
if(is_checkout()){ }
//cart page
if(is_cart()){ }
//store page
if(is_shop()){ }

Leave a Comment

Your email address will not be published.

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

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close