Skip to content

Commit c4b5f39

Browse files
authored
Merge pull request #49 from Vrishabhsk/fix/VJ-36-weird-chars-prod
Fix : Free Tickets [ PROD ]
2 parents 3a90138 + cd09b43 commit c4b5f39

1 file changed

Lines changed: 71 additions & 18 deletions

File tree

themes/osi/inc/sugar-calendar.php

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
exit;
99
}
1010

11+
use Sugar_Calendar\AddOn\Ticketing\Common\Functions as Functions;
12+
use Sugar_Calendar\AddOn\Ticketing\Frontend\Single as Single;
13+
1114
/**
1215
* Add date & time details to event contents.
1316
* A modified version of the function sc_add_date_time_details() of the plugin Sugar Calendar.
1417
*
15-
* @param int $post_id
18+
* @param integer $post_id Post ID.
19+
* @param boolean $show_time Show time.
20+
*
21+
* @return void
1622
*/
17-
function osi_add_date_time_details( $post_id = 0, $show_time = true ) {
23+
function osi_add_date_time_details( int $post_id = 0, bool $show_time = true ) {
1824

1925
// Support 1.x
2026
$event = sugar_calendar_get_event_by_object( $post_id );
@@ -158,11 +164,11 @@ function osi_add_date_time_details( $post_id = 0, $show_time = true ) {
158164
/**
159165
* Display ticketing information.
160166
*
161-
* @param int $post_id
167+
* @return void
162168
*/
163169
function osi_maybe_display_ticketing() {
170+
remove_action( 'sc_after_event_content', 'Sugar_Calendar\AddOn\Ticketing\Frontend\Single\display' );
164171
if ( ! is_single() ) {
165-
remove_action( 'sc_after_event_content', 'Sugar_Calendar\AddOn\Ticketing\Frontend\Single\display' );
166172
remove_action( 'sc_event_details', 'Sugar_Calendar\AddOn\Events\URLs\Theme\Singular\display' );
167173
}
168174
}
@@ -171,12 +177,32 @@ function osi_maybe_display_ticketing() {
171177
/**
172178
* Add the registration button after the content in the event listing page.
173179
*
174-
* @param int $post_id
180+
* @param integer $post_id The ID of the post.
181+
*
175182
* @return void
176183
*/
177-
function osi_register_button_after_content( $post_id ) {
184+
function osi_register_button_after_content( int $post_id ) {
178185

186+
// For single posts, if ticket is priced at $0.0, display 'Free Event'.
187+
// And Button text 'Get a ticket' -> 'Register'
179188
if ( is_single() ) {
189+
// Capture current sugar-calendar output
190+
ob_start();
191+
192+
Single\display( $post_id );
193+
194+
$event_tickets_html = ob_get_clean();
195+
196+
// translators: %s is a placeholder for the currency amount.
197+
$price_html = sprintf( esc_html__( '%s Per Ticket', 'sc-event-ticketing' ), Functions\currency_filter( 0.0 ) );
198+
199+
// Replace amount with "Free Event"
200+
if ( false !== strpos( $event_tickets_html, $price_html ) ) {
201+
$event_tickets_html = str_replace( $price_html, esc_html__( 'Free Event', 'osi' ), $event_tickets_html );
202+
}
203+
204+
echo $event_tickets_html; // phpcs:ignore WordPress.Security.EscapeOutput
205+
180206
return;
181207
}
182208

@@ -208,11 +234,11 @@ function osi_register_button_after_content( $post_id ) {
208234
/**
209235
* Check if the event is past.
210236
*
211-
* @param integer $post_id
237+
* @param integer $post_id Post ID.
238+
*
212239
* @return boolean
213240
*/
214-
function osi_is_past_event( $post_id ) {
215-
241+
function osi_is_past_event( int $post_id ) {
216242
$is_past_event = false;
217243

218244
$event = sugar_calendar_get_event_by_object( $post_id );
@@ -230,16 +256,16 @@ function osi_is_past_event( $post_id ) {
230256
}
231257

232258
return $is_past_event;
233-
234259
}
235260

236261
/**
237262
* Add location details to event contents.
238263
*
239-
* @param int $post_id
264+
* @param integer $post_id Post ID.
265+
*
266+
* @return void
240267
*/
241-
function osi_add_location_details( $post_id = 0 ) {
242-
268+
function osi_add_location_details( int $post_id = 0 ) {
243269
$location = sugar_calendar_get_event_by_object( $post_id, 'post' )->location;
244270

245271
// Maybe add location
@@ -261,15 +287,42 @@ function osi_add_location_details( $post_id = 0 ) {
261287
/**
262288
* Add the purchase button to the event details.
263289
*
264-
* @param string $button_html
265-
* @param object $event
290+
* @param string $button_html Button HTML.
291+
* @param object $event Event object.
292+
*
266293
* @return string
267294
*/
268-
function osi_purchase_button_html( $button_html, $event ) {
295+
function osi_purchase_button_html( string $button_html, object $event ) {
269296

270-
$button_html = str_replace( 'Add to Cart', 'Get a ticket', $button_html );
297+
// Get the ticket price.
298+
$ticket_price = get_event_meta( $event->id, 'ticket_price', true );
271299

272-
return $button_html;
300+
// If the price is $0.0, Button text should be replace by 'Register'.
301+
if ( 0.0 === (float) $ticket_price ) {
302+
$button_html = str_replace( 'Add to Cart', __( 'Register', 'osi' ), $button_html );
303+
}
304+
305+
$button_html = str_replace( 'Add to Cart', __( 'Get a ticket', 'osi' ), $button_html );
273306

307+
return $button_html;
274308
}
275309
add_filter( 'sc_et_purchase_button_html', 'osi_purchase_button_html', 10, 2 );
310+
311+
/**
312+
* Generates default event metadata if ticket price is empty.
313+
*
314+
* @param mixed $value The value of the metadata.
315+
* @param integer $object_id The ID of the object.
316+
* @param string $meta_key The key of the metadata.
317+
*
318+
* @return mixed The modified or original value.
319+
*/
320+
function osi_default_event_metadata( mixed $value, int $object_id, string $meta_key ) {
321+
if ( 'ticket_price' === $meta_key && '' === $value ) {
322+
$value = 0.0;
323+
}
324+
325+
return $value;
326+
}
327+
328+
add_filter( 'default_sc_event_metadata', 'osi_default_event_metadata', 20, 3 );

0 commit comments

Comments
 (0)