WC_AJAX::product_ordering │ public static │ WC 1.0
Ajax request handling for product ordering.
Метод класса: WC_AJAX{}
Хуки из метода
Возвращает
void. Ничего (null).
Использование
$result = WC_AJAX::product_ordering();
Код WC_AJAX::product_ordering() WC AJAX::product ordering WC 11.2.2
public static function product_ordering() {
global $wpdb;
check_ajax_referer( 'product-ordering', 'security' );
if ( ! current_user_can( 'edit_products' ) || empty( $_POST['id'] ) ) {
wp_die( -1 );
}
$previous_id = absint( $_POST['previd'] ?? 0 );
$product_id = absint( $_POST['id'] );
$next_id = absint( $_POST['nextid'] ?? 0 );
$has_per_product_hook = has_action( 'woocommerce_after_single_product_ordering' );
$has_post_ordering_hook = has_action( 'woocommerce_after_product_ordering' );
if ( $has_per_product_hook || $has_post_ordering_hook ) {
// See `clean_post_cache`, `wp_ajax_woocommerce_product_ordering`, `woocommerce_product_ordering_process_reindexed_products`
// and `woocommerce_product_ordering_process_moved_products` for available migration primitives.
if ( $has_per_product_hook ) {
wc_deprecated_hook( 'woocommerce_after_single_product_ordering', '11.2', null, 'Using this hook forces a non-optimized reordering path which causes performance issues on larger catalogs.' );
}
if ( $has_post_ordering_hook ) {
wc_deprecated_hook( 'woocommerce_after_product_ordering', '11.2', null, 'Using this hook forces a non-optimized reordering path which causes performance issues on larger catalogs.' );
}
// Based on Simple Page Ordering by 10up (https://wordpress.org/plugins/simple-page-ordering/).
$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 ( $product_id === $id ) {
continue;
}
if ( $next_id === $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 its ordering updated.
*
* @param int $id The product ID.
* @param int $index The new sort position.
*
* @since 3.1.0
*/
do_action( 'woocommerce_after_single_product_ordering', $id, $index );
}
if ( isset( $menu_orders[ $previous_id ] ) ) {
$menu_orders[ $product_id ] = $menu_orders[ $previous_id ] + 1;
} elseif ( isset( $menu_orders[ $next_id ] ) ) {
$menu_orders[ $product_id ] = $menu_orders[ $next_id ] - 1;
} else {
$menu_orders[ $product_id ] = 0;
}
if ( $wpdb->update( $wpdb->posts, array( 'menu_order' => $menu_orders[ $product_id ] ), array( 'ID' => $product_id ) ) ) {
// We only need to clean the cache if the menu order was actually modified.
clean_post_cache( $product_id );
}
WC_Post_Data::delete_product_query_transients();
/**
* When products ordering update completed.
*
* @param int $product_id The product ID that was repositioned.
* @param array<int,int> $all_positions All product sort positions (product ID → actual menu_order value).
*
* @since 3.1.0
*/
do_action( 'woocommerce_after_product_ordering', $product_id, $menu_orders );
wp_send_json( $menu_orders );
} else {
$modifications = wc_get_container()->get( ProductsOrderingMoveService::class )->move( $previous_id, $product_id, $next_id );
$moved = ! empty( $modifications->moved );
$reindexed = ! empty( $modifications->reindexed );
if ( $moved || $reindexed ) {
WC_Post_Data::delete_product_query_transients();
if ( $reindexed ) {
/**
* Fires after a full catalog reindex was triggered during product ordering.
*
* @param int $product_id The product ID that was repositioned.
* @param array<int,int> $reindexed Reindexed product positions (product ID → menu_order), excludes moved products.
*
* @since 11.2.0
*/
do_action( 'woocommerce_product_ordering_process_reindexed_products', $product_id, $modifications->reindexed );
unset( $modifications->reindexed );
}
if ( $moved ) {
/**
* Fires after products have been repositioned during product ordering.
*
* @param int $product_id The product ID that was repositioned.
* @param array<int,int> $moved Moved product positions (product ID → menu_order).
*
* @since 11.2.0
*/
do_action( 'woocommerce_product_ordering_process_moved_products', $product_id, $modifications->moved );
}
}
wp_send_json( $modifications->moved );
}
}