WC_Cart_Totals::sort_coupons_callback
Sort coupons so discounts apply consistently across installs.
In order of priority;
- sort param
- usage restriction
- application position for sequential discounts
- coupon value
- ID
Метод класса: WC_Cart_Totals{}
Хуков нет.
Возвращает
int.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->sort_coupons_callback( $a, $b );
Код WC_Cart_Totals::sort_coupons_callback() WC Cart Totals::sort coupons callback WC 11.3.0
protected function sort_coupons_callback( $a, $b ) {
if ( $a->sort === $b->sort ) {
if ( $a->get_limit_usage_to_x_items() === $b->get_limit_usage_to_x_items() ) {
$a_position = $this->coupon_application_positions[ spl_object_id( $a ) ] ?? null;
$b_position = $this->coupon_application_positions[ spl_object_id( $b ) ] ?? null;
// Equal positions must fall through to the amount fallback, so the inequality check stays outside the helper.
if ( null !== $a_position && null !== $b_position && $a_position !== $b_position ) {
return wc_uasort_comparison( $a_position, $b_position );
}
if ( $a->get_amount() === $b->get_amount() ) {
return $b->get_id() - $a->get_id();
}
return ( $a->get_amount() < $b->get_amount() ) ? -1 : 1;
}
return ( $a->get_limit_usage_to_x_items() < $b->get_limit_usage_to_x_items() ) ? -1 : 1;
}
return ( $a->sort < $b->sort ) ? -1 : 1;
}