Last active
July 27, 2018 11:23
-
-
Save BurlesonBrad/e4a1376c6a63540f53a1aecec6bd3139 to your computer and use it in GitHub Desktop.
javier
This file contains 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
// Additional Code from Brad. // | |
// Provides the ability to sort Component Option Filters by custom priority numbers. // | |
// Added to Child X Theme functions.php file // | |
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