WC_AJAX::product_ordering
Ajax request handling for product ordering.
Based on Simple Page Ordering by 10up (https://wordpress.org/plugins/simple-page-ordering/).
Method of the class: WC_AJAX{}
Hooks from the method
Returns
null. Nothing (null).
Usage
$result = WC_AJAX::product_ordering();
WC_AJAX::product_ordering() WC AJAX::product ordering code WC 10.5.0
public static function product_ordering() {
global $wpdb;
// phpcs:disable WordPress.Security.NonceVerification.Missing
if ( ! current_user_can( 'edit_products' ) || empty( $_POST['id'] ) ) {
wp_die( -1 );
}
$sorting_id = absint( $_POST['id'] );
$previd = absint( isset( $_POST['previd'] ) ? $_POST['previd'] : 0 );
$nextid = absint( isset( $_POST['nextid'] ) ? $_POST['nextid'] : 0 );
$menu_orders = wp_list_pluck( $wpdb->get_results( "SELECT ID, menu_order FROM {$wpdb->posts} WHERE post_type = 'product' ORDER BY menu_order ASC, post_title ASC" ), 'menu_order', 'ID' );
$index = 0;
foreach ( $menu_orders as $id => $menu_order ) {
$id = absint( $id );
if ( $sorting_id === $id ) {
continue;
}
if ( $nextid === $id ) {
++$index;
}
++$index;
$menu_orders[ $id ] = $index;
if ( $wpdb->update( $wpdb->posts, array( 'menu_order' => $index ), array( 'ID' => $id ) ) ) {
// We only need to clean the cache if the menu order was actually modified.
clean_post_cache( $id );
}
/**
* When a single product has gotten it's ordering updated.
* $id The product ID
* $index The new menu order
*/
do_action( 'woocommerce_after_single_product_ordering', $id, $index );
}
if ( isset( $menu_orders[ $previd ] ) ) {
$menu_orders[ $sorting_id ] = $menu_orders[ $previd ] + 1;
} elseif ( isset( $menu_orders[ $nextid ] ) ) {
$menu_orders[ $sorting_id ] = $menu_orders[ $nextid ] - 1;
} else {
$menu_orders[ $sorting_id ] = 0;
}
if ( $wpdb->update( $wpdb->posts, array( 'menu_order' => $menu_orders[ $sorting_id ] ), array( 'ID' => $sorting_id ) ) ) {
// We only need to clean the cache if the menu order was actually modified.
clean_post_cache( $sorting_id );
}
WC_Post_Data::delete_product_query_transients();
do_action( 'woocommerce_after_product_ordering', $sorting_id, $menu_orders );
wp_send_json( $menu_orders );
// phpcs:enable
}