SOLVED! - Using get_template_part() in Sage 10 with Blade

Hi guys, im updating one of my sites to sage 10 (from 9) and one thing that worked before is not working anymore…

i was adding an action to include a blade template in some woocommerce hooks using the following code:

add_action( 'woocommerce_after_add_to_cart_form' , 'variations_list' , 8 );
add_action( 'woocommerce_product_thumbnails' , 'variations_list' , 8 );


function variations_list() {
     return get_template_part('partials.variations_table');
}

and that totally worked in sage 9. the template was added in those 2 parts from my product. But now when i try to use the same code, it just returns an empty string

after reviewing some code in the forum i found that you could do something like

include template_path(locate_template ('resources/views/partials/variations_table.blade.php'));

but when i do that, i get this error.
Fatal error:
Uncaught Symfony\Component\Debug\Exception\FatalThrowableError:
Call to undefined function template_path()

Also if I namespace the file

namespace App;

the error changes to:

( ! ) Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘variations_list’ not found or invalid function name

and returns nothing… also other functions on the same file fail with similar errors. please notice i’m not a hardcore developer and i’m just a guy who hacks around into sage’s code…

thanks for your help

You’re looking for Roots\view. I’m not sure what output the WooCommerce filters expect, but more or less you want something like:

<?php

use function Roots\view;
use function Roots\add_filters;

add_filters([
    'woocommerce_after_add_to_cart_form',
    'woocommerce_product_thumbnails'
], function () {
    return view('partials.variations-table')->render();
});

(you don’t have to use add_filters())

2 Likes

@Log1x thanks for the code, this one at least is not throwing me errors, but still not showing anything. it dies silently… how can i debug this?

try echoing it instead

love you man!!! 3 days invested in this little snippet and its finally working :smile:

1 Like

so, to recapitulate, if anyone needs to use get_template_part() in sage 10, you instead need to use it as the following example:

    use function Roots\view;
    use function Roots\add_filters;
    add_filters([
        'woocommerce_after_add_to_cart_form',
        'woocommerce_product_thumbnails'
    ], function () {
        echo view('partials.YOUR-PARTIAL-NAME')->render();
    });

thanks to @Log1x for the solution!

7 Likes

This topic was automatically closed after 42 days. New replies are no longer allowed.