Last active
February 29, 2020 20:50
-
-
Save manospsyx/79e26897fcd91b6bc93c to your computer and use it in GitHub Desktop.
Use this snippet to sort Component Option Layered Filters by custom priority numbers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WooCommerce Composite Products - Sort Component Option Layered Filters by Priority | |
* Plugin URI: https://woocommerce.com/products/composite-products/ | |
* Description: Provides the ability to sort Component Option Filters by custom priority numbers. | |
* Version: 1.0 | |
* Author: SomewhereWarm | |
* Author URI: https://somewherewarm.gr/ | |
* Developer: Manos Psychogyiopoulos | |
* | |
* Requires at least: 3.8 | |
* Tested up to: 5.3 | |
* | |
* Copyright: © 2017-2020 SomewhereWarm SMPC ([email protected]). | |
* License: GNU General Public License v3.0 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
*/ | |
// To use this snippet, download this file into your plugins directory and activate it, or copy the code under this line into the functions.php file of your (child) theme. | |
add_action( 'woocommerce_composite_component_admin_config_filter_options', 'wc_cp_admin_filter_priorities', 10, 3 ); | |
add_filter( 'woocommerce_composite_process_component_data', 'wc_cp_admin_process_filter_priorities', 10, 4 ); | |
add_filter( 'woocommerce_composite_component_filters', 'wc_cp_sort_filters', 10, 3 ); | |
function wc_cp_admin_filter_priorities( $id, $data, $product_id ) { | |
$filters_priority = isset( $data[ 'filters_priority' ] ) ? $data[ 'filters_priority' ] : ''; | |
?> | |
<div class="filters_sorting_priority"> | |
<div class="form-field"> | |
<label><?php echo __( 'Sort Filters', 'woocommerce-composite-sorting-options' ); ?>:</label> | |
<input type="text" class="filters_sorting_priority" style="width:75%" name="bto_data[<?php echo $id; ?>][filters_priority]" value="<?php echo $filters_priority; ?>"/> | |
</div> | |
</div> | |
<?php | |
} | |
function wc_cp_admin_process_filter_priorities( $component_data, $posted_component_data, $component_id, $composite_id ) { | |
if ( isset( $posted_component_data[ 'filters_priority' ] ) ) { | |
$custom_sort_order = implode( WC_DELIMITER, array_map( 'wc_clean', explode( WC_DELIMITER, $posted_component_data[ 'filters_priority' ] ) ) ); | |
$component_data[ 'filters_priority' ] = $custom_sort_order; | |
} | |
return $component_data; | |
} | |
function wc_cp_sort_filters( $filters, $component_id, $product ) { | |
$component_data = $product->get_component_data( $component_id ); | |
$custom_sort_order_data = isset( $component_data[ 'filters_priority' ] ) ? $component_data[ 'filters_priority' ] : ''; | |
if ( $custom_sort_order_data ) { | |
$sorted_filters = array(); | |
$unsorted_filters = array(); | |
$custom_sort_order = array_map( 'wc_clean', explode( WC_DELIMITER, $custom_sort_order_data ) ); | |
foreach ( array_values( $filters ) as $loop => $data ) { | |
if ( isset( $custom_sort_order[ $loop ] ) ) { | |
$sorted_filters[ $custom_sort_order[ $loop ] ] = $data; | |
} else { | |
$unsorted_filters[] = $data; | |
} | |
} | |
ksort( $sorted_filters ); | |
$sorted_filters = array_merge( $sorted_filters, $unsorted_filters ); | |
return $sorted_filters; | |
} | |
return $filters; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment