PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.8
Yatra – Travel Booking & Tour Operator Software v3.0.2.8
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.8, at app/Shortcodes/DiscountAndDealsShortcode.php

257 lines 9.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 // Debug: Log the query
113 if (defined('WP_DEBUG') && WP_DEBUG) {
114 error_log('DISCOUNT QUERY: ' . $query);
115 error_log('DISCOUNT ATTRS: ' . print_r($atts, true));
116 }
117
118 // Add category filter if specified
119 if (!empty($atts['category'])) {
120 $tripClassificationsTable = \Yatra\Database\Tables\TripClassificationsTable::getTableName();
121 $query .= " AND id IN (
122 SELECT tc.trip_id
123 FROM {$tripClassificationsTable} tc
124 INNER JOIN {$wpdb->prefix}terms t ON tc.classification_id = t.term_id
125 WHERE tc.classification_type = 'category'
126 AND t.slug = %s
127 )";
128 }
129
130 // Add destination filter if specified
131 if (!empty($atts['destination'])) {
132 $tripClassificationsTable = \Yatra\Database\Tables\TripClassificationsTable::getTableName();
133 $query .= " AND id IN (
134 SELECT tc.trip_id
135 FROM {$tripClassificationsTable} tc
136 INNER JOIN {$wpdb->prefix}terms t ON tc.classification_id = t.term_id
137 WHERE tc.classification_type = 'destination'
138 AND t.slug = %s
139 )";
140 }
141
142 $query .= " ORDER BY created_at {$order} LIMIT {$limit}";
143
144 // Prepare the query with parameters
145 $params = [];
146 if (!empty($atts['category'])) {
147 $params[] = $atts['category'];
148 }
149 if (!empty($atts['destination'])) {
150 $params[] = $atts['destination'];
151 }
152
153 if (!empty($params)) {
154 $trips = $wpdb->get_results($wpdb->prepare($query, ...$params));
155 } else {
156 $trips = $wpdb->get_results($query);
157 }
158
159 // Debug: Log the results
160 if (defined('WP_DEBUG') && WP_DEBUG) {
161 error_log('DISCOUNT RESULTS COUNT: ' . count($trips));
162 if (!empty($trips)) {
163 foreach ($trips as $trip) {
164 error_log('DISCOUNT TRIP: ' . print_r([
165 'id' => $trip->id,
166 'title' => $trip->title,
167 'original_price' => $trip->original_price,
168 'discounted_price' => $trip->discounted_price,
169 'sale_price' => $trip->sale_price
170 ], true));
171 }
172 }
173 }
174
175 // Process trips and calculate discount information
176 $processed_trips = [];
177 foreach ($trips as $trip) {
178 $trip_data = $this->processTripData($trip);
179 if ($trip_data['has_discount']) {
180 $processed_trips[] = (object) $trip_data;
181 }
182 }
183
184 // Debug: Log processed trips
185 if (defined('WP_DEBUG') && WP_DEBUG) {
186 error_log('PROCESSED DISCOUNT TRIPS COUNT: ' . count($processed_trips));
187 if (!empty($processed_trips)) {
188 foreach ($processed_trips as $trip) {
189 error_log('PROCESSED TRIP: ' . print_r([
190 'id' => $trip->id,
191 'title' => $trip->title,
192 'has_discount' => $trip->has_discount,
193 'best_discount' => $trip->best_discount
194 ], true));
195 }
196 }
197 }
198
199 // Return in same format as TripShortcode
200 return [
201 'trips' => $processed_trips,
202 'max_pages' => 1,
203 'current_page' => 1,
204 'total_found' => count($processed_trips)
205 ];
206 }
207
208 /**
209 * Process trip data and calculate discount information
210 */
211 private function processTripData($trip): array
212 {
213 // Use centralized TripPricingService for pricing and discount computation
214 $pricing = \Yatra\Services\TripPricingService::resolveDisplayPricing($trip);
215 $original_price = $pricing['original_price'];
216 $current_price = $pricing['current_price'];
217 $discountInfo = \Yatra\Services\TripPricingService::computeDiscount($original_price, $current_price);
218
219 $best_discount = null;
220 if ($discountInfo['has_discount']) {
221 $best_discount = [
222 'type' => 'percentage',
223 'value' => round((float) $discountInfo['discount_percentage'], 1),
224 'amount' => $discountInfo['discount_amount'],
225 'original_price' => $original_price,
226 'discounted_price' => $current_price
227 ];
228 }
229
230 // Generate permalink directly
231 $permalink = function_exists('yatra_get_trip_permalink')
232 ? yatra_get_trip_permalink($trip)
233 : home_url('/' . \Yatra\Services\SettingsService::getTripBase() . '/' . $trip->slug);
234
235 return [
236 'id' => $trip->id,
237 'title' => $trip->title,
238 'slug' => $trip->slug,
239 'description' => $trip->description ?? '',
240 'short_description' => $trip->short_description ?? '',
241 'original_price' => $original_price,
242 'discounted_price' => $discountInfo['has_discount'] ? $current_price : null,
243 'sale_price' => $discountInfo['has_discount'] ? $current_price : null,
244 'has_discount' => $discountInfo['has_discount'],
245 'best_discount' => $best_discount,
246 'current_price' => $current_price,
247 'featured_image' => $trip->featured_image,
248 'starting_location' => $trip->starting_location ?? '',
249 'duration_days' => $trip->duration_days ?? 0,
250 'duration_nights' => $trip->duration_nights ?? 0,
251 'difficulty_level' => $trip->difficulty_level,
252 'created_at' => $trip->created_at,
253 'permalink' => $permalink
254 ];
255 }
256 }
257