mindkomm / timber-integration-woocommerce Goto Github PK
View Code? Open in Web Editor NEWWooCommerce integration for Timber
License: MIT License
WooCommerce integration for Timber
License: MIT License
Hi there,
When do you plan to update your package to be PHP 8 compatible? The problem I'm facing is in the platform requirements which is only PHP 7., when I hope it was PHP 7. || PHP 8.*
Hi!
I'm testing out this integration (I like the way it allows you to use twig partials intead of overriding the complete wc templates!) but I'm fighting against a couple of issues...
Fatal error: Uncaught Error: Call to a member function is_on_sale() on string
in P:\glocal\dev_clienti\wp-content\plugins\woocommerce\templates\single-product\sale-flash.php on line 25
Call Stack:
include()
wp-content/plugins/woocommerce/includes/wc-core-functions.php:345
wc_get_template()
wp-content/plugins/woocommerce/includes/wc-template-functions.php:1609
woocommerce_show_product_sale_flash()
wp-includes/class-wp-hook.php:292
WP_Hook::apply_filters()
wp-includes/class-wp-hook.php:316
WP_Hook::do_action()
wp-includes/plugin.php:484
do_action()
wp-content/plugins/woocommerce/templates/content-single-product.php:43
require()
wp-includes/template.php:732
load_template()
wp-content/plugins/woocommerce/includes/wc-core-functions.php:284
wc_get_template_part()
wp-content/plugins/woocommerce/templates/single-product.php:37
include()
wp-includes/template-loader.php:106
require_once()
wp-blog-header.php:19
require()
index.php:17
Warning | count(): Parameter must be an array or an object that implements Countable in wp-includes/post-template.php:319 | Plugin: woocommerce
Notice | Trying to access array offset on value of type null in wp-includes/post-template.php:325 | Plugin: woocommerce
I'm on the latest wc (5.5.1).
I've tried the integration with my theme, but also with the Timber starter, facing the same 2 issues.
Any help / advice would be much appreciated :-)
Thanks!
When developing an classic theme (not with Timber integrated), WooCommerce templates can be overrided by placing php templates in i.e. woocommerce/cart/cart.php.
What i want is:
Is that possible in your integration?
Hi there,
I'm trying the Timber -> WC integration but, after cloning and copying the plugin source code into the WP plugins folder when I activate it I get this error from the CMS:
Warning: require_once( ... /wp-content/plugins/timber-integration-woocommerce/vendor/autoload.php): failed to open stream:
No such file or directory in ... /wp-content/plugins/timber-integration-woocommerce/timber-integration-woocommerce.php on line 10
It seems the plugin looks for a vendor
folder, which isn't included in the source code. It seems that the code should be placed in the Timber plugin instead, the directory structure is similar and there's a vendor/autoload.php
.
Am I missing something?
Note: I'm using latest WC 3.4.3 and Timber 1.7.1.
Since the ProductIterator only runs on WooCommerce Pages (see https://github.com/mindkomm/timber-integration-woocommerce/blob/master/lib/Timber/Integrations/WooCommerce/WooCommerce.php#L91) I propose to add a custom product setup function to call before rendering products.
Something like that:
final public function addProductFunction($twig) { $twig->addFunction(new \Twig_Function('wci_wc_setup_product_data', function($post_id) { wc_setup_product_data($post_id); return ''; })); return $twig; }
Hi,
Thank you for bringing this plugin!
It's quite embarrassing to ask, but where should I run composer require mindkomm/timber-integration-woocommerce
command? If I want to install as a plugin, do I have to compile by myself?
I tried to run in the root folder of my timber theme and inside the timber plugin folder, but this seems not working to both path.
It would be really nice if you can deliberate more about how to install πThank you!
Hey!
First of all - i appreciate the effort you put into developing this extension!
Since Timber v2 is close to release and offers numerous improvements, I'm curious about any plans to update the extension to be compatible with the new version.
Thanks in advance!
I'm getting the following error when using a single-product.twig
.
Warning: count(): Parameter must be an array or an object that implements Countable in /app/public/wp-includes/post-template.php on line 285
The $pages array is null at the point where the_content()
is called in wp-content/plugins/woocommerce/templates/single-product/tabs/description.php
It appears this is a common error when the_content()
is called outside of the loop. but as far as I can see it should be in it. At least it is when using a non-timber theme such as TwentySixteen.
I'm using the timber start theme.
Hi,
I'm trying to rearrange the way WC reviews are being displayed, but it seams that not all WC templates files can be overriden when i use your library.
If i add a single-product-reviews.twig
file in my views/woocommerce/
directory, it's not taken into account. It's working fine for single-product.twig
and friends.
Is it normal ?
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?
add_action('wp_ajax_mnn_sign_up', array($this, 'update_cart_total'));
add_action('wp_ajax_nopriv_mnn_sign_up', array($this, 'update_cart_total'));
public function update_cart_total() {
$context = Timber::context();
Timber::render( 'woocommerce/cart/cart-totals.twig', $context );
}
where twig is this {% do wc_action('woocommerce_before_cart_totals') %}
.
I get an error "CRITICAL Uncaught Twig\Error\SyntaxError: Unknown "wc_action" function. Did you mean "action"?". What's wrong?
I've created a pull request that resolves a Fatal error, due to the instantiation of "Twig_Function".
Pull request: #2
Hi, first thanks for your work.
I'm starting using your integration and I'm experimenting something confusing.
I'm using the 'if present, automatic loading / parsing template.twig file' feature, but I needed to change some basic php logic. I noticed that if i want to both override, for example woocommerce/single-product/add-to-cart/simple.php
AND templates/woocommerce/single-product/add-to-cart/simple.twig
, the twig template has the priority over the php and my woocommerce override would never be executed.
Am I the only on experiencing the situation ? And is it possible to :
I'm open to discuss, thank you !
Add license file
Update repo description (About βοΈ) "WooCommerce integration for Timber"
untick "Packages"
Fix typos
lib/Product.php:71: Mimick ==> Mimic
lib/WooCommerce.php:347: mimicks ==> mimics
Add timber/timber as dependency
diff --git a/lib/WooCommerce.php b/lib/WooCommerce.php
index 24c30eb..cb3f5b2 100644
--- a/lib/WooCommerce.php
+++ b/lib/WooCommerce.php
@@ -82,7 +82,7 @@ class WooCommerce {
// Add WooCommerce context data to normal context.
add_filter( 'timber/context', array( $self, 'get_woocommerce_context' ) );
- add_action( 'timber/twig/functions', array( $self, 'add_timber_functions' ) );
+ add_filter( 'timber/twig/functions', array( $self, 'add_timber_functions' ) );
}
/**
@@ -445,7 +445,7 @@ class WooCommerce {
/**
* Make function available in Twig.
*
- * @param \Twig_Environment $twig Twig Environment.
+ * @param \Twig\Environment $twig Twig environment.
*
* @return mixed
*/
clean up exported files
git archive HEAD | tar --list --exclude="lib" --exclude="lib/*"
move TODO-s from source code and README into GitHub issues
@gchtr What do you think?
Hi,
First off, this plugin for Timber makes developing with Timber, Twig and Woocommerce really simple and easy. The easy replacement of woocommerce with twig files is a godsend.
My question comes as part of that process. Normally, in the Woocommerce Status page, there is a section that tells you what template files that are out of date, allowing developers the chance to update their templates to the latest versions. I assume this is implemented through Woocommerce reading the Comment blocks at the top of each file and determining whether or not they are out of date. (based on this function in Woocommerce core)
However, I have noticed that when replacing a template with a custom one, this is not reflected in the list of overrides within the Status page.
Is there a way we can alter this plugin to reflect what twig templates are being used to override the PHP templates, so that we can see easily which templates might need tweaks?
For some background info, when developing I'm using this plugins \Timber\Integrations\WooCommerce\WooCommerce::render_default_template()
function in my woocommerce.php
file. Any other templates being replaced are happening automatically such as replacing the result-count.php
template file in the loop folder.
I look forward to hearing your response, and any thoughts you may have on implementing. Happy to try and find a solution if you could point me in the right direction :)
Thanks,
Ed
If I currently want to get data from a product I need to do so by using the woocommerce function which is not so nice. We could implement __call()
and __get()
method in the imber\Integrations\WooCommerce\Product
to be able to get data from the product and routes the function calls and property calls to the contained product variable.
Hi guys, at the start I very appreciate this project. It's awesome!
In examples you show this code at views/woocommerce/single-product/related.twig:
<?php foreach ( $related_products as $related_product ) : ?>
<?php
$post_object = get_post( $related_product->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product' );
?>
<?php endforeach; ?>
Replaced with
{% for post in wc.related_products %}
{{ fn('wc_get_template_part', 'content', 'product' ) }}
{% endfor %}
If you try it, in product detail it work's correct? If I look at related products, there are wrong data. Image, price and rating in all related products is inherited from main product.
After debugging loop displayed above I found that in file WooCommerce.php function maybe_render_twig_template_part stores in variable global $product currently displayed product, in global $post current related product's post. They are different, but code below passes to the context data based on global $product variable, which is currently displayed product.
if ( $product instanceof \WC_Product ) {
$context['product'] = $product;
$context['post_id'] = $product->get_id();
$context['post'] = Timber::get_post( $product->get_id() );
}
If you can test it, please look at it, if it works correct.
Iny my case, I fixed it with getting product from current post, if global post and product ID's differ, for such cases:
// Add current product to context.
if ( $product instanceof \WC_Product ) {
$context['product'] = $product;
$context['post_id'] = $product->get_id();
$context['post'] = Timber::get_post( $product->get_id() );
}
// MOD - Fix related.twig - there was passed parent product ID for related products
if($post->id !== $product->get_ID()){
$context['product'] = wc_setup_product_data( $post );
$context['post_id'] = $post->ID;
$context['post'] = Timber::get_post( $post->ID );
}
Currently not tested at whole project, if are there some places where should be that ID's different.
Thank you for feedback.
Problem
I am using the 'products' shortcode provided by WooCommerce. This executes class-wc-shortcode-products.php's product_loop(). This code sets the $GLOBALS['post'] but not $GLOBALS['product']. Because of this, and other code, the loop of products prints the same product over and over again (to be more specific, name of product is correct but image and price is from the first product).
It seems the code at the bottom of https://timber.github.io/docs/guides/woocommerce/ attempts to resolve but did not work for me.
Solution
Looking into I found this library's WooComerce.php maybe_render_twig_template_part() contains
global $post, $product;
if ( ! $product ) {
$product = wc_setup_product_data( $post );
}
The above sets the global $product when $product not yet set. This then leads to the global 'product' to always be the first one in a loop. To resolve the condition in the 'if' needs to be changed to:
global $post, $product;
if ( ! $product || $product->get_id() != $post->ID ) {
$product = wc_setup_product_data( $post );
}
Your library is fantastic! I love using Twig so its a must when doing WooCommerce. Your library makes it so much easier. If you can please resolve the above that would be AWESOME!
Thanks,
David
When I create a new Woocommerce product with two or more upsells, only one upsell shows.
Please note that my PHP skills aren't strong.
I would appreciate any help that anyone could give. I'm sure more information is needed and if you tell me what you need, I'm happy to provide it.
Thank you.
Hi,
That's a nice piece of code we have here, thanks for sharing your work !
Everything's fine with the default single-product.twig
& content-product.twig
files i copied from the package, but i have an issue with archive-product.twig
where the wc_get_loop_prop loop
is returning Timber\Integrations\WooCommerce\Product
instances of blog posts instead of products, here :
<ul class="products columns-{{ fn('wc_get_loop_prop', 'columns')|e }}">
{% if fn('wc_get_loop_prop', 'total') %}
{% for product in posts if fn('have_posts') %}
{{ dump(product) }} {# <- Got Blog posts as Timber\Integrations\WooCommerce\Product here #}
{{ fn('wc_get_template_part', 'content', 'product' ) }}
{% endfor %}
{% endif %}
</ul>
I could force the posts in the context in woocommerce.php
, but that might not be the best solution :
add_filter('timber/woocommerce/context', function($context){
if ( is_archive() ) {
$posts = Timber::get_posts();
$context['posts'] = $posts;
}
return $context;
});
Timber\Integrations\WooCommerce\WooCommerce::render_default_template();
Any hint on what could be the problem ?
Hi,
Not sure if this is intentional or not, but I think I've noticed a bug in the way the default template plugin renders templates.
In order to take full advantage of using this plugin I have been using:
\Timber\Integrations\WooCommerce\WooCommerce::render_default_template();
in my woocommerce.php
file so that I get the recommended files. I have noticed though, that when using this function, that any custom context that I have defined within the woocommerce.php
file is not pulled into this function, even though the context is defined at the top of your function using $context = Timber::get_context();
.
woocommerce.php
file in your theme\Timber\Integrations\WooCommerce\WooCommerce::render_default_template();
to render the recommended templates within this plugin{{ customText }}
or printing it out using dump(customText)
NULL
Add a variable to pass into the function, and then check if it's been passed in and merge the two together.
public static function render_default_template($customContext = array()) {
$context = Timber::get_context();
if(!empty($customContext)) {
$context = array_merge($context, $customContext);
}
If this is intentional, then please ignore me π If not, happy to introduce a PR to fix.
Thanks,
Ed
Hi !
I have an issue with the upsells products list on the single product page : the current product image is displayed for each upsell, instead of the upsell product image itself.
The upsell name & link are correct.
I tried to disable every piece of custom code i have but still got the same issue.
Any hints ?
Thanks
I get this error on the main WooCommerce site using the default archive-product.twig
FYI I cheated on the installation, my Docker setup didn't take into account Vendor folders so I had to manually set the php files from this plugin into the theme and required them all in functions.php
It should work right?
Fatal error:
Uncaught Twig_Error_Syntax: Unexpected "do_action" tag (expecting closing tag for the "block" tag defined near line 4). in /var/www/html/wp-content/themes/rut/views/archive-product.twig:80
Stack trace:
#0 /var/www/html/wp-content/plugins/timber-library/vendor/twig/twig/lib/Twig/TokenParser/Block.php(40): Twig_Parser->subparse(Array, true)
#1 /var/www/html/wp-content/plugins/timber-library/vendor/twig/twig/lib/Twig/Parser.php(192): Twig_TokenParser_Block->parse(Object(Twig_Token))
#2 /var/www/html/wp-content/plugins/timber-library/vendor/twig/twig/lib/Twig/Parser.php(105): Twig_Parser->subparse(NULL, false)
#3 /var/www/html/wp-content/plugins/timber-library/vendor/twig/twig/lib/Twig/Environment.php(716): Twig_Parser->parse(Object(Twig_TokenStream))
#4 /var/www/html/wp-content/plugins/timber-library/vendor/twig/twig/lib/Twig/Environment.php(774): Twig_Environment->parse(Object(Twig_TokenStream))
#5 /var/www/html/wp-content/plugins/timber-library/vendor/twig/twig/lib/Twig/Environment.php(452): Twig_Environment-> in /var/www/html/wp-content/themes/rut/views/archive-product.twig on line 80
When getting posts via ajax with admin-ajax
Timber\Integrations\WooCommerce\WooCommerce does not init.
Specifically highlighted when trying to use Product Twig Function.
Fixed by changing line 55
if ( is_admin() && ! wp_doing_ajax() ) {
return;
}
Ok, it's not about a problem, just to say thank you !
I use your woocommerce integration and it's very cool to use both timber and woocommerce, it works like a charm.
The project is still maintained ? What about the "next" branch release ?
I would participate on this project after the end of my current woocommerce/timber project.
Have a nice day !
Hello! I've just started using your fantastic Timber Integration for WooCommerce project. It's really made coding for WooCommerce about the most pleasurable it can be!
I've noticed one issue though so far, and it's a bit of a strange one.
In my theme, I have woocommerce.php
which has some code inside to do extra stuff depending on what part of WooCommerce we are viewing, e.g.
woocommerce.php
use Timber\Integrations\WooCommerce\WooCommerce;
use Timber\Timber;
$extra_context = [];
if (is_singular('product')) {
// default related products from a specific category instead of random
$extra_context['default_related_products'] = Timber::get_posts([
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 10,
'suppress_filters' => true,
'tax_query' => [
[
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'default-related-products',
]
],
]);
}
WooCommerce::render_default_template($extra_context);
What I've found is that inside my templates/woocommerce/single-product/related.twig
file, I am unable to see the variable default_related_products
if I {{ dump(_context) }}
.
It's so strange because in other template files, I've taken a similar approach and it works just fine e.g. in templates/woocommerce/archive-product.twig
I was setting a default header image for the page into $extra_context
and I could access this fine in the Twig context. It seems to just affect the templates/woocommerce/single-product.twig
and child templates/woocommerce/single-product/*.twig
files.
My workaround has been to replace the final line above with the following code, but I wanted to highlight the issue in case there was anything else going on.
woocommerce.php (amended final lines)
add_filter('timber/context', function ($context) use ($extra_context) {
return array_merge($context, $extra_context);
}, 50);
WooCommerce::render_default_template();
Doing this, everything is working as expected again.
I started to debug the Timber\Integrations\WooCommerce\WooCommerce
class and around line 376 if I debug the $context
before and after it has been merged using array_merge
I can see my default_related_products
data.
Any ideas what might be going on here?
Hi, I'm getting this error. All I did was installing the integration with Composer.
Uncaught Error: Class 'Timber\Integrations\WooCommerce\WooCommerce' not found
I'm using Timber 2.0.0-alpha.3 (maybe that's the problem?), installed with Composer as well.
Hello. I've never worked with WooCommerce before but build all my sites with Timber. When I found this integration I was really excited. However, I seem to be running into an error.
For a simple test to see if everything was hooked up, I tried to call the featured products via a shortcode in a Gutenberg Block template (ACF Blocks). Before I installed this integration, that shortcode was displaying the featured products through that block.
Here's the complete error:
Fatal error: Uncaught Error: Class 'Timber\LocationManager' not found in /app/public/wp-content/themes/roughhands/vendor/mindkomm/timber-integration-woocommerce/lib/Timber/Integrations/WooCommerce/WooCommerce.php:140 Stack trace: #0 /app/public/wp-includes/class-wp-hook.php(288): Timber\Integrations\WooCommerce\WooCommerce->maybe_render_twig_partial('/app/public/wp-...', 'loop/sale-flash...', Array) #1 /app/public/wp-includes/plugin.php(208): WP_Hook->apply_filters('/app/public/wp-...', Array) #2 /app/public/wp-content/plugins/woocommerce/includes/wc-core-functions.php(207): apply_filters('wc_get_template', '/app/public/wp-...', 'loop/sale-flash...', Array, '', '') #3 /app/public/wp-content/plugins/woocommerce/includes/wc-template-functions.php(1242): wc_get_template('loop/sale-flash...') #4 /app/public/wp-includes/class-wp-hook.php(286): woocommerce_show_product_loop_sale_flash(Array) #5 /app/public/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array) #6 /app/public/wp-includes/plugin.php(465): WP_Hook-> in /app/public/wp-content/themes/roughhands/vendor/mindkomm/timber-integration-woocommerce/lib/Timber/Integrations/WooCommerce/WooCommerce.php on line 140
Any help is much appreciated!
How would I be able to show featured posts within a loop. I assumed it would be the same as get related products but instead using wc_get_featured_product_ids() but that does not seem to work.
Would you be able to point me in the right direction on how to implement this?
Many thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.