| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dynamic Pricing Service for Yatra Plugin |
| 4 |
* |
| 5 |
* Handles dynamic pricing calculations and filters |
| 6 |
* |
| 7 |
* @package Yatra\Services |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Yatra\Services; |
| 11 |
|
| 12 |
class DynamicPricingService |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Initialize dynamic pricing hooks |
| 16 |
*/ |
| 17 |
public static function init(): void |
| 18 |
{ |
| 19 |
// Enable dynamic pricing module |
| 20 |
add_filter('yatra_dynamic_pricing_enabled', '__return_true'); |
| 21 |
|
| 22 |
// Register pricing filters |
| 23 |
add_filter('yatra_availability_price', [self::class, 'applyDynamicPricing'], 10, 3); |
| 24 |
add_filter('yatra_trip_display_price', [self::class, 'applyDisplayPricing'], 10, 3); |
| 25 |
add_filter('yatra_booking_trip_price', [self::class, 'applyBookingPricing'], 10, 3); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Apply dynamic pricing logic |
| 30 |
* |
| 31 |
* @param float $price |
| 32 |
* @param int $trip_id |
| 33 |
* @param array $context |
| 34 |
* @return float |
| 35 |
*/ |
| 36 |
public static function applyDynamicPricing(float $price, int $trip_id, array $context): float |
| 37 |
{ |
| 38 |
// Debug logging |
| 39 |
$original_price = $price; |
| 40 |
|
| 41 |
// Apply basic dynamic pricing logic |
| 42 |
$departure_date = $context['departure_date'] ?? null; |
| 43 |
$spots_remaining = $context['spots_remaining'] ?? null; |
| 44 |
|
| 45 |
// Example: Increase price if less than 5 spots remaining (high demand) |
| 46 |
if ($spots_remaining !== null && $spots_remaining <= 5 && $spots_remaining > 0) { |
| 47 |
$price = $price * 1.15; // 15% increase for high demand |
| 48 |
} |
| 49 |
|
| 50 |
// Example: Early bird discount (more than 30 days in advance) |
| 51 |
if ($departure_date) { |
| 52 |
$days_until = (strtotime($departure_date) - time()) / (60 * 60 * 24); |
| 53 |
if ($days_until > 30) { |
| 54 |
$price = $price * 0.9; // 10% early bird discount |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return $price; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Apply dynamic pricing for trip display pages |
| 63 |
* |
| 64 |
* @param float $price |
| 65 |
* @param int $trip_id |
| 66 |
* @param array $context |
| 67 |
* @return float |
| 68 |
*/ |
| 69 |
public static function applyDisplayPricing(float $price, int $trip_id, array $context): float |
| 70 |
{ |
| 71 |
// Apply same logic for trip display pages |
| 72 |
return apply_filters('yatra_availability_price', $price, $trip_id, $context); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Apply dynamic pricing for booking calculations |
| 77 |
* |
| 78 |
* @param float $price |
| 79 |
* @param int $trip_id |
| 80 |
* @param array $context |
| 81 |
* @return float |
| 82 |
*/ |
| 83 |
public static function applyBookingPricing(float $price, int $trip_id, array $context): float |
| 84 |
{ |
| 85 |
// Apply same logic for booking calculations |
| 86 |
return apply_filters('yatra_availability_price', $price, $trip_id, $context); |
| 87 |
} |
| 88 |
} |
| 89 |
|