Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sage 10 compatibilty #23

Open
wants to merge 5 commits into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -1,6 +1,6 @@
# WooCommerce integration for Sage 9 themes
# WooCommerce integration for Sage 10 themes

This package enables WooCommerce integration with Sage 9 themes and Blade templates.
This package enables WooCommerce integration with Sage 10 themes and Blade templates.

## Installation

@@ -11,6 +11,14 @@ cd wp-content/themes/your-sage-theme-folder
composer require roots/sage-woocommerce
```

Add package path to Sage view namespaces in `config/view.php`:

```php
'namespaces' => [
'SageWoocommerce' => get_theme_file_path('/vendor/roots/sage-woocommerce/src/resources/views'),
],
```

## Usage

Create `/resources/views/woocommerce` folder in your theme and place there any template used by WooCommerce with `.blade.php` extension. This template will be loaded instead of a template from the WooCommerce plugin. If you want to replace particular template, please have a look into plugin folder `woocommerce/templates` and use same folder structure and file name (and change the extension to `.blade.php`) as the original template.
@@ -21,4 +29,4 @@ By default, you will get an error message that themes without `header.php`, `foo
do_action('get_header', 'shop');
do_action('get_sidebar', 'shop');
do_action('get_footer', 'shop');
```
```
@@ -0,0 +1 @@
<?php
@@ -1,5 +1,5 @@
<?php
namespace App;
namespace Roots;

if (defined('WC_ABSPATH')) {
add_action('after_setup_theme', function () {
@@ -12,53 +12,72 @@
*/
function wc_template_loader(String $template)
{
return strpos($template, WC_ABSPATH) === -1
? $template
: (locate_template(WC()->template_path() . str_replace(WC_ABSPATH . 'templates/', '', $template)) ? : $template);
if (strpos($template, WC_ABSPATH) === -1) {
return $template;
}

return locate_template(app('sage.finder')->locate(WC()->template_path() . str_replace(WC_ABSPATH . 'templates/', '', $template))) ? : $template;
}
add_filter('template_include', __NAMESPACE__ . '\\wc_template_loader', 100, 1);
add_filter('template_include', __NAMESPACE__ . '\\wc_template_loader', 90, 1);
add_filter('comments_template', __NAMESPACE__ . '\\wc_template_loader', 100, 1);

add_filter('wc_get_template_part', function ($template) {
$theme_template = locate_template(WC()->template_path() . str_replace(WC_ABSPATH . 'templates/', '', $template));
$theme_template = locate_template(app('sage.finder')->locate(
WC()->template_path() . str_replace(WC_ABSPATH . 'templates/', '', $template)
));

if ($theme_template) {
$data = collect(get_body_class())->reduce(function ($data, $class) {
return apply_filters("sage/template/{$class}/data", $data);
$view = app('view.finder')
->getPossibleViewNameFromPath($file = realpath($theme_template));

$view = trim($view, '\\/.');

/** Gather data to be passed to view */
$data = array_reduce(get_body_class(), function ($data, $class) use ($view, $file) {
return apply_filters("sage/template/{$class}/data", $data, $view, $file);
}, []);

echo template($theme_template, $data);
return get_stylesheet_directory() . '/index.php';
echo view($view, $data)->render();
}

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);
$theme_template = locate_template(app('sage.finder')->locate(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);
$view = app('view.finder')
->getPossibleViewNameFromPath($file = realpath($theme_template));

$view = trim($view, '\\/.');

/** Gather data to be passed to view */
$data = array_reduce(get_body_class(), function ($data, $class) use ($view, $file) {
return apply_filters("sage/template/{$class}/data", $data, $view, $file);
}, []);

echo template($theme_template, array_merge(
echo view($view, array_merge(
compact(explode(' ', 'template_name template_path located args')),
$data,
$args
));
))->render();
}
}, PHP_INT_MAX, 4);

add_filter('wc_get_template', function ($template, $template_name, $args) {
$theme_template = locate_template(WC()->template_path() . $template_name);
$theme_template = locate_template(app('sage.finder')->locate(WC()->template_path() . $template_name));

// return theme filename for status screen
if (is_admin() && ! wp_doing_ajax() && function_exists('get_current_screen') && get_current_screen() && get_current_screen()->id === 'woocommerce_page_wc-status') {
if (is_admin() &&
! wp_doing_ajax() &&
function_exists('get_current_screen') &&
get_current_screen() &&
get_current_screen()->id === 'woocommerce_page_wc-status') {
return $theme_template ? : $template;
}

// return empty file, output already rendered by 'woocommerce_before_template_part' hook
return $theme_template ? get_stylesheet_directory() . '/index.php' : $template;
return $theme_template ? view('SageWoocommerce::empty')->getPath() : $template;
}, 100, 3);
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.