Files
wc-bootstrap/inc/TemplateOverride.php
magdev 00872a6568 Add Bootstrap 5 product archive with card grid and sidebar
Replace WooCommerce's default shop/category page rendering with a
Bootstrap 5 card grid layout featuring responsive columns, sale badges,
star ratings, and an offcanvas sidebar for filters on mobile.

Key implementation details:

- Bypass parent theme's TemplateController for product archives via
  wp_bootstrap_should_render_template filter, render at template_redirect
  priority 11 using the same page shell injection pattern as plugin pages

- Add archive-product.php (Bootstrap layout with optional sidebar) and
  content-product.php (PHP bridge for wc_get_template_part interception)

- Inject global $product into Twig context in TemplateOverride to fix
  empty price/add-to-cart/rating/sale-flash in loop sub-templates — Twig
  has isolated variable scopes and cannot access PHP globals directly

- Fix pagination URLs: use get_pagenum_link() instead of ?page= query
  param (WordPress uses 'paged' for archive pagination, not 'page')

- Fix double-escaped – in result count by adding |raw filter

- Reset WooCommerce float-based layout CSS (woocommerce-layout.css) for
  shop pages to prevent conflicts with Bootstrap flex grid

- Register shop-sidebar widget area with Bootstrap-styled markup

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-28 15:06:33 +01:00

146 lines
4.8 KiB
PHP

<?php
/**
* Template Override.
*
* 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
*/
namespace WcBootstrap;
use WPBootstrap\Twig\TwigService;
class TemplateOverride {
/**
* Path to the child theme's templates directory.
*
* @var string
*/
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->templatePath = WC_BOOTSTRAP_PATH . 'templates/';
}
/**
* Register the template override hooks.
*
* Only registers if the parent theme's TwigService is available.
*
* @return void
*/
public function register(): void {
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 );
}
/**
* Before WooCommerce includes a PHP template.
*
* 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 beforeTemplatePart( string $templateName, string $templatePath, string $located, array $args ): void {
$twigTemplate = $this->resolveTwigTemplate( $templateName );
if ( null === $twigTemplate ) {
return; // No Twig override — let PHP render normally.
}
try {
$twig = TwigService::getInstance();
$context = $args;
// Inject the global $product into the Twig context.
// WooCommerce PHP templates access it via `global $product;` but Twig
// templates have isolated variable scopes and need it passed explicitly.
if ( ! isset( $context['product'] ) && ! empty( $GLOBALS['product'] ) ) {
$context['product'] = $GLOBALS['product'];
}
echo $twig->render( $twigTemplate, $context );
// 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( 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;
}
}