Skip to content

Commit

Permalink
Fix bug when product global is not present on singular product pages
Browse files Browse the repository at this point in the history
  • Loading branch information
gchtr committed Jan 13, 2023
1 parent 9008665 commit 345e711
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Timber Integration for WooCommerce

## 0.7.1 - 2023-01-13

- Fixed a bug when product global is not present on singular product pages.

## 0.7.0 - 2023-01-13

- Updated PHP requirements to allow PHP 8.0 and higher.
Expand Down
29 changes: 28 additions & 1 deletion lib/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public static function init( $args = array() ) {
add_filter( 'wc_get_template', array( $self, 'maybe_render_twig_template' ), 10, 3 );
add_filter( 'wc_get_template_part', array( $self, 'maybe_render_twig_template_part' ), 10, 3 );

// Fixes product global for singular product pages.
add_action( 'woocommerce_before_main_content', array( $self, 'maybe_setup_product_data' ), 1 );

// Add WooCommerce context data to normal context.
add_filter( 'timber/context', array( $self, 'get_woocommerce_context' ) );

Expand Down Expand Up @@ -165,6 +168,7 @@ public function maybe_render_twig_template( $located, $template_name, $args ) {

// If a file was found, render that file with the given args, otherwise, return the default location.
if ( $file ) {
// Setup missing product global.
global $product, $post;

if ( ! $product ) {
Expand Down Expand Up @@ -250,6 +254,29 @@ public function maybe_render_twig_template_part( $template, $slug, $name = '' )
return false;
}

/**
* Fixes product global for singular product pages.
*
* On singular product pages, the product global is set up when the template loads. But it is
* reset when Timber calls the 'the_post' action repeatedly on objects that are not
* WooCommerce posts.
*
* By hooking into the 'woocommerce_before_main_content' action, we can set up the product
* global again.
*
* @since 0.6.2
* @link https://github.com/timber/timber/issues/1639
* @see Remove this for Timber 2.0?
*/
public function maybe_setup_product_data() {
// Setup missing product global.
global $product, $post;

if ( ! $product ) {
$product = wc_setup_product_data( $post );
}
}

public static function convert_objects( $args ) {
// Convert WP objects to Timber objects.
foreach ( $args as &$arg ) {
Expand Down Expand Up @@ -321,7 +348,7 @@ public static function maybe_convert_to_collection( $args ) {
* the corresponding Twig file. It builds up an array with Twig templates to check for. Timber
* will use the first Twig file that exists. In addition to the default WooCommerce template
* files, there are some opininated "Goodies" that can make your life easier. E.g., you don’t
* have to to use *woocommerce/single-product.twig*, but can use *woocommerce/single.twig*.
* have to use *woocommerce/single-product.twig*, but can use *woocommerce/single.twig*.
*
* If you have your own solution going on or need to do more checks, you don’t have to call this
* function.
Expand Down

0 comments on commit 345e711

Please sign in to comment.