WooCommerce hacks that still works
Some usefull snippets for expanding shop functionality.
WooCommerce is one of the most popular eCommerce platforms on the web. In this article I want to share my code snippets I use to extend WooCommerce functionality.
To start using this hacks you need to copy-paste a selected code into the functions.php file in your current theme folder.
Out of stock managment
Show out of stock products last (in categories):
class show_outofstock_last {
public function __construct() {
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
add_filter('posts_clauses', array($this, 'order_by_stock_status'), 2000);
}
}
public function order_by_stock_status($posts_clauses) {
global $wpdb;
if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag())) {
$posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
$posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
$posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
}
return $posts_clauses;
}
}
new show_outofstock_last;
Show recent products only in stock:
add_filter( 'woocommerce_shortcode_products_query', function( $query_args, $atts, $loop_name ){
if( $loop_name == 'recent_products' ){
$query_args['meta_query'] = array( array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT LIKE',
) );
}
return $query_args;
}, 10, 3);
Show related products only in stock:
function related_products_instock( $template_name, $template_path, $located, $args ) {
if( $template_name !== "single-product/related.php" ) {
return;
}
add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', function( $option ) { return "yes"; }, 10, 1 );
}
add_action( 'woocommerce_before_template_part', 'related_products_instock', 10, 4 );
Cart and checkout
Hide subtotal line in cart, checkout and emails:
add_filter( 'woocommerce_get_order_item_totals', 'adjust_woocommerce_get_order_item_totals' );
function adjust_woocommerce_get_order_item_totals( $totals ) {
unset($totals['cart_subtotal'] );
return $totals;
}
Set minimal total to complete order:
add_action( 'woocommerce_check_cart_items', 'set_min_total' );
function set_min_total() {
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set minimum cart total
$minimum_cart_total = 90;
$total = WC()->cart->subtotal;
$needed = $minimum_cart_total - $total;
if( $total <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( 'Minimal total is <strong>$%s</strong>!<br />You need to buy something on $%s to complete this order.',
$minimum_cart_total,
$needed ),
'error');
}
}
}
Other
Show label for free (zero-priced) products:
add_filter( 'woocommerce_get_price_html', 'product_price_free_zero_empty', 100, 2 );
function product_price_free_zero_empty( $price, $product ){
if ( '' === $product->get_price() || 0 == $product->get_price() ) {
$price = '<span class="woocommerce-Price-amount amount">Free</span>';
}
return $price;
}
Disable srcset and 768px images:
function unset_medium_large($sizes) {
unset($sizes['medium_large']);
return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'unset_medium_large');
add_filter('max_srcset_image_width', create_function('', 'return 1;'));
Enable WooCommerce new gallery support for old themes:
add_action( 'after_setup_theme', 'krutygolov_thumbs' );
function krutygolov_thumbs() {
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
I hope this snippets will be useful. If you get something similar or find a bug in this codes — please, reply to this article.