How do we build a Shortcode for Wordpress?
Posted: Tue Mar 26, 2019 6:17 am
Code: Select all
// Note: products shortcode docs https://docs.woocommerce.com/document/woocommerce-shortcodes/#section-6
// In order for the snippet to work, you must add the parameter orderby = "stock" to the shortcode
// You can add more parameters to it of course e.g. limit, paginate, etc. It's all in the docs
add_filter('woocommerce_shortcode_products_query', 'bbloomer_sort_by_stock_status_shortcode', 999, 3);
function bbloomer_sort_by_stock_status_shortcode( $args, $atts, $type ) {
if ( $atts['orderby'] == "stock" ) {
$args['orderby'] = array( 'meta_value' => 'ASC' );
$args['meta_key'] = array('_stock_status';
}
return $args;
}
Trouble is, we want it to sort by Stock then by Title.
So the page ends up showing it in Alpha order, but with all the 'in stock' coming first.