Skip to content

Commit

Permalink
Initial commit for Stage & Sage 10 support
Browse files Browse the repository at this point in the history
  • Loading branch information
ouun committed Apr 7, 2020
1 parent fe3fd00 commit 2da0708
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 93 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
"name": "Philipp",
"email": "[email protected]"
}
],
"autoload": {
Expand Down
50 changes: 0 additions & 50 deletions examples/views/archive-product.blade.php

This file was deleted.

22 changes: 0 additions & 22 deletions examples/views/single-product.blade.php

This file was deleted.

2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
148 changes: 132 additions & 16 deletions src/woocommerce.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,140 @@
<?php
namespace App;
namespace Roots;

if (defined('WC_ABSPATH')) {

/**
* Set WooCommerce Theme Support
*/
add_action('after_setup_theme', function () {
add_theme_support('woocommerce');
});

add_filter('template_include', function ($template) {
return strpos($template, WC_ABSPATH) === -1
? $template
: locate_template('woocommerce/' . 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));
return $theme_template ? template_path($theme_template) : $template;
}, 100, 1);

add_filter('wc_get_template', function ($template, $template_name, $args) {
$theme_template = locate_template('woocommerce/' . $template_name);
return $theme_template ? template_path($theme_template, $args) : $template;
}, 100, 3);
/**
* Set template path to /shop
*/
add_filter(
'woocommerce_template_path',
function () {
return 'shop/';
},
100,
1
);

/**
* @param string $template
*
* @return string
*/
function wc_template_loader(string $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', 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
);
}

0 comments on commit 2da0708

Please sign in to comment.