Hi guys,
Since last week I’ve been building a Woocommerce store in sage 9.
After a few hours of search I found out how to override woocommerce.php/single-product.php and archive-product.php - Those 3 are working properly - don’t know if this is the best way… but is working.
But when I try to use the same logic to replace some template parts like content-single-product.php to content-single-product.blade.php my single-product just duplicates my product display.
My set-up is the following:
- filters.php:
add_filter( ‘woocommerce_template_loader_files’, function ( $templates, $default_file ) {
return filter_templates(array_merge($templates, array_filter([$default_file, ‘woocommerce’])));
}, PHP_INT_MAX, 2 );
add_filter( ‘template_include’, function ( $template ) {
$template = str_contains( $template, 'single-product.php' ) ? get_stylesheet_directory() . '/views/woocommerce/single-product.blade.php' : $template;
$template = str_contains( $template, 'archive-product.php' ) ? get_stylesheet_directory() . '/views/woocommerce/archive-product.blade.php' : $template;
$data = collect( get_body_class() )->reduce( function ( $data, $class ) use ( $template ) {
return apply_filters( "sage/template/{$class}/data", $data, $template );
}, [] );
if ( $template ) {
echo template( $template, $data );
return get_stylesheet_directory() . '/index.php';
}
return $template;
}, PHP_INT_MAX );
add_filter( ‘wc_get_template_part’, function ( $template, $slug, $name, $args = [] ) {
$bladeTemplate = false;
if ( $name && ! WC_TEMPLATE_DEBUG_MODE ) {
$bladeTemplate = locate_template( [ “{$slug}-{$name}.blade.php”, WC()->template_path() . “{$slug}-{$name}.blade.php” ] );
}
if ( ! $template && ! WC_TEMPLATE_DEBUG_MODE ) {
$bladeTemplate = locate_template( [ “{$slug}.blade.php”, WC()->template_path() . “{$slug}.blade.php” ] );
}
if ( $bladeTemplate ) {
echo template( $bladeTemplate, $args );
return null;
}
$normalTemplate = false;
if ( $name && ! WC_TEMPLATE_DEBUG_MODE ) {
$normalTemplate = locate_template( [ “{$slug}-{$name}.php”, WC()->template_path() . “{$slug}-{$name}.php” ] );
}
if ( ! $normalTemplate && ! WC_TEMPLATE_DEBUG_MODE ) {
$normalTemplate = locate_template( [ “{$slug}.php”, WC()->template_path() . “{$slug}.php” ] );
}
if ( $normalTemplate ) {
return get_theme_file_path( $normalTemplate );
}
return $template;
}, PHP_INT_MAX, 4 );
add_filter( ‘wc_get_template’, function ( $located, $template_name, $args, $template_path, $default_path ) {
$bladeTemplate = locate_template( [ $template_name, ‘resources/views/’ . WC()->template_path() . $template_name ] );
if ( $bladeTemplate ) {
return template_path( $bladeTemplate, $args );
}
return $located;
}, PHP_INT_MAX, 5 );
- helpers.php:
function filter_templates($templates)
{
$paths = apply_filters(‘sage/filter_templates/paths’, [
‘views’,
‘resources/views’
]);
$paths_pattern = “#^(” . implode(’|’, $paths) . “)/#”;
return collect($templates)
->map(function ($template) use ($paths_pattern) {
/** Remove .blade.php/.blade/.php from template names */
$template = preg_replace('#\.(blade\.?)?(php)?$#', '', ltrim($template));
/** Remove partial $paths from the beginning of template names */
if (strpos($template, '/')) {
$template = preg_replace($paths_pattern, '', $template);
}
return $template;
})
->flatMap(function ($template) use ($paths) {
return collect($paths)
->flatMap(function ($path) use ($template) {
return [
"{$path}/{$template}.blade.php",
"{$path}/{$template}.php",
];
})
->concat([
"{$template}.blade.php",
"{$template}.php",
]);
})
->filter()
->unique()
->all();
}
/**
- @param string|string[] $templates Relative path to possible template files
-
@return string Location of the template
*/
function locate_template($templates)
{
return \locate_template(filter_templates($templates));
}
-woocommerce.blade.php:
@extends(‘layouts.app-page’)
@section(‘content’)
{!! App\woocommerce_content(get_defined_vars()) !!}
@endsection
Can you guys throw some light on this subject?
I’ve searched everywhere and I got a lot of different aproaches that doesn’t work.
Am I doing the filters set-up right?
What is the best way to override woocommerce templates?