From 2da0708f26607c447887620677d2dd87d5cb1c22 Mon Sep 17 00:00:00 2001 From: Philipp Date: Tue, 7 Apr 2020 11:58:58 +0200 Subject: [PATCH] Initial commit for Stage & Sage 10 support --- composer.json | 8 +- examples/views/archive-product.blade.php | 50 -------- examples/views/single-product.blade.php | 22 ---- readme.md | 2 +- src/woocommerce.php | 148 ++++++++++++++++++++--- 5 files changed, 137 insertions(+), 93 deletions(-) delete mode 100644 examples/views/archive-product.blade.php delete mode 100644 examples/views/single-product.blade.php diff --git a/composer.json b/composer.json index 49c69ec..cfcf05b 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,12 @@ { - "name": "roots/sage-woocommerce", - "description": "WooCommerce integration for Sage 9", + "name": "ouun/stage-woocommerce", + "description": "WooCommerce integration for Stage & Sage 10", "type": "library", "license": "MIT", "authors": [ { - "name": "Daniel Mejta", - "email": "daniel@mejta.net" + "name": "Philipp", + "email": "philipp@ouun.io" } ], "autoload": { diff --git a/examples/views/archive-product.blade.php b/examples/views/archive-product.blade.php deleted file mode 100644 index 48c7d59..0000000 --- a/examples/views/archive-product.blade.php +++ /dev/null @@ -1,50 +0,0 @@ -@extends('layouts.app') - -@section('content') - @php - do_action('get_header', 'shop'); - do_action('woocommerce_before_main_content'); - @endphp - -
- @if(apply_filters('woocommerce_show_page_title', true)) -

- @endif - - @php - do_action('woocommerce_archive_description'); - @endphp -
- - @if(woocommerce_product_loop()) - @php - do_action('woocommerce_before_shop_loop'); - woocommerce_product_loop_start(); - @endphp - - @if(wc_get_loop_prop('total')) - @while(have_posts()) - @php - the_post(); - do_action('woocommerce_shop_loop'); - wc_get_template_part('content', 'product'); - @endphp - @endwhile - @endif - - @php - woocommerce_product_loop_end(); - do_action('woocommerce_after_shop_loop'); - @endphp - @else - @php - do_action('woocommerce_no_products_found'); - @endphp - @endif - - @php - do_action('woocommerce_after_main_content'); - do_action('get_sidebar', 'shop'); - do_action('get_footer', 'shop'); - @endphp -@endsection diff --git a/examples/views/single-product.blade.php b/examples/views/single-product.blade.php deleted file mode 100644 index cbe5cd8..0000000 --- a/examples/views/single-product.blade.php +++ /dev/null @@ -1,22 +0,0 @@ -@extends('layouts.app') - -@section('content') - @php - do_action('get_header', 'shop'); - do_action('woocommerce_before_main_content'); - @endphp - - @while(have_posts()) - @php - the_post(); - do_action('woocommerce_shop_loop'); - wc_get_template_part('content', 'single-product'); - @endphp - @endwhile - - @php - do_action('woocommerce_after_main_content'); - do_action('get_sidebar', 'shop'); - do_action('get_footer', 'shop'); - @endphp -@endsection diff --git a/readme.md b/readme.md index 3ca480d..f62270b 100644 --- a/readme.md +++ b/readme.md @@ -16,4 +16,4 @@ Create `shop` folder in `/resources/views` folder of your theme and place there ## Forked -This is a fork from #roots/sage-woocommerce for Sage 9 and adjusted to Sage 10. +This is a fork from `roots/sage-woocommerce` for Sage 9 and adjusted to Sage 10. diff --git a/src/woocommerce.php b/src/woocommerce.php index f508963..f3b2a7a 100644 --- a/src/woocommerce.php +++ b/src/woocommerce.php @@ -1,24 +1,140 @@ locate( + WC()->template_path() . str_replace( + WC_ABSPATH . 'templates/', + '', + $template + ) + ) + ) ?: $template; + } + + 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( + app('sage.finder')->locate( + WC()->template_path() . str_replace(WC_ABSPATH . 'templates/', '', $template) + ) + ); + + if ($theme_template) { + $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); + }, + array() + ); + + 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(app('sage.finder')->locate(WC()->template_path() . $template_name)); + + if ($theme_template) { + $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); + }, + array() + ); + + 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(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' + ) { + return $theme_template ?: $template; + } + + // return empty file, output already rendered by 'woocommerce_before_template_part' hook + return $theme_template ? view('empty')->getPath() : $template; + }, + 100, + 3 + ); }