Conditionally switching the blocks/tickets/extra-price template
-
Hi, I’m the author of WooCommerce Name Your Price which allows customers to enter their own price for products. I’ve had some folks inquire about selling Tickets using Name Your Price. And I used to have a bridge plugin that made it possible.
github.com/helgatheviking/woocommerce-name-your-price-event-tickets/
But as bridge plugins are sometimes likely to do, after enough updates on both sides the bridge plugin stopped working.
In revisiting this compatibility, it now looks like I need to replace the
tickets/blocks/tickets/extra-price.php
template in order to display the price input. I’ve found this code which allows me to override templates from a plugin:
https://gist.github.com/cliffordp/39e68939132bd0f483e0111972165455However, what I think makes the most sense, would be to conditionally override this template only when the ticket is in Name Your Price mode. This way regular priced tickets stay the same and I’m not responsible for keeping that template up to date.
Here’s what I have so far… targeting the
tribe_template_file
filter.public function __construct() { add_filter( 'tribe_template_file', array( $this, 'switch_price_template' ), 10, 3 ); } /** * Switch template for extra-price.php * * @param string $file Complete path to include the PHP File * @param array $name Template name * @param self $template Current instance of the Tribe__Template */ public function switch_price_template( $file, $name, $template ) { if ( is_array( $name ) ) { $name = implode( '/', $name ); } if ( 'blocks/tickets/extra-price' === $name ) { /** @var Tribe__Tickets__Tickets $provider */ $provider = $template->get( 'provider' ); $provider_class = $provider->class_name; if ( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' === $provider_class ) { /** @var Tribe__Tickets__Ticket_Object $ticket */ $ticket = $template->get( 'ticket' ); if ( WC_Name_Your_Price_Helpers::is_nyp( $ticket->ID ) ) { $file = WC_NYP_Tickets()->get_plugin_path() . '/templates/tickets/blocks/tickets/extra-price.php'; } } } return $file; }
Though I think what this means is my template cannot be overridden in themes? Maybe instead I should change the template’s name (if that’s possible) to something like
blocks/tickets/nyp-price
. Would love to hear some thoughts on this.
- The topic ‘Conditionally switching the blocks/tickets/extra-price template’ is closed to new replies.