How to use Woocommerce (3.4.3) with Sage 9.0.1 (Blade + SoberWP controllers), WP 4.9.7 (17/07/2018)
Check dev branches for latest updates.
Check previous release if error with latest.
- Includes:
- Blade Woocommerce templates
- Override using default .php file, or rename to .blade.php to use blade (SoberWP controllers variable are available in both case)
- place your woocommerce templates overrides in /your-sage-theme/resources/views/woocommerce/
- eg:
/plugins/woocommerce/templates/content-single-product.php
=>/your-sage-theme/resources/views/woocommerce/content-single-product.blade.php
- eg:
/plugins/woocommerce/templates/single-product/meta.php
=>/your-sage-theme/resources/views/woocommerce/single-product/meta.blade.php
- still need test for sub-templates, but seems to works with
meta.blade.php
- eg:
- SoberWP controllers with Woocommerce blade (or php) templates
- Get \App global default Sage controller variables available in Woocommerce Blade templates
- Create custom controllers for Woocommerce templates with same Woocommerce/SoberWP template/naming hierarchy naming logic (eg:
single-product
controller forsingle-product.blade.php
thencontent-single-product.blade.php
template)
- Blade Woocommerce templates
- I haven't tested all yet, please feel free to report any bug or improvement in issues and I'll fix
- Sources:
- discourse.roots.io
- discourse.roots.io
- github (much thanks again to @MarekVrofski for his improvements)
- github sage PR
- github
- 17/07/2018 - test still in progress
- Sage 9.0.1
- Woocommerce 3.4.3
- SoberWP controller 9.0.0-beta.4 (Sage 9.0.1 uses SoberWP controller 9.0.0-beta.4, not latest SoberWP controller 2.0.1)
- PHP 7.2.5 (fpm), Nginx, Debian (& Windows 10 - Laragon PHP 7.1)
- Something changed, Woocommerce didn't use anymore our
woocommerce.blade.php
. Had to force the template path into the Woocommerce default template array. Seewoocommerce_template_loader_files
filter code comment infilters.php
- 18/05/2018 - tested with
- Woocommerce 3.3.5
- Sage 9.0.1
- SoberWP controller 9.0.0-beta.4 (Sage 9.0.1 uses SoberWP controller 9.0.0-beta.4, not latest SoberWP controller 2.0.1)
- PHP 7.2.5 (fpm), Nginx, Debian (& Windows 10 - Laragon PHP 7.1)
Editing shop page
- I prefer not use the default woocommerce shop page. So I do my own template/controller (wp_query) and pagination (will publish soon, really easy) and redirect official shop page to this custom one. As this template is mostly managed by our App\woocommerce_content() and sub-templated. But it is doable, updating woocommerce_content and related sub-template. Longer maybe ? Cleaner for sure :)
- Single product page, my account, checkout templates should be easy to override
- I'll publish another repo with "how to's" and helpers for Sage (time, time, time...)
- Since V1.1, we force
/resources/views/woocommerce.blade.php
as first in woocommerce default template file array (see changelog) - we add_filter on
template_include
: editsingle-product.php
andarchive-product.php
template path to/resources/views/woocommerce/*.blade.php
(then retrieve controller data and render blade as usual)wc_get_template_part
: edit woocommerce template method to look for blade files then php fileswc_get_template
: tell woocommerce to get template fromresources/views/
single-product.php
andarchive-product.php
includeswoocommerce.blade.php
templatewoocommerce.blade.php
calls our overridedApp\woocommerce_content(get_defined_vars())
that output Woocommerce content
- SoberWP
Controller.php
(new Loader()
) will parse all controller and methods and store them using "slugified"$template
name as keys in$this->instance
(thecontroller::class
object is stored) - SoberWP
controller.php loader()
willadd_filter('sage/template/' . $template . '-data/data')
for all controllers (filter string/controller) with a method that return, (among other datas) the controller data we want - Sage/WP will normally get
single-product.php
template (changed assingle-product.blade.php
in ouradd_filter('template_include')
andadd_filter('wc_get_template_part')
updates targeted in/resources/views
thanks to ouradd_filter('wc_get_template')
) as template hierarchy rules - Sage will
apply_filter('sage/template/' . $template . '-data/data')
(it usesbody_class()
to find the$template
name to match the correct controllers with current template) to add data to theblade->render
(that also render sub-templates that could also need controller data) fromtemplate_include
filter (that we override but just for the "single-product.php" and "archive-product.php" replace update, and load controller data) - At the moment, (we are in
resources/views/woocommerce/single-product.blade.php
), data are defined inget_defined_data()
(as rendered from filtertemplate_include
with controllers data), so WE HAVE THE DATA HERE - from here, we call
helpers::template()
(We don't have HTMl here, we call a method to output Woocommerce content) usingwoocommerce
as template, and we send our vars (fromget_dedined_vars()
) to it woocommerce.blade.php
will callApp\woocommerce_content(get_defined_vars())
===> HERE we lose data if we don't passget_defined_vars()
as parameterApp\woocommerce_content()
is overrided from Woocommercewp-content/plugins/woocommerce/includes/wc-template-functions.php
- to add a parameter to pass
$args
, which are our datas
App\woocommerce_content()
will output correct Woocommerce template hierarchy, passing$args
towc_get_template_part()
it calls
- few test with SoberWP 2.0.1
- Few errors as default Namespace changes from
\App
to\App\Controller
- should be fixable when Sage update to SoberWP Controller
2.0.1
- Few errors as default Namespace changes from
- Lot of Woocommerce features and templates (and controller variables passing) to test...
- controllers variables to sub templates (& nested)
- Absolutely not tested with all Woocommerce templates and features
- Checkout, My accounts, Emails, nested templates, Woocommerce plugins templates... need more tests
- Test before usage in production (I do and it runs smoothly tho, but it's not a complex store)
- still WIP, issues and improvement reports are welcomed !
Notice
error about theme requiringheader.php
andsidebar.php
as it's deprecated to be without since3.0.0
, see roots/sage@1620- (
Theme without header.php is deprecated since version 3.0.0 with no alternative available. Please include a header.php/sidebar.php template in your theme. \wp-includes\functions.php
)
- (
@debug('dump')
@debug('hierarchy')
@debug('controller')
@debug
function print_filters_for( $hook = "" ) {
global $wp_filter;
if( empty( $hook ) || !isset( $wp_filter[$hook] ) )
return;
print "<pre>";
print_r( $wp_filter[$hook] );
print "</pre>";
}