TheCartPress allows, to plugin developers, to add new sorting methods using three filters. Those filters allow to add new sorting methods or to remove the existing ones.
Those filters are:
- tcp_sorting_fields: Allows to add or delete the types of sorts.
- tcp_sort_query: Allows to sort the query for TCP shortcodes and Custom post type widgets.
- tcp_sort_main_loop: Allows to add a sorting method to execute in the main loop.
Example
The example is a plugin to add a new classification by Stock:
<?php
/*
Plugin Name: TheCartPress Order by Stock
Plugin URI: http://thecartpress.com
Description: Order by Stock for TheCartPress
Version: 1.1
Author: TheCartPress team
Author URI: http://thecartpress.com
License: GPL
Parent: thecartpress
*/
class OrderByStock {
function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
function init() {
add_filter( 'tcp_sorting_fields', array( $this, 'tcp_sorting_fields' ) );
if ( ! is_admin() ) {
add_filter( 'tcp_sort_main_loop', array( $this, 'tcp_sort_main_loop' ), 10, 3 );</span>
}
}
//adds the new sorting method: 'stock'
function tcp_sorting_fields( $sorting_fields ) {
$sorting_fields[] = array( 'value' => 'stock', 'title' => 'By Stock' );
return $sorting_fields;
}
private function compare_by_stock_asc( $s1, $s2 ) {</span>
$p1 = tcp_get_the_stock( $s1->ID );
$p2 = tcp_get_the_stock( $s2->ID );
$v = $p1 > $p2 ? 1 : ( $p1 < $p2 ? -1 : 0 );
return $v;
}
private function compare_by_stock_desc( $s1, $s2 ) {
$p1 = tcp_get_the_stock( $s1->ID );
$p2 = tcp_get_the_stock( $s2->ID );
$v = $p1 > $p2 ? -1 : ($p1 < $p2 ? 1 : 0 );
return $v;
}
function tcp_sort_main_loop( $query, $order_type, $order_desc ) {
if ( $order_type == 'stock' ) {
$query['orderby'] = 'meta_value_num';
$query['meta_key'] = 'tcp_stock';
//$query_string['order'] = $order_desc();
}
return $query;
}
}
new OrderByStock();
?>
Usage
Since 1.1
Related
tcp_the_sort_panel | tcp_get_sorting_fields | tcp_get_sorting_fields
