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

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