{"id":54392,"date":"2017-08-28T15:50:31","date_gmt":"2017-08-28T18:50:31","guid":{"rendered":"https:\/\/wiki.samuelchopard.ch\/?p=54392"},"modified":"2019-03-18T14:29:21","modified_gmt":"2019-03-18T13:29:21","slug":"order-products-by-one-sale","status":"publish","type":"post","link":"https:\/\/samuelchopard.ch\/wiki\/library\/order-products-by-one-sale\/","title":{"rendered":"Order products by &#8220;One Sale&#8221;"},"content":{"rendered":"<p><!--more--><\/p>\n<p>In your theme&#8217;s <em>functions.php<\/em>, add the following code:<\/p>\n<pre class=\"EnlighterJSRAW\">&lt;?php\nadd_filter( 'woocommerce_get_catalog_ordering_args', 'wcs_get_catalog_ordering_args' );\nfunction wcs_get_catalog_ordering_args( $args ) {\n  \n  $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );\n    if ( 'on_sale' == $orderby_value ) {\n      \n        $args['orderby'] = 'meta_value_num';\n        $args['order'] = 'DESC';\n        $args['meta_key'] = '_sale_price';\n        \n    }\n    return $args;\n    \n}\nadd_filter( 'woocommerce_default_catalog_orderby_options', 'wcs_catalog_orderby' );\nadd_filter( 'woocommerce_catalog_orderby', 'wcs_catalog_orderby' );\nfunction wcs_catalog_orderby( $sortby ) {\n  \n    $sortby['on_sale'] = 'On Sale';\n    return $sortby;\n    \n}<\/pre>\n<p><strong>Note:<\/strong>\u00a0you can replace the\u00a0<em>On Sale <\/em>text<em>.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Source:\u00a0<a target=\"_blank\" href=\"https:\/\/metorik.com\/blog\/order-products-by-on-sale-in-woocommerce\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/metorik.com\/blog\/order-products-by-on-sale-in-woocommerce<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"In your theme's functions.php, add the following code: &lt;?php add_filter( 'woocommerce_get_catalog_ordering_args', 'wcs_get_catalog_ordering_args' ); function wcs_get_catalog_ordering_args( $args ) { $orderby_value = [...]","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4,8],"tags":[108,195,196,235,236,256,275,327],"class_list":["post-54392","post","type-post","status-publish","format-standard","hentry","category-php-tricks","category-plugins","category-woocommerce","tag-filter","tag-one-sale","tag-order","tag-promo","tag-promotion","tag-sale","tag-sort","tag-woocommerce"],"_links":{"self":[{"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/posts\/54392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/comments?post=54392"}],"version-history":[{"count":1,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/posts\/54392\/revisions"}],"predecessor-version":[{"id":54871,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/posts\/54392\/revisions\/54871"}],"wp:attachment":[{"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/media?parent=54392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/categories?post=54392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/tags?post=54392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}