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') diff --git a/src/woocommerce.php b/src/woocommerce.php index 77ce2cc..db53165 100644 --- a/src/woocommerce.php +++ b/src/woocommerce.php @@ -9,29 +9,49 @@ 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) { - 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(WC()->template_path() . $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); + $theme_template = locate_template(WC()->template_path() . $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); }