简体   繁体   中英

Include PHP file in Woocommerce template

I'm customising the Woocommerce content-single-product.php and want to add a custom template file for the sidebar.

so far I've tried:

<?php require get_stylesheet_directory() . '/layout/page-sidebar.php'; ?>

This works elsewhere in my theme but not on Woocommerce templates where it gives an error.

Fatal error: Uncaught Error: Call to a member function get_price_html() on null in /Applications/MAMP/htdocs/stage/wp-content/themes/bright-theme/woocommerce/single-product/price.php:25 Stack trace: #0 /Applications/MAMP/htdocs/stage/wp-content/plugins/woocommerce/includes/wc-core-functions.php(249): include() #1 /Applications/MAMP/htdocs/stage/wp-content/plugins/woocommerce/includes/wc-template-functions.php(1524): wc_get_template('single-product/...') #2 /Applications/MAMP/htdocs/stage/wp-includes/class-wp-hook.php(288): woocommerce_template_single_price('') #3 /Applications/MAMP/htdocs/stage/wp-includes/class-wp-hook.php(312): WP_Hook->apply_filters(NULL, Array) #4 /Applications/MAMP/htdocs/stage/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #5 /Applications/MAMP/htdocs/stage/wp-content/themes/bright-theme/woocommerce/content-single-product.php(54): do_action('woocommerce_sin...') #6 /Applications/MAMP/htdocs/stage/wp-includes/template.php(724): require('/Applications/M...' ) #7 /Applications/MAMP/htdocs/stage/ in /Applications/MAMP/htdocs/stage/wp-content/themes/bright-theme/woocommerce/single-product/price.php on line 25

get_price_html() has to be called on an instance of $product . Maybe there is no product on your page and thats why it is null - which is again why your error says called get_price_html() on null .

As your're on a product there should be a product loaded, so you can try finding the product by calling global $product; before the method is called.

The error says that the method was called on line 25 - is this the line of the import?

We would need some more surrounding code to really track down the issue.

It was a query in my template file I was calling.

<?php endwhile; wp_reset_query(); ?>

Fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM