Roots Discourse

Include partials from a hook

We are currently developing a new WooCommerce theme based on Sage 9.

We always use hooks to change the WooCommerce pages. We use a separate php file in (located in /app) to keep everything tidy. Right now I want to add a partial to the single product page. We haven’t figured out a way to include a blade file.

We tried it with the following code, but also with require_once and include. However, the result is that the blade is spit out as text.

add_action('woocommerce_after_single_product', function() { get_template_part( 'partials/section', 'awards.blade' ); }, 10);

Any ideas how to tackle this?

This should return the html.

App\template('partials.section-awards');
1 Like

Thank you @KimH.

For future reference the final code is:
add_action('woocommerce_after_single_product', function() { echo App\template('partials.section-awards'); }, 10);

2 Likes

Thank you @KimH,

Your snippet works fine if the partial template does not require a parameter, i have tried passing parameters to the partial but it does not work,

add_action('woocommerce_after_single_product', function() { echo App\template('partials.section-awards' , [$*my_param*]); }, 10);
Anyone have a clue please ?

What is the source of the parameter you’re trying to pass? In your example there is nothing else I’m the scope of the anonymous function passed to the filter, which is the only scope that App\template() has access to.

1 Like
In my case, i pass  a value that i retrieve from ACF options page :
  add_action( 'woocommerce_after_main_content', function () {
       $warranties = get_field( 'warranties', 'option' );
       echo App\template( 'partials.sections.warranties', [ $warranties ] );
} );

The template function expects a keyed array because it uses the array keys to create the variable names in the Blade context.

1 Like

It works! Thank you a lot! :relaxed: