Any working example of Sage 9 latest (Sage 9.0.0-beta.4+) with WooCommerce (3.1.1+)?

blade
woocommerce
sage9

#1

I’ve been trying every example on the forum without much luck.

Some of the examples duplicate the woocommerce content over and over again (in my case), some just don’t work.

I’ve ended with a setup that seams to work, but not sure how to overwrite additional files from woo.

// app/setup.php
add_theme_support('woocommerce');
// app/woocommerce.php
<?php

namespace App;

/**
 * WooCommerce Support
 */
add_filter('woocommerce_template_loader_files', function ($search_files, $default_file) {
    return filter_templates(array_merge($search_files, [$default_file, 'woocommerce']));
}, 100, 2);

add_filter('woocommerce_locate_template', function ($template, $template_name, $template_path) {
    $theme_template = locate_template("{$template_path}{$template_name}");

    return $theme_template ? template_path($theme_template) : $template;
}, 100, 3);

add_filter('wc_get_template_part', function ($template, $slug, $name) {
    $theme_template = locate_template(["woocommerce/{$slug}-{$name}", "woocommerce/${name}"]);

    return $theme_template ? template_path($theme_template) : $template;
}, 100, 3);
// resources/functions.php

array_map(function ($file) use ($sage_error) {
    $file = "../app/{$file}.php";
    if (!locate_template($file, true, true)) {
        $sage_error(sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file), 'File not found');
    }
}, ['helpers', 'setup', 'filters', 'admin', 'woocommerce']);

All from https://github.com/roots/sage/pull/1923

// resources/views/woocommerce/archive-product.php

<?php echo App\Template('woocommerce');
// resources/views/woocommerce.blade.php

@extends ('layouts.app')

@section ('content')
@php (woocommerce_content())
@stop

Does anyone have a better setup?
Can I make any improvements to the current setup?


Controller and WooCommerce
#2

It took me a lot of messing around to get this working for me with Sage 9.0.0-beta.4

Put the following in /resources/views/woocommerce.blade.php

@extends('layouts.app')
@section('content')
  @php(woocommerce_content())
@endsection

But then the thing that worked for me was putting archive-single.php and single-product.php in /resources/woocommerce/ (you need to create the woocommerce folder) instead of putting them in /resources/views/woocommerce/

Both these files have the following content:

<?php echo App\Template('woocommerce');

Appears to be working without errors at the moment.


#3

This works great! Thanks!!


#4

Hi,
With this method, the variation doesn’t seems to work (the price isn’t updated and all the hook aren’t called).

But I mixed up this solution whit the @indrek-k one on github that you find here.
https://github.com/roots/sage/issues/1807#issuecomment-273543163,

adding this filters, you’ll be able to load .blade.php files for each template in the woocommerce plugin folder. Btw you won’t be able to load the single-product.php and the archive-product.php files.

So I just uses both solutions but instead of your content, in the resources/single-product.php file and resources/the archive-product.php I put this content:
single-product.php:

<?php echo App\Template('single-product');

archive-product.php:

<?php echo App\Template('archive-product');

Then i added the single-product.blade.php and the archive-product.blade.php
And, finally, I converted that file in a blade way :slight_smile:
@extends(‘layouts.app’)
@section(‘content’)<?php if ( ! defined( ‘ABSPATH’ ) ) exit;
?><?php do_action( ‘woocommerce_before_main_content’ )
?><?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'single-product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php do_action( 'woocommerce_after_main_content' )?>
<?php do_action( 'woocommerce_sidebar' )?>
@endsection

Plus, I suggest to add the comments in the resources/woocommerce/single-product.php so woocommerce will be able to detect if your template is outdated with the new versions

Actually I think this is too much work to let woocommerce files work on Sage, but I don’t know how it could be better, i’m thinking on buildin a woocommerce-sage theme. Does it will be helpful?


#5

Some clear documentation about integrating Woocommerce with Sage 9 would be very very useful indeed!