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

247 lines 9.0 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 // Per-instance card layout override. 'inherit' (default) = use the
32 // site-wide Settings → Design "Listing Card Layout".
33 // Also accepts: standard | compact_mobile | compact_all.
34 'card_layout' => 'inherit'
35 ]);
36 }
37
38 /**
39 * Render the discount and deals shortcode content
40 */
41 protected function renderContent(array $atts): string
42 {
43 $atts = shortcode_atts($this->default_attributes, $atts, $this->tag);
44
45 // Extract per_page from attributes (only per_page parameter)
46 $per_page = 10; // default
47 if (!empty($atts['per_page']) && is_numeric($atts['per_page'])) {
48 $per_page = (int) $atts['per_page'];
49 }
50 $atts['per_page'] = $per_page;
51
52 // Get trips using Yatra's service (same as TripShortcode)
53 $trips_data = $this->getTrips($atts);
54
55 // Prepare data for template (match expected structure - same as TripShortcode)
56 $data = [
57 'trips' => [
58 'trips' => $trips_data['trips'] ?? [],
59 'max_pages' => $trips_data['max_pages'] ?? 1,
60 'current_page' => $trips_data['current_page'] ?? 1,
61 'total_found' => $trips_data['total_found'] ?? 0
62 ],
63 'atts' => $atts,
64 'max_pages' => $trips_data['max_pages'] ?? 1,
65 'current_page' => $trips_data['current_page'] ?? 1,
66 'total_found' => $trips_data['total_found'] ?? 0,
67 'per_page' => $per_page
68 ];
69
70 $tripShortcodeCssPath = YATRA_PLUGIN_PATH . 'assets/css/shortcodes/trip-shortcode.css';
71 $tripShortcodeCssVer = is_readable($tripShortcodeCssPath) ? YATRA_VERSION . '.' . filemtime($tripShortcodeCssPath) : YATRA_VERSION;
72 wp_enqueue_style(
73 'yatra-trip-shortcode',
74 YATRA_PLUGIN_URL . 'assets/css/shortcodes/trip-shortcode.css',
75 \Yatra\Providers\FrontendAssetsProvider::shortcodeStyleDependencies(),
76 $tripShortcodeCssVer
77 );
78
79 wp_enqueue_script(
80 'yatra-trip-shortcode',
81 YATRA_PLUGIN_URL . 'assets/js/trip-shortcode.js',
82 ['jquery'],
83 YATRA_VERSION,
84 true
85 );
86
87 // Pass data to JavaScript (same as trip shortcode)
88 wp_localize_script('yatra-trip-shortcode', 'yatraTripShortcode', [
89 'ajaxurl' => admin_url('admin-ajax.php'),
90 'nonce' => wp_create_nonce('yatra_trip_shortcode_nonce')
91 ]);
92
93 return $this->loadTemplate('shortcodes/trip.php', $data);
94 }
95
96 /**
97 * Get trips using Yatra's service (same as TripShortcode)
98 */
99 public function getTrips(array $atts): array
100 {
101 global $wpdb;
102
103 $tripsTable = \Yatra\Database\Tables\TripsTable::getTableName();
104 $limit = (int) $atts['per_page'];
105 $order = strtolower($atts['order']) === 'desc' ? 'DESC' : 'ASC';
106
107 // Build the query to get trips with discounts - more flexible
108 $query = "SELECT * FROM {$tripsTable}
109 WHERE status = 'publish'
110 AND (
111 (discounted_price > 0 AND discounted_price < original_price) OR
112 (sale_price > 0 AND sale_price < original_price) OR
113 (discounted_price > 0) OR
114 (sale_price > 0)
115 )";
116
117
118
119 // Add category filter if specified
120 if (!empty($atts['category'])) {
121 $tripClassificationsTable = \Yatra\Database\Tables\TripClassificationsTable::getTableName();
122 $query .= " AND id IN (
123 SELECT tc.trip_id
124 FROM {$tripClassificationsTable} tc
125 INNER JOIN {$wpdb->prefix}terms t ON tc.classification_id = t.term_id
126 WHERE tc.classification_type = 'category'
127 AND t.slug = %s
128 )";
129 }
130
131 // Add destination filter if specified
132 if (!empty($atts['destination'])) {
133 $tripClassificationsTable = \Yatra\Database\Tables\TripClassificationsTable::getTableName();
134 $query .= " AND id IN (
135 SELECT tc.trip_id
136 FROM {$tripClassificationsTable} tc
137 INNER JOIN {$wpdb->prefix}terms t ON tc.classification_id = t.term_id
138 WHERE tc.classification_type = 'destination'
139 AND t.slug = %s
140 )";
141 }
142
143 $query .= " ORDER BY created_at {$order} LIMIT {$limit}";
144
145 // Prepare the query with parameters
146 $params = [];
147 if (!empty($atts['category'])) {
148 $params[] = $atts['category'];
149 }
150 if (!empty($atts['destination'])) {
151 $params[] = $atts['destination'];
152 }
153
154 if (!empty($params)) {
155 $trips = $wpdb->get_results($wpdb->prepare($query, ...$params));
156 } else {
157 $trips = $wpdb->get_results($query);
158 }
159
160 // Debug: Log the results
161 if (defined('WP_DEBUG') && WP_DEBUG) {
162
163 if (!empty($trips)) {
164 foreach ($trips as $trip) {
165
166 }
167 }
168 }
169
170 // Process trips and calculate discount information
171 $processed_trips = [];
172 foreach ($trips as $trip) {
173 $trip_data = $this->processTripData($trip);
174 if ($trip_data['has_discount']) {
175 $processed_trips[] = (object) $trip_data;
176 }
177 }
178
179 // Debug: Log processed trips
180 if (defined('WP_DEBUG') && WP_DEBUG) {
181
182 if (!empty($processed_trips)) {
183 foreach ($processed_trips as $trip) {
184
185 }
186 }
187 }
188
189 // Return in same format as TripShortcode
190 return [
191 'trips' => $processed_trips,
192 'max_pages' => 1,
193 'current_page' => 1,
194 'total_found' => count($processed_trips)
195 ];
196 }
197
198 /**
199 * Process trip data and calculate discount information
200 */
201 private function processTripData($trip): array
202 {
203 // Use centralized TripPricingService for pricing and discount computation
204 $pricing = \Yatra\Services\TripPricingService::resolveDisplayPricing($trip);
205 $original_price = $pricing['original_price'];
206 $current_price = $pricing['current_price'];
207 $discountInfo = \Yatra\Services\TripPricingService::computeDiscount($original_price, $current_price);
208
209 $best_discount = null;
210 if ($discountInfo['has_discount']) {
211 $best_discount = [
212 'type' => 'percentage',
213 'value' => round((float) $discountInfo['discount_percentage'], 1),
214 'amount' => $discountInfo['discount_amount'],
215 'original_price' => $original_price,
216 'discounted_price' => $current_price
217 ];
218 }
219
220 // Generate permalink directly
221 $permalink = function_exists('yatra_get_trip_permalink')
222 ? yatra_get_trip_permalink($trip)
223 : home_url('/' . \Yatra\Services\SettingsService::getTripBase() . '/' . $trip->slug);
224
225 return [
226 'id' => $trip->id,
227 'title' => $trip->title,
228 'slug' => $trip->slug,
229 'description' => $trip->description ?? '',
230 'short_description' => $trip->short_description ?? '',
231 'original_price' => $original_price,
232 'discounted_price' => $discountInfo['has_discount'] ? $current_price : null,
233 'sale_price' => $discountInfo['has_discount'] ? $current_price : null,
234 'has_discount' => $discountInfo['has_discount'],
235 'best_discount' => $best_discount,
236 'current_price' => $current_price,
237 'featured_image' => $trip->featured_image,
238 'starting_location' => $trip->starting_location ?? '',
239 'duration_days' => $trip->duration_days ?? 0,
240 'duration_nights' => $trip->duration_nights ?? 0,
241 'difficulty_level' => $trip->difficulty_level,
242 'created_at' => $trip->created_at,
243 'permalink' => $permalink
244 ];
245 }
246 }
247