Timber / WooCommerce 3.3.x问题?

时间:2018-04-13 13:11:13

标签: php wordpress woocommerce timber

我一直在使用Timber一段时间用于我的WordPress主题开发并喜欢它。但是,我最近遇到了一个我以前没见过的问题。请注意,该网站是使用WC 3.2.6和Timber 1.7.0开发的,一切都运行良好。该项目有点搁浅,现在已经开始推出了,所以我更新了所有插件并遇到了一个问题,好像WooCommerce在初始化时发生了变化(可能?)。

我根本不使用WooCommerce,所以我希望这是一个简单而明显的东西,我只是缺席了。

使用WC 3.2.6 WC页面使用他们自己的模板和我在主题目录中的任何覆盖,非WC页面按预期使用Twig模板。 更新后,WC使用Twig模板而不是自己的模板 - 不用说完全打破了我的网站。我使用一个模式进行Timber / WordPress开发,其中所有内容都通过主要的functions.php文件,其中包含一个主题函数对象,其中决定了渲染模板 - 我在主题目录中唯一的php文件是与特定自定义模板相关的文件(gallery.php,staff-listing.php等..)和index.php文件。它们看起来像这样:

$data = Timber::get_context();
Timber::render($data['template'], $data, 600);

我的基本functions.php文件包括以下内容:

if(!class_exists('Timber')){
    add_action('admin_notices', function(){
        print("<div class='error'><h1>Please activate the Timber plugin</h1></div>");
    });
    return;
}

require_once('includes/ThemeFunctions.php');
$fn = new ThemeFunctions();
// ...
add_filter('timber_context', array($fn,'addToContext'));
//...

ThemeFunctions文件包含以下方法:

    public function addToContext(array $context){
        if(defined('DOING_AJAX') && DOING_AJAX){
            return $context;
        }
        $context['posts'] = new Timber\PostQuery();
    // ...
        $context['template'] = $this->getRenderTemplate($context['posts']);
    // ...
        return $context;
    }

    private function getRenderTemplate(ArrayObject $post){
        $post = isset($post[0]) ? $post[0] : null;
        $tpl = !empty($post) ? get_page_template_slug($post->ID) : null;
        if(empty($tpl)){
            $tpl = 'index';
        }
        if(is_404()){
            $tpl = '404';
        }
        if(is_front_page()){
            $tpl = 'home';
        }
        if(is_home() || is_archive() || is_search()){
            $tpl = 'archive';
        }
        if(is_single()){
            $tpl = 'single';
        }
        $tpl = str_replace(array('.','/',' ','php','"',"'"), '', $tpl);
        if(!file_exists($this->_themeDir.'views/'.$tpl.$this->_fileExt) || !ctype_alnum(str_replace(array('-','_'), '', $tpl))){
            return '404'.$this->_fileExt;
        }
        return esc_attr($tpl).$this->_fileExt;
    }

我的每个Twig模板都在开始的body标签上包含了一个data-template属性,这就是我知道3.2.6是在渲染WC模板 - 那个数据属性不在那里。现在是。

TL; DR

我是否遗漏了某些内容,或者版本3.3.0或附近的WC发生了变化,当插件初始化时需要更改get_context过滤器的时间或优先级?我正在查看更改日志和源代码,但希望有人知道我应该看看他们的头顶。非常感谢提前!

1 个答案:

答案 0 :(得分:2)

如果其他人遇到类似的问题,问题的原因是用于向主题添加WooCommerce支持的钩子。我通常使用'init'钩子来添加我的所有主题支持,包括WC。对于v 3.3.0及更高版本,似乎init挂钩在此过程中触发得太晚,并且必须使用'after_setup_theme'挂钩添加WC支持。