Yes, thats fine, but you maybe able to shed some light on something for me?
I've copied over mini-cart.php and importing it to my twig file but it display the PHP code, I'm guessing i've got to covert it to twig and call it like i do the other pages? $context['cart'] = WC()->cart->get_cart();, i just need to know if theres a standard, not going to say easy way because i know that rarely exists
@rgcouk The starting point for working with WooCommerce seems to be http://timber.github.io/timber/#woocommerce. That’s probably the only standard that exists.
When using Timber, you don’t necessarily have to convert everything to Twig. You could still use PHP. If you need the output of a PHP file in a Twig file, something like this might work:
ob_start();
include ('path/to/mini-cart.php');
$context['minicart'] = ob_get_contents();
ob_end_clean();
As you assumed, importing PHP code directly into Twig won’t work, so you’d have to convert it to Twig. Something like this might work:
$context['cart'] = WC()->cart;
In your Twig file you could maybe do something like this:
{% if not cart.is_empty }
{% do action( 'woocommerce_before_mini_cart_contents' ) %}
{% for cart_item_key, cart_item in cart.get_cart %}
{% set product = fn('apply_filters', 'woocommerce_cart_item_product', cart_item.data, cart_item, cart_item_key)}
{# … #}
{% endfor %}
{% endif %}
Do i wrap {%
ob_start();
include ('woocommerce/cart/mini-cart.php');
$context['minicart'] = ob_get_contents();
ob_end_clean();
%}
{{ minicart }}
to output the contents from mini-cart.php. But I can’t tell you if that works. As I said, I’m not familiar enough with WooCommerce.
ob_get_clean()?
instead of ob_end_clean()
http://stackoverflow.com/questions/8510532/using-include-within-ob-start
$context
in https://github.com/rgcouk/Atomic/blob/ba96c64a6a3d7ef7ba38ee9ee4c4571d249eb2d9/index.php#L16. The ob_start();
part should come after you use get_context()
. Maybe the 0 appears because when you use {{ mini-cart }}
in https://github.com/rgcouk/Atomic/blob/ba96c64a6a3d7ef7ba38ee9ee4c4571d249eb2d9/templates/blocks/header.twig#L26, it doesn’t exist. Also, I wouldn’t use variable names with hyphens. Use minicart
or mini_cart
instead.