Skip to content

Instantly share code, notes, and snippets.

@bdmason
Last active October 26, 2022 10:48
Show Gist options
  • Save bdmason/f26bb8891a3c066ce609200d5ad95912 to your computer and use it in GitHub Desktop.
Save bdmason/f26bb8891a3c066ce609200d5ad95912 to your computer and use it in GitHub Desktop.
Using Sage 9 blade templates for WooCommerce

Using Sage 9 blade templates for WooCommerce

Declare WooCommerce theme support

Open app/setup.php, find the after_setup_theme action and put add_theme_support('woocommerce'); inside the function.

Override the plugin templates

Add the templates you want to override in resources/woocommerce:

theme    
│
└───resources
    │
    └───woocommerce
        │   archive-product.php
        │   single-product.php
        │   ...

Enable blade templates

Inside each of the template overrides created in the previous step add the following code:

<?php echo App\Template('woo.template-name'); ?>

This assumues your blade is located at resources/views/woo/template-name.blade.php, but the blades can be anywhere inside resources/views.

Use the blades

Just write your blades like normal, adding the bits from the default plugin templates you want to keep.

@php if(!defined('ABSPATH')) { exit; } @endphp
@extends('layouts.app')
@section('content')
	@while(have_posts())
		@php the_post() @endphp
		@include('woo.partials.single-product.content')
	@endwhile
	@php do_action('woocommerce_sidebar') @endphp
@endsection

If you want to use a default partial from the plugin use @php wc_get_template_part() @endphp instead of @include.

@camaulay
Copy link

camaulay commented Apr 15, 2020

This was super helpful for me but I wasn't able to access any Controller data from within my blade template. I was able to achieve this by reducing the $data object based on the page classes and then passing that into the template.

<?php
    $data = collect(get_body_class())->reduce(function ($data, $class) {
        return apply_filters("sage/template/{$class}/data", $data);
    }, []);
    echo App\Template('woo.template-name', $data);
?>

@tgeorgel
Copy link

This was super helpful for me but I wasn't able to access any Controller data from within my blade template. I was able to achieve this by reducing the $data object based on the page classes and then passing that into the template.

<?php
    $data = collect(get_body_class())->reduce(function ($data, $class) {
        return apply_filters("sage/template/{$class}/data", $data);
    }, []);
    echo App\Template('woo.template-name', $data);
?>

Works like a charm, thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment