• Resolved HelgaTheViking

    (@helgatheviking)


    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/39e68939132bd0f483e0111972165455

    However, 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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Sky Shabatura

    (@skyshab)

    Hi Helga!

    I know you from a forum somewhere long ago. You have a handle that is easy to remember.

    My take on this: since this is a filter, if you add your logic in the callback and if the ticket is NOT a “name your own price” ticket, just return the original value that was passed in. It should load whatever file in the hierarchy (child-theme, theme, plugin) it would have otherwise.

    Is the code snippet you shared working for you? What happens if you add another version of the template in your child theme with a minor change? Is the template in the child theme not being picked up? With and without the filter snippet?

    Let me know if that helps!

    Best,
    Sky

    Thread Starter HelgaTheViking

    (@helgatheviking)

    Hi Sky!

    Yes, there are only a handful of Helga the Vikings out there! And I’ve had this handle for a long time.

    It’s been a while since I was dabbling with this so I don’t recall what happens with the code I wrote… or what happens if someone is overriding the extra-price template in their theme. Ideally, I only want to insert my template when that ticket is Name Your Price enabled. Though looking in the source the tribe_template_file filter does fire after the theme file is found.

    On twitter, it was suggested to me to use the tribe_template_html:tickets/blocks/tickets/extra-price filter like so (needs conditional logic still):

    
    function test_ticket(  $html, $file, $name, $template )
     {
     return 'Helga loves tacos';
    }
    add_filter( 'tribe_template_html:tickets/blocks/tickets/extra-price', 'test_ticket', 10, 4);
    

    Do you have a preference between these two approaches? Or is it 6 in one, a half-dozen in the other? 🙂

    Plugin Author Sky Shabatura

    (@skyshab)

    Hi again,

    I actually did not know about the “tribe_template_html:” filters, so that is great to learn.

    I tested both approaches, and it seems the same either way, as far as whether it overrides any user customizations of the same template in the child theme. I’m not clear if you are needing to keep the ability for users to override the template still. I would think you’d want to enforce your view when appropriate.

    I’m not overly experienced with either of these methods enough to have a preference to recommend, but it seems the “tribe_template_html:” filter is great for a small component without having to mess with template files, while the template filter might be preferable for a larger component with more html markup.

    Hope that helps! And thanks for the tip on that filter. I am personally working on integrating my theme with with the Events Calendar plugins right now, so that will come in handy.

    Thread Starter HelgaTheViking

    (@helgatheviking)

    I would think you’d want to enforce your view when appropriate.

    That’s correct! I also don’t want to interfere with with anyone using the template overrides for regular tickets.

    I have a function I can call to generate all the markup I need, so I may go with the filter and not introduce any additional templates.

    Good luck with your theme integration!

    Plugin Author Sky Shabatura

    (@skyshab)

    Hi again,

    Great! Sounds like the tribe_template_html” filter will work for your needs.

    Have a great week!

    Sky

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Conditionally switching the blocks/tickets/extra-price template’ is closed to new replies.