Created
November 5, 2024 08:47
-
-
Save tivuno/bb96d0483ae1fa78c59701b36041d759 to your computer and use it in GitHub Desktop.
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 | |
add_action( 'rest_api_init', function () { | |
function set_custom_sorting( $response ) { | |
//var_dump([rand()]); | |
$response->data['custom_order'] = (int) get_term_meta($response->data['id'], 'order', true); | |
return $response; | |
} | |
add_filter( 'woocommerce_rest_prepare_product_attribute_term', 'set_custom_sorting', 10, 1); | |
add_filter( | |
'woocommerce_rest_product_attribute_term_collection_params', | |
function( $params ) { | |
$fields = ["custom_order"]; | |
foreach ($fields as $key => $value) { | |
$params['orderby']['enum'][] = $value; | |
} | |
return $params; | |
}, | |
10, | |
2 | |
); | |
// Εδώ κάπου χάθηκα | |
add_filter( | |
'__woocommerce_rest_product_attribute_term_query', | |
function ( $args, $request ) { | |
$fields = ["custom_order"]; | |
$order_by = $request->get_param( 'orderby' ); | |
if ( isset( $order_by ) && in_array($order_by, $fields)) { | |
$args['meta_key'] = $order_by; | |
$args['orderby'] = 'meta_value_num'; // user 'meta_value_num' for numerical fields | |
} | |
return $args; | |
}, | |
10, | |
3 | |
); | |
}); // rest_api_init |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment