Ecommerce Shopify WordPress Discussion

Problem with Woocommerce and orderby "On Sale"

I've been testing for a few hours codes to filter the On Sale products of the online store (Woocommerce) and in all I've tried it filters me well in the parent category but in the subcategories only shows me the offers and does not show me all the products (making the offers are the first). I can't find a solution and I don't know if anyone has encountered this problem. I paste some codes that I have tried and have worked but only in the categories, in the subcategories happens what I said above: Code1: add_filter( 'woocommerce_get_catalog_ordering_args', 'wcs_get_catalog_ordering_args' ); function wcs_get_catalog_ordering_args( $args ) { $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ); if ( 'on_sale' == $orderby_value ) { $args['orderby'] = 'meta_value_num'; $args['order'] = 'DESC'; $args['meta_key'] = '_sale_price'; } return $args; } add_filter( 'woocommerce_default_catalog_orderby_options', 'wcs_catalog_orderby' ); add_filter( 'woocommerce_catalog_orderby', 'wcs_catalog_orderby' ); function wcs_catalog_orderby( $sortby ) { $sortby['on_sale'] = 'Sort by on sale'; return $sortby; } Code2: function custom_default_product_orderby_option($defaults) { $defaults['default_orderby'] = array( 'default' => 'on_sale', 'label' => __('Orden por defecto', 'textdomain'), 'type' => 'select', 'choices' => array( 'on_sale' => __('Productos en oferta', 'textdomain'), 'menu_order' => __('Orden del menú', 'textdomain'), 'popularity' => __('Popularidad', 'textdomain'), 'rating' => __('Valoración media', 'textdomain'), 'date' => __('Más recientes', 'textdomain'), 'price' => __('Precio: de menor a mayor', 'textdomain'), 'price-desc' => __('Precio: de mayor a menor', 'textdomain'), ), ); return $defaults; } add_filter('woocommerce_default_catalog_orderby_options', 'custom_default_product_orderby_option'); add_filter('woocommerce_catalog_orderby', 'custom_default_product_orderby_option'); function custom_default_product_order($query) { if (is_admin() || !$query->is_main_query()) { return; } $orderby = $query->get('orderby'); if ('on_sale' === $orderby) { $query->set('orderby', 'meta_value_num date'); $query->set('order', 'DESC'); $query->set('meta_key', '_sale_price'); $query->set('meta_query', array( 'relation' => 'OR', array( 'key' => '_sale_price', 'compare' => 'EXISTS', ), array( 'key' => '_sale_price', 'compare' => 'NOT EXISTS', ), )); } } add_action('pre_get_posts', 'custom_default_product_order'); I hope you can help me, thanks and best regards. Let's see if anyone else has had this happen and can help me to find the code that works well so that it can also be seen in the subcategories.
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.