Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

woocommerce.php $product = wc_get_product( $context['post'] -> ID ) cannot get correct result #1384

Closed
qycary opened this issue Apr 8, 2017 · 5 comments

Comments

@qycary
Copy link

qycary commented Apr 8, 2017

I follow the doc to integrate woocommerce (https://github.com/jarednova/timber/wiki/WooCommerce-Integration)
but in single-product.twig {% do action('woocommerce_single_product_summary') %} can not get the correct result (it always get the same product price, add cart button point the same product )


I debug display my themes woocommerce.php the `$product = wc_get_product($context['post'] -> ID)` did not get the correct result(`$product` always point the same product), then I debug `plugins/woocommerce/templates/single-product /price.php` found `global $product` always point the same product
my `woocommerce.php` code
<?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;

 
    Timber::render('views/woo/single-product.twig', $context);
  
    //echo '<pre>';
    //print_r($product );
    //echo '</pre>';
}

cart page, checkout page and my_account page are use shortcode to show, so woocommerce.php only add single-product.

$context['post'] display correct sing-product information,
$context['post'] -> ID display correct sing-product ID
but when do wc_get_product( $context['post'] -> ID ) $product always show the same product info


my `single-product.twig`
{% extends 'template.twig' %}


{% block content %}

  <div style="max-width:1000px; margin:auto; padding-top:20px;">
    {% do action('woocommerce_before_single_product') %}
  </div>
  {% if post.get_field('product_hero_image') %}
    <div class="layout__block product-hero">
      <div class="wrapper">
        <div class="product-hero__wrapper">
          <div class="product-hero__wrapper__media">
            <img src="{{TimberImage(post.get_field('product_hero_image')).src}}"/>
          </div>
          <div class="product-hero__wrapper__action">
            {% do action('woocommerce_single_product_summary') %}
          </div>
          {% do action('woocommerce_after_single_product') %}
        </div>
      </div>
    </div>
  {% endif %}
  </div>

  {{ parent() }}
{% endblock %}

@jacobimpson
Copy link

I tested your code in a fresh project and wc_get_product was returning the correct product object without any issues. Are you running the latest version of Woocommerce (3.0 at the time of writing)? Some older versions will use get_product($id), but that has since been depreciated.

@qycary
Copy link
Author

qycary commented Apr 12, 2017

Hi
Thank you for answering me.
woocommerce version is 2.6.14, woocommerce 3.0 and 3.0.1 dropdown button cannot display normally, so I use 2.6.14
I solved by add {{ fn('the_post') }} in single-product.twig

@ahbyass
Copy link

ahbyass commented Jan 24, 2018

Also having this issue

@CaryQY
Copy link

CaryQY commented Jan 26, 2018

Hi, @AlexanderBMAS
try this demo

{% extends 'template.twig' %}


{% block content %}

  {{fn('the_post')}}  # this code is important to solve problem


  <div style="max-width:1000px; margin:auto; padding-top:20px;">
    {% do action('woocommerce_before_single_product') %}
  </div>
  {% if post.get_field('product_hero_image') %}
    <div class="layout__block product-hero">
      <div class="wrapper">
        <div class="product-hero__wrapper">
          <div class="product-hero__wrapper__media">
            <img src="{{TimberImage(post.get_field('product_hero_image')).src}}"/>
          </div>
          <div class="product-hero__wrapper__action">
            {% do action('woocommerce_single_product_summary') %}
          </div>
          {% do action('woocommerce_after_single_product') %}
        </div>
      </div>
    </div>
  {% endif %}
  </div>

  {{ parent() }}
{% endblock %}

compare my old single-product.twig in description, I just add {{fn('the_post')}} then it can work

@gchtr
Copy link
Member

gchtr commented Apr 30, 2018

I’m gonna go ahead and suggest that you try out the new Timber integration for WooCommerce.

From what I see, your issue could be solved if you’d use the integration like this:

  1. Install the integration.
  2. After you’ve initialized the integration, update the woocommerce.php file.
  3. Move your views/woo/single-product.twig file to views/woocommerce/single-product.twig.

Now this would probably break your sidebar. You could add it to the WooCommerce context:

functions.php

add_filter( 'timber/woocommerce/context', function( $context ) {
    $context['sidebar'] = Timber::get_widgets( 'shop-sidebar' );
    
    return $context;
} );

Or, you could also add it to the top of woocommerce.php:

add_filter( 'timber/context', function( $context ) {
    $context['sidebar'] = Timber::get_widgets( 'shop-sidebar' );
    
    return $context;
} );

@gchtr gchtr closed this as completed Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants