【发布时间】:2018-09-19 21:51:24
【问题描述】:
我已按照本指南将 Woocommerce 集成到 Timber: https://timber.github.io/docs/guides/woocommerce/
我也在functions.php中加入了这个
add_action( 'after_setup_theme', 'add_woocommerce_support' );
function add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
我的woocommerce.php与指南中的一模一样:
<?php
if ( ! class_exists( 'Timber' ) ){
echo 'Timber not activated. Make sure you activate the plugin in <a href="/wp-admin/plugins.php#timber">/wp-admin/plugins.php</a>';
return;
}
$context = Timber::get_context();
$context['sidebar'] = Timber::get_widgets( 'shop-sidebar' );
if ( is_singular( 'product' ) ) {
$context['post'] = Timber::get_post();
$product = wc_get_product( $context['post']->ID );
$context['product'] = $product;
// Get related products
$related_limit = wc_get_loop_prop( 'columns' );
$related_ids = wc_get_related_products( $context['post']->id, $related_limit );
$context['related_products'] = Timber::get_posts( $related_ids );
// Restore the context and loop back to the main query loop.
wp_reset_postdata();
Timber::render( 'views/woo/single-product.twig', $context );
} else {
$posts = Timber::get_posts();
$context['products'] = $posts;
if ( is_product_category() ) {
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$context['category'] = get_term( $term_id, 'product_cat' );
$context['title'] = single_term_title( '', false );
}
Timber::render( 'views/woo/archive.twig', $context );
}
然而,每当我尝试访问产品页面时,我都会收到此错误
Fatal error: Uncaught Error: Call to a member function is_on_sale() on null in /app/wp-content/plugins/woocommerce/templates/single-product/sale-flash.php on line 30
这意味着全局$product 是null。
在错误行之前直接添加$product = wc_get_product($post->ID) 可以解决问题,但这显然不是解决方法,因为我要修改核心文件。
我错过了什么吗?
【问题讨论】:
标签: wordpress woocommerce timber