You've already forked wc-bootstrap
Add WooCommerce-to-Twig rendering bridge
Intercept WooCommerce's PHP template loading via
woocommerce_before_template_part / woocommerce_after_template_part hooks
to render Bootstrap 5 Twig templates instead. This makes all 99 child
theme templates functional in a standard WooCommerce environment.
- Create WooCommerceExtension (Twig AbstractExtension) with ~50 functions
and 7 filters covering WC API, WordPress hooks, escaping, and forms
- Rewrite TemplateOverride to use hook-based interception with stack-based
output buffering for nested template support
- Wire bridge initialization at init priority 20 in functions.php
- Fix invalid {% do return() %} in two order templates
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
<?php
|
||||
/**
|
||||
* Template Override
|
||||
* Template Override.
|
||||
*
|
||||
* Hooks into the plugin's Twig FilesystemLoader to prepend
|
||||
* the child theme's templates/ directory, allowing template overrides.
|
||||
* Intercepts WooCommerce's PHP template rendering and replaces it
|
||||
* with Twig template output from the child theme's templates/ directory.
|
||||
*
|
||||
* Uses woocommerce_before_template_part / woocommerce_after_template_part
|
||||
* hooks to render the Twig version and discard the PHP include output.
|
||||
*
|
||||
* @package WcBootstrap
|
||||
* @since 0.1.0
|
||||
@@ -11,9 +14,7 @@
|
||||
|
||||
namespace WcBootstrap;
|
||||
|
||||
// IMPORTANT: Update these imports to match your plugin's actual class names.
|
||||
use Magdev\Woocommerce\Frontend\Template;
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
use WPBootstrap\Twig\TwigService;
|
||||
|
||||
class TemplateOverride {
|
||||
|
||||
@@ -22,55 +23,114 @@ class TemplateOverride {
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private string $template_path;
|
||||
private string $templatePath;
|
||||
|
||||
/**
|
||||
* Stack of template names with active output buffers.
|
||||
*
|
||||
* Handles nested template calls where one WC template triggers
|
||||
* another via wc_get_template() from within action hooks.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private array $bufferStack = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->template_path = WC_BOOTSTRAP_PATH . 'templates';
|
||||
$this->templatePath = WC_BOOTSTRAP_PATH . 'templates/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the template override with WordPress hooks.
|
||||
* Register the template override hooks.
|
||||
*
|
||||
* Must be called after the plugin's Template singleton is initialized
|
||||
* (plugin inits at 'init' priority 0).
|
||||
* Only registers if the parent theme's TwigService is available.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
add_action( 'init', [ $this, 'override_template_paths' ], 20 );
|
||||
if ( ! class_exists( TwigService::class ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
add_action( 'woocommerce_before_template_part', [ $this, 'beforeTemplatePart' ], 10, 4 );
|
||||
add_action( 'woocommerce_after_template_part', [ $this, 'afterTemplatePart' ], 10, 4 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepend the child theme's templates directory to the Twig loader.
|
||||
* Before WooCommerce includes a PHP template.
|
||||
*
|
||||
* This makes Twig look in the child theme's templates/ first,
|
||||
* falling back to the plugin's templates/ if not found.
|
||||
* If a matching Twig template exists, renders it and starts output
|
||||
* buffering to capture (and later discard) the PHP template output.
|
||||
*
|
||||
* @param string $templateName Template name (e.g., 'cart/cart.php').
|
||||
* @param string $templatePath Template path override.
|
||||
* @param string $located Full path to the located PHP template.
|
||||
* @param array $args Template context variables.
|
||||
* @return void
|
||||
*/
|
||||
public function override_template_paths(): void {
|
||||
if ( ! class_exists( Template::class ) ) {
|
||||
return;
|
||||
}
|
||||
public function beforeTemplatePart( string $templateName, string $templatePath, string $located, array $args ): void {
|
||||
$twigTemplate = $this->resolveTwigTemplate( $templateName );
|
||||
|
||||
if ( ! is_dir( $this->template_path ) ) {
|
||||
return;
|
||||
if ( null === $twigTemplate ) {
|
||||
return; // No Twig override — let PHP render normally.
|
||||
}
|
||||
|
||||
try {
|
||||
$twig = Template::get_instance()->get_twig();
|
||||
$loader = $twig->getLoader();
|
||||
$twig = TwigService::getInstance();
|
||||
echo $twig->render( $twigTemplate, $args );
|
||||
|
||||
if ( $loader instanceof FilesystemLoader ) {
|
||||
$loader->prependPath( $this->template_path );
|
||||
}
|
||||
} catch ( \Exception $e ) {
|
||||
// Buffer the upcoming PHP include so we can discard it.
|
||||
ob_start();
|
||||
$this->bufferStack[] = $templateName;
|
||||
} catch ( \Throwable $e ) {
|
||||
// Twig render failed — let PHP render as fallback.
|
||||
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
|
||||
error_log( 'WooCommerce Bootstrap: Failed to register template override - ' . $e->getMessage() );
|
||||
error_log( sprintf(
|
||||
'WC Bootstrap: Twig render failed for %s — %s',
|
||||
$templateName,
|
||||
$e->getMessage()
|
||||
) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* After WooCommerce includes a PHP template.
|
||||
*
|
||||
* If we started buffering for this template, discards the PHP output.
|
||||
*
|
||||
* @param string $templateName Template name.
|
||||
* @param string $templatePath Template path override.
|
||||
* @param string $located Full path to the located PHP template.
|
||||
* @param array $args Template context variables.
|
||||
* @return void
|
||||
*/
|
||||
public function afterTemplatePart( string $templateName, string $templatePath, string $located, array $args ): void {
|
||||
if ( ! empty( $this->bufferStack ) && end( $this->bufferStack ) === $templateName ) {
|
||||
ob_end_clean(); // Discard PHP template output.
|
||||
array_pop( $this->bufferStack );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a WooCommerce template name to a Twig template path.
|
||||
*
|
||||
* Maps 'cart/cart.php' to 'cart/cart.html.twig' and checks that
|
||||
* the file exists in the child theme's templates/ directory.
|
||||
*
|
||||
* @param string $templateName WooCommerce template name.
|
||||
* @return string|null Twig template path relative to templates/, or null if not found.
|
||||
*/
|
||||
private function resolveTwigTemplate( string $templateName ): ?string {
|
||||
$twigName = preg_replace( '/\.php$/', '.html.twig', $templateName );
|
||||
$fullPath = $this->templatePath . $twigName;
|
||||
|
||||
if ( file_exists( $fullPath ) ) {
|
||||
return $twigName;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user