PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.9
Yatra – Travel Booking & Tour Operator Software v3.0.2.9
3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 All 83 releases
yatra / app / Shortcodes / DiscountAndDealsShortcode.php

DiscountAndDealsShortcode.php in Yatra – Travel Booking & Tour Operator Software 3.0.2.9, at app/Shortcodes/DiscountAndDealsShortcode.php

242 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\Shortcodes;
6
7 /**
8 * Discount and Deals Shortcode
9 *
10 * Displays discounted tours and special deals using trip-listing-card.php template
11 */
12 class DiscountAndDealsShortcode extends BaseShortcode
13 {
14 public function __construct()
15 {
16 parent::__construct('yatra_discount_and_deals', [
17 'order' => 'asc',
18 'per_page' => '10',
19 'columns' => '3',
20 'discount_type' => 'all', // all, percentage, fixed, group
21 'min_discount' => '',
22 'max_discount' => '',
23 'category' => '',
24 'destination' => '',
25 'show_original_price' => 'yes',
26 'show_percentage' => 'yes',
27 'show_time_left' => 'yes',
28 'show_pagination' => 'yes',
29 'show_filters' => 'no',
30 'title' => 'Special Deals & Discounts'
31 ]);
32 }
33
34 /**
35 * Render the discount and deals shortcode content
36 */
37 protected function renderContent(array $atts): string
38 {
39 $atts = shortcode_atts($this->default_attributes, $atts, $this->tag);
40
41 // Extract per_page from attributes (only per_page parameter)
42 $per_page = 10; // default
43 if (!empty($atts['per_page']) && is_numeric($atts['per_page'])) {
44 $per_page = (int) $atts['per_page'];
45 }
46 $atts['per_page'] = $per_page;
47
48 // Get trips using Yatra's service (same as TripShortcode)
49 $trips_data = $this->getTrips($atts);
50
51 // Prepare data for template (match expected structure - same as TripShortcode)
52 $data = [
53 'trips' => [
54 'trips' => $trips_data['trips'] ?? [],
55 'max_pages' => $trips_data['max_pages'] ?? 1,
56 'current_page' => $trips_data['current_page'] ?? 1,
57 'total_found' => $trips_data['total_found'] ?? 0
58 ],
59 'atts' => $atts,
60 'max_pages' => $trips_data['max_pages'] ?? 1,
61 'current_page' => $trips_data['current_page'] ?? 1,
62 'total_found' => $trips_data['total_found'] ?? 0,
63 'per_page' => $per_page
64 ];
65
66 // Enqueue same CSS and JS as regular trip shortcode
67 wp_enqueue_style(
68 'yatra-trip-shortcode',
69 YATRA_PLUGIN_URL . 'assets/css/shortcodes/trip-shortcode.css',
70 [],
71 YATRA_VERSION
72 );
73
74 wp_enqueue_script(
75 'yatra-trip-shortcode',
76 YATRA_PLUGIN_URL . 'assets/js/trip-shortcode.js',
77 ['jquery'],
78 YATRA_VERSION,
79 true
80 );
81
82 // Pass data to JavaScript (same as trip shortcode)
83 wp_localize_script('yatra-trip-shortcode', 'yatraTripShortcode', [
84 'ajaxurl' => admin_url('admin-ajax.php'),
85 'nonce' => wp_create_nonce('yatra_trip_shortcode_nonce')
86 ]);
87
88 return $this->loadTemplate('shortcodes/trip.php', $data);
89 }
90
91 /**
92 * Get trips using Yatra's service (same as TripShortcode)
93 */
94 public function getTrips(array $atts): array
95 {
96 global $wpdb;
97
98 $tripsTable = \Yatra\Database\Tables\TripsTable::getTableName();
99 $limit = (int) $atts['per_page'];
100 $order = strtolower($atts['order']) === 'desc' ? 'DESC' : 'ASC';
101
102 // Build the query to get trips with discounts - more flexible
103 $query = "SELECT * FROM {$tripsTable}
104 WHERE status = 'publish'
105 AND (
106 (discounted_price > 0 AND discounted_price < original_price) OR
107 (sale_price > 0 AND sale_price < original_price) OR
108 (discounted_price > 0) OR
109 (sale_price > 0)
110 )";
111
112
113
114 // Add category filter if specified
115 if (!empty($atts['category'])) {
116 $tripClassificationsTable = \Yatra\Database\Tables\TripClassificationsTable::getTableName();
117 $query .= " AND id IN (
118 SELECT tc.trip_id
119 FROM {$tripClassificationsTable} tc
120 INNER JOIN {$wpdb->prefix}terms t ON tc.classification_id = t.term_id
121 WHERE tc.classification_type = 'category'
122 AND t.slug = %s
123 )";
124 }
125
126 // Add destination filter if specified
127 if (!empty($atts['destination'])) {
128 $tripClassificationsTable = \Yatra\Database\Tables\TripClassificationsTable::getTableName();
129 $query .= " AND id IN (
130 SELECT tc.trip_id
131 FROM {$tripClassificationsTable} tc
132 INNER JOIN {$wpdb->prefix}terms t ON tc.classification_id = t.term_id
133 WHERE tc.classification_type = 'destination'
134 AND t.slug = %s
135 )";
136 }
137
138 $query .= " ORDER BY created_at {$order} LIMIT {$limit}";
139
140 // Prepare the query with parameters
141 $params = [];
142 if (!empty($atts['category'])) {
143 $params[] = $atts['category'];
144 }
145 if (!empty($atts['destination'])) {
146 $params[] = $atts['destination'];
147 }
148
149 if (!empty($params)) {
150 $trips = $wpdb->get_results($wpdb->prepare($query, ...$params));
151 } else {
152 $trips = $wpdb->get_results($query);
153 }
154
155 // Debug: Log the results
156 if (defined('WP_DEBUG') && WP_DEBUG) {
157
158 if (!empty($trips)) {
159 foreach ($trips as $trip) {
160
161 }
162 }
163 }
164
165 // Process trips and calculate discount information
166 $processed_trips = [];
167 foreach ($trips as $trip) {
168 $trip_data = $this->processTripData($trip);
169 if ($trip_data['has_discount']) {
170 $processed_trips[] = (object) $trip_data;
171 }
172 }
173
174 // Debug: Log processed trips
175 if (defined('WP_DEBUG') && WP_DEBUG) {
176
177 if (!empty($processed_trips)) {
178 foreach ($processed_trips as $trip) {
179
180 }
181 }
182 }
183
184 // Return in same format as TripShortcode
185 return [
186 'trips' => $processed_trips,
187 'max_pages' => 1,
188 'current_page' => 1,
189 'total_found' => count($processed_trips)
190 ];
191 }
192
193 /**
194 * Process trip data and calculate discount information
195 */
196 private function processTripData($trip): array
197 {
198 // Use centralized TripPricingService for pricing and discount computation
199 $pricing = \Yatra\Services\TripPricingService::resolveDisplayPricing($trip);
200 $original_price = $pricing['original_price'];
201 $current_price = $pricing['current_price'];
202 $discountInfo = \Yatra\Services\TripPricingService::computeDiscount($original_price, $current_price);
203
204 $best_discount = null;
205 if ($discountInfo['has_discount']) {
206 $best_discount = [
207 'type' => 'percentage',
208 'value' => round((float) $discountInfo['discount_percentage'], 1),
209 'amount' => $discountInfo['discount_amount'],
210 'original_price' => $original_price,
211 'discounted_price' => $current_price
212 ];
213 }
214
215 // Generate permalink directly
216 $permalink = function_exists('yatra_get_trip_permalink')
217 ? yatra_get_trip_permalink($trip)
218 : home_url('/' . \Yatra\Services\SettingsService::getTripBase() . '/' . $trip->slug);
219
220 return [
221 'id' => $trip->id,
222 'title' => $trip->title,
223 'slug' => $trip->slug,
224 'description' => $trip->description ?? '',
225 'short_description' => $trip->short_description ?? '',
226 'original_price' => $original_price,
227 'discounted_price' => $discountInfo['has_discount'] ? $current_price : null,
228 'sale_price' => $discountInfo['has_discount'] ? $current_price : null,
229 'has_discount' => $discountInfo['has_discount'],
230 'best_discount' => $best_discount,
231 'current_price' => $current_price,
232 'featured_image' => $trip->featured_image,
233 'starting_location' => $trip->starting_location ?? '',
234 'duration_days' => $trip->duration_days ?? 0,
235 'duration_nights' => $trip->duration_nights ?? 0,
236 'difficulty_level' => $trip->difficulty_level,
237 'created_at' => $trip->created_at,
238 'permalink' => $permalink
239 ];
240 }
241 }
242