Я следовал этому руководству, чтобы интегрировать 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)
непосредственно перед строкой ошибки решает проблему, но это явно не обходной путь, так как я бы изменял основные файлы.
Я что-то здесь упускаю?