self::TYPE_PERCENTAGE_WITH_MINIMUM, ); /** * @return string[] */ public static function types() { return array( self::TYPE_FIXED, self::TYPE_PERCENTAGE, self::TYPE_PERCENTAGE_WITH_MINIMUM, self::TYPE_TIERED ); } /** * @param mixed $type Fee type. * @return string A known type, or percentage_with_minimum when unknown. */ public static function normalize_type( $type ) { $type = is_string( $type ) ? strtolower( trim( $type ) ) : ''; if ( isset( self::TYPE_ALIASES[ $type ] ) ) { $type = self::TYPE_ALIASES[ $type ]; } return in_array( $type, self::types(), true ) ? $type : self::TYPE_PERCENTAGE_WITH_MINIMUM; } /** * Cleans a tier list: numeric bounds, known tier types, non-negative values, sorted by min_order. * * @param mixed $tiers Tiers from the app. * @return array */ public static function sanitize_tiers( $tiers ) { if ( ! is_array( $tiers ) ) { return array(); } $clean = array(); foreach ( $tiers as $tier ) { if ( ! is_array( $tier ) || ! isset( $tier['value'] ) || ! is_numeric( $tier['value'] ) ) { continue; } $type = isset( $tier['type'] ) && self::TYPE_PERCENTAGE === $tier['type'] ? self::TYPE_PERCENTAGE : self::TYPE_FIXED; $max = isset( $tier['max_order'] ) && is_numeric( $tier['max_order'] ) ? max( 0.0, (float) $tier['max_order'] ) : null; $clean[] = array( 'min_order' => isset( $tier['min_order'] ) && is_numeric( $tier['min_order'] ) ? max( 0.0, (float) $tier['min_order'] ) : 0.0, 'max_order' => $max, 'type' => $type, 'value' => max( 0.0, (float) $tier['value'] ), 'minimum' => isset( $tier['minimum'] ) && is_numeric( $tier['minimum'] ) ? max( 0.0, (float) $tier['minimum'] ) : 0.0, ); if ( count( $clean ) >= 50 ) { break; } } usort( $clean, static function ( $a, $b ) { return $a['min_order'] <=> $b['min_order']; } ); return $clean; } /** * Builds tiers from the single-value settings older plugin versions stored. * * @param string $type Fee type. * @param float $percentage Percentage of the subtotal. * @param float $base Fixed amount, or the minimum for percentage_with_minimum. * @return array */ public static function tiers_from_simple( $type, $percentage, $base ) { $type = self::normalize_type( $type ); $percentage = max( 0.0, (float) $percentage ); $base = max( 0.0, (float) $base ); switch ( $type ) { case self::TYPE_FIXED: return array( array( 'min_order' => 0.0, 'max_order' => null, 'type' => self::TYPE_FIXED, 'value' => $base, 'minimum' => 0.0 ) ); case self::TYPE_PERCENTAGE: return array( array( 'min_order' => 0.0, 'max_order' => null, 'type' => self::TYPE_PERCENTAGE, 'value' => $percentage, 'minimum' => 0.0 ) ); default: return array( array( 'min_order' => 0.0, 'max_order' => null, 'type' => self::TYPE_PERCENTAGE, 'value' => $percentage, 'minimum' => $base ) ); } } /** * The fee for an order amount. * * @param float $amount Order amount the fee is based on (cart subtotal). * @param array $tiers Sanitized tiers. * @param int $decimals Currency decimals. * @return float */ public static function calculate( $amount, array $tiers, $decimals = 2 ) { $amount = max( 0.0, (float) $amount ); if ( empty( $tiers ) ) { return 0.0; } $applicable = null; foreach ( $tiers as $tier ) { $min = isset( $tier['min_order'] ) ? (float) $tier['min_order'] : 0.0; $max = isset( $tier['max_order'] ) && null !== $tier['max_order'] ? (float) $tier['max_order'] : PHP_FLOAT_MAX; if ( $amount >= $min && $amount < $max ) { $applicable = $tier; break; } } // Above every range: the last tier applies (same as the app). if ( null === $applicable ) { $applicable = end( $tiers ); } $value = isset( $applicable['value'] ) ? max( 0.0, (float) $applicable['value'] ) : 0.0; $minimum = isset( $applicable['minimum'] ) ? max( 0.0, (float) $applicable['minimum'] ) : 0.0; if ( isset( $applicable['type'] ) && self::TYPE_PERCENTAGE === $applicable['type'] ) { $fee = $amount * ( $value / 100 ); if ( $minimum > 0 && $fee < $minimum ) { $fee = $minimum; } } else { $fee = $value; } return round( $fee, max( 0, (int) $decimals ) ); } }