mindkomm / timber-integration-woocommerce

WooCommerce integration for Timber
MIT License
109 stars 20 forks source link

Cannot override template when using shortcode #11

Closed stljeff1 closed 5 years ago

stljeff1 commented 5 years ago

Hello,

Great tool, your work has been very helpful.

I am trying to create a page that would show products from various categories. The page would have products from multiple categories, so this is different then your traditional archive page.

It seems that the best (and maybe only) way of doing this is to use shortcodes Therefore, if I am using your integration, my page template would look like:

page-mypage.twig

{{ fn('do_shortcode', "[products category=my_cat_slug]") }}

The problem I am facing is that I can't seem to override the content-product template, like I am overriding other templates.

As far as I can tell, Woocommerce uses the following files when I use the Products shortcode: ~woo/templates/loop/loop-start.php for each product, use ~woo/templates/content-product.php ~woo/templates/loop/loop-end.php

So therefore, if I create ~theme/views/woocommerce/loop/loop-start.twig, ~theme/views/woocommerce/content-product.twig, and ~theme/views/woocommerce/loop/loop-end.twig, I should be able to construct my desired HTML. Is this correct understanding?

I am unable to override the content-product file. I can override the loop files. So far I've created archivie-product, single-product, and loop/add-to-cart templates, all templates that you demonstrate, but I am stumped with this content-product template.

do you have an example of customizing the content-single file, or using the products shortcode?

stljeff1 commented 5 years ago

Is there a better way of displaying products from multiple categories on the same page? I do not want to query by category, I want to have independent widgets that list products from a single cat.

stljeff1 commented 5 years ago

I solved my problem by creating a content-product.php template at the root of my theme.

After reseaching how WC implements shortcodes, I learned that the plugin calls wc_get_template_part( 'content', 'product' ); after setting necessary global variables. I assume your code wasn't intercepting that call, so I created a content-product.php template. Sure enough, WC used my template file once i created it. Here is what that file looks like:

global $post;
$context = Timber::get_context();
$context['post'] = $post;
Timber::render( 'woocommerce/content-product.twig', $context );

Here is how I decided to customize content-product

<div class='cell'>
    <div class="{{ fn('post_class') }}">
        {% do action('woocommerce_before_shop_loop_item') %}
    {% do action('woocommerce_before_shop_loop_item_title') %}
    {% do action('woocommerce_shop_loop_item_title') %}
    {% do action('woocommerce_after_shop_loop_item_title') %}
    {% do action('woocommerce_after_shop_loop_item') %}
  </div>
</div>

I still have my woocommerce file that gets used with Single and archive pages. what I am doing is on'y needed when using shortcodes on non woocommerce templates

tlygnersjo commented 5 years ago

Hi @stljeff1!

May i ask you if you figured out how you can use the same template file in a shortcode (content-product.php). I guess you use something like a tease-product.twig template for other pages (i.e. archive.twig)?

I'm in a kind of same situation as you were. I don't want to reinvent the wheel, so i would like to use the same template for "tease products" all over my timber theme.

Any idea how to solve that?

Any help would be greatly appreciated!

stljeff1 commented 5 years ago

Yes I could just as easily use tease-product.twig instead of content-product.twig.

I am using two different templates because I am using each template differently. I am using content-product.twig to show the product title, image, description, price, and Cart button. However, I am using tease-product.twig to only show the product title and image.

So yes, if you don't want to reinvent the well, just make your content-product.php use the tease-product.twig template. And remember, content-product.php is only used when leveraging the [products shortcode.

tlygnersjo commented 5 years ago

Thank you! Just got it sorted out.

in "content-product.php" i had to change the following row: $context['post'] = Timber::get_post();

to: $context['post'] = Timber::get_post($post->ID);