From ac8301e8118182fce6bb33be91d66a561ce7dfc9 Mon Sep 17 00:00:00 2001 From: Daniel Mejta Date: Thu, 23 Aug 2018 09:56:46 +0200 Subject: [PATCH 1/3] wc_get_template enhancements - Add rendering to 'woocommerce_before_template_part' hook (inspiration in https://github.com/kimhf/sage-woocommerce-support) - Add local variables to template #6 - Render only when not in status screen #9 - Provide data access as suggested in #9 --- src/woocommerce.php | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/woocommerce.php b/src/woocommerce.php index 77ce2cc..f50bf5e 100644 --- a/src/woocommerce.php +++ b/src/woocommerce.php @@ -16,22 +16,42 @@ $theme_template = locate_template('woocommerce/' . str_replace(WC_ABSPATH . 'templates/', '', $template)); if ($theme_template) { - echo template($theme_template); + $data = collect(get_body_class())->reduce(function ($data, $class) { + return apply_filters("sage/template/{$class}/data", $data); + }, []); + + echo template($theme_template, $data); return get_stylesheet_directory() . '/index.php'; } return $template; }, PHP_INT_MAX, 1); + add_action('woocommerce_before_template_part', function($template_name, $template_path, $located, $args) { + $theme_template = locate_template('woocommerce/' . $template_name); + + if ($theme_template) { + $data = collect(get_body_class())->reduce(function ($data, $class) { + return apply_filters("sage/template/{$class}/data", $data); + }, []); + + echo template($theme_template, array_merge( + compact(explode(' ', 'template_name template_path located args')), + $data, + $args + )); + } + }, PHP_INT_MAX, 4); + add_filter('wc_get_template', function ($template, $template_name, $args) { $theme_template = locate_template('woocommerce/' . $template_name); - // Don't render template when used in REST - if ($theme_template && !(defined('REST_REQUEST') && REST_REQUEST)) { - echo template($theme_template, $args); - return get_stylesheet_directory() . '/index.php'; + // return theme filename for status screen + if (is_admin() && function_exists('get_current_screen') && get_current_screen()->id === 'woocommerce_page_wc-status') { + return $theme_template ?: $template; } - return $theme_template ? template_path($theme_template, $args) : $template; - }, PHP_INT_MAX, 3); + // return empty file, output already rendered by 'woocommerce_before_template_part' hook + return $theme_template ? get_stylesheet_directory() . '/index.php' : $template; + }, 100, 3); } From c9e50d518ab40c73ff62f6e16a95c28a41779ae4 Mon Sep 17 00:00:00 2001 From: Daniel Mejta Date: Thu, 23 Aug 2018 10:15:43 +0200 Subject: [PATCH 2/3] Add comments to template files so it will not be shown as an outdated in status screen --- .../views/woocommerce/archive-product.blade.php | 16 ++++++++++++++++ .../views/woocommerce/single-product.blade.php | 17 +++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/examples/resources/views/woocommerce/archive-product.blade.php b/examples/resources/views/woocommerce/archive-product.blade.php index 48c7d59..27bc0ed 100644 --- a/examples/resources/views/woocommerce/archive-product.blade.php +++ b/examples/resources/views/woocommerce/archive-product.blade.php @@ -1,3 +1,19 @@ +{{-- +The Template for displaying product archives, including the main shop page which is a post type archive + +This template can be overridden by copying it to yourtheme/woocommerce/archive-product.php. + +HOWEVER, on occasion WooCommerce will need to update template files and you +(the theme developer) will need to copy the new files to your theme to +maintain compatibility. We try to do this as little as possible, but it does +happen. When this occurs the version of the template file will be bumped and +the readme will list any important changes. + +@see https://docs.woocommerce.com/document/template-structure/ +@package WooCommerce/Templates +@version 3.4.0 +--}} + @extends('layouts.app') @section('content') diff --git a/examples/resources/views/woocommerce/single-product.blade.php b/examples/resources/views/woocommerce/single-product.blade.php index cbe5cd8..ec98cb0 100644 --- a/examples/resources/views/woocommerce/single-product.blade.php +++ b/examples/resources/views/woocommerce/single-product.blade.php @@ -1,3 +1,20 @@ +{{-- +The Template for displaying all single products + +This template can be overridden by copying it to yourtheme/woocommerce/single-product.php. + +HOWEVER, on occasion WooCommerce will need to update template files and you +(the theme developer) will need to copy the new files to your theme to +maintain compatibility. We try to do this as little as possible, but it does +happen. When this occurs the version of the template file will be bumped and +the readme will list any important changes. + +@see https://docs.woocommerce.com/document/template-structure/ +@author WooThemes +@package WooCommerce/Templates +@version 1.6.4 +--}} + @extends('layouts.app') @section('content') From 042c544eb1d0313e77af4089ce6637f74147b74c Mon Sep 17 00:00:00 2001 From: Daniel Mejta Date: Thu, 23 Aug 2018 17:17:05 +0200 Subject: [PATCH 3/3] Do not hardcode '/woocommerce' as WooCommerce template folder #11 --- src/woocommerce.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/woocommerce.php b/src/woocommerce.php index f50bf5e..db53165 100644 --- a/src/woocommerce.php +++ b/src/woocommerce.php @@ -9,11 +9,11 @@ add_filter('template_include', function ($template) { return strpos($template, WC_ABSPATH) === -1 ? $template - : locate_template('woocommerce/' . str_replace(WC_ABSPATH . 'templates/', '', $template)) ?: $template; + : locate_template(WC()->template_path() . str_replace(WC_ABSPATH . 'templates/', '', $template)) ?: $template; }, 100, 1); add_filter('wc_get_template_part', function ($template) { - $theme_template = locate_template('woocommerce/' . str_replace(WC_ABSPATH . 'templates/', '', $template)); + $theme_template = locate_template(WC()->template_path() . str_replace(WC_ABSPATH . 'templates/', '', $template)); if ($theme_template) { $data = collect(get_body_class())->reduce(function ($data, $class) { @@ -28,7 +28,7 @@ }, PHP_INT_MAX, 1); add_action('woocommerce_before_template_part', function($template_name, $template_path, $located, $args) { - $theme_template = locate_template('woocommerce/' . $template_name); + $theme_template = locate_template(WC()->template_path() . $template_name); if ($theme_template) { $data = collect(get_body_class())->reduce(function ($data, $class) { @@ -44,7 +44,7 @@ }, PHP_INT_MAX, 4); add_filter('wc_get_template', function ($template, $template_name, $args) { - $theme_template = locate_template('woocommerce/' . $template_name); + $theme_template = locate_template(WC()->template_path() . $template_name); // return theme filename for status screen if (is_admin() && function_exists('get_current_screen') && get_current_screen()->id === 'woocommerce_page_wc-status') {