Automattic\WooCommerce\Internal\Products
ProductsOrderingMoveService::compose_move_map
Computes the target position and the range of products that must shift to accommodate the move.
Метод класса: ProductsOrderingMoveService{}
Хуков нет.
Возвращает
object{ product_id:int, old_position:int, new_position:int, range_from:int, range_to:int, range_delta:int }.
Использование
// private - только в коде основоного (родительского) класса $result = $this->compose_move_map( $previous_id, $product_id, $next_id, $anchor_positions ): object;
- $previous_id(int) (обязательный)
- ID of the product immediately before the target position, or 0 when moving to the start.
- $product_id(int) (обязательный)
- ID of the product being repositioned.
- $next_id(int) (обязательный)
- ID of the product immediately after the target position, or 0 when moving to the end.
- $anchor_positions(array<int,int>) (обязательный)
- Map of product ID → current menu_order for the three anchor products.
Код ProductsOrderingMoveService::compose_move_map() ProductsOrderingMoveService::compose move map WC 11.2.2
private function compose_move_map( int $previous_id, int $product_id, int $next_id, array $anchor_positions ): object {
$previous_position = (int) ( $anchor_positions[ $previous_id ] ?? 0 );
$product_position = (int) ( $anchor_positions[ $product_id ] ?? 0 );
$next_position = (int) ( $anchor_positions[ $next_id ] ?? 0 );
if ( $previous_position > $product_position ) {
// Moving forward: products between current and new position shift down.
$range_from = $product_position + 1;
$range_to = $previous_position;
$range_delta = -1;
$new_position = $previous_position;
} else {
// Moving backward: products between new and current position shift up.
$range_from = $next_position;
$range_to = $product_position - 1;
$range_delta = +1;
$new_position = $next_position;
}
return (object) array(
'product_id' => $product_id,
'old_position' => $product_position,
'new_position' => $new_position,
'range_from' => $range_from,
'range_to' => $range_to,
'range_delta' => $range_delta,
);
}