Sage: [Feature request] Woocommerce template support

Created on 12 Jan 2017  路  6Comments  路  Source: roots/sage

Feature request


What is the current behavior?
Currently, there is no support for Woocommerce templates.
Template files placed in templates/woocommerce/ with .blade.php extension aren't recognized (ignored)
If copy file from Woocommerce plugin *templates
folder to templates/woocoommerce then Wordpress throws Theme without header.php is <strong>deprecated</strong> since version 3.0.0 with no alternative available. Please include a header.php template in your theme. in /Users/emilsgulbis/Documents/Development/infinitum/[theme-name]/wp-includes/functions.php on line 3959

What is the expected or desired behavior?
Sage templating system should recongnize Woocommerce template files

Most helpful comment

Ok, found solution to prevent Theme without header.php is...
Like here https://roots.io/using-woocommerce-with-sage/ you should create 2 files in templates/woocommerce - archive-product.php and single-product.php and add only
<?php echo App\Template('woocommerce'); to them.
Then in templates/woocommerce add woocommerce.blade.php that contains
@extends('layouts.base') @section('content') @php(woocommerce_content()) @endsection

All 6 comments

Ok, found solution to prevent Theme without header.php is...
Like here https://roots.io/using-woocommerce-with-sage/ you should create 2 files in templates/woocommerce - archive-product.php and single-product.php and add only
<?php echo App\Template('woocommerce'); to them.
Then in templates/woocommerce add woocommerce.blade.php that contains
@extends('layouts.base') @section('content') @php(woocommerce_content()) @endsection

While @emilsgulbis solution works to get base layout. There is still missing pieces, ex. I could not get variable product's variation selection work. Price or stock didn't update when selecting variation.

Started a new project using Sage 9 and WooCommerce. I put together a quick PoC. I'll be testing this in the following days, but maybe it already helps someone.

// src/filters.php

add_filter('wc_get_template_part', function ($template, $slug, $name) {

    $bladeTemplate = false;

    // Look in yourtheme/slug-name.blade.php and yourtheme/woocommerce/slug-name.blade.php
    if ( $name && ! WC_TEMPLATE_DEBUG_MODE ) {
        $bladeTemplate = locate_template( array( "{$slug}-{$name}.blade.php", WC()->template_path() . "{$slug}-{$name}.blade.php" ) );
    }

    // If template file doesn't exist, look in yourtheme/slug.blade.php and yourtheme/woocommerce/slug.blade.php
    if ( ! $template && ! WC_TEMPLATE_DEBUG_MODE ) {
        $bladeTemplate = locate_template( array( "{$slug}.blade.php", WC()->template_path() . "{$slug}.blade.php" ) );
    }

    if ($bladeTemplate) {
        echo template($bladeTemplate);

        // Return a blank file to make WooCommerce happy
        return get_theme_file_path('index.php');
    }

    return $template;
}, PHP_INT_MAX, 3);


add_filter('wc_get_template', function($located, $template_name, $args, $template_path, $default_path) {

    $bladeTemplateName = str_replace('.php', '.blade.php', $template_name);
    $bladeTemplate = locate_template( array($bladeTemplateName, WC()->template_path() . $bladeTemplateName ) );

    if ($bladeTemplate) {
        return template_path($bladeTemplate, $args);
    }

    return $located;
}, PHP_INT_MAX, 5);

I'm already thinking it might be better to filter wc_locate_template() instead. Would appreciate any comments.

WooCommerce 2.7 (currently in beta) allows you to hook woocommerce_template_loader_files.

See

It's highly unlikely that we're going to be doing anything with that filter out of the box in Sage, so at this point, this issue can be taken to discourse or possibly the docs repo.

@indrek-k with your snippet, should it be possible to have the woocommerce templates, e.g. archive-product.php as blade templates, archive-product.blade.php templates?

Did you manage to mix php and blade templates successfully? I'm having a hard time to come up with a solution. My current layout has a sidebar which is a blade template and gets data from a controller, the main column should output the woocommerce templates.

Yes, it should be possible to mix them pretty much interchangeably.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nicooprat picture nicooprat  路  5Comments

emilsgulbis picture emilsgulbis  路  6Comments

MitchEff picture MitchEff  路  3Comments

ajoah picture ajoah  路  4Comments

jlariza picture jlariza  路  5Comments