Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Lukas Gächter
    @gchtr

    @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 %}
    Ricky Green
    @rgcouk
    Thanks @gchtr I'm going to read over this.
    Much appreciated

    Do i wrap {%
    ob_start();
    include ('woocommerce/cart/mini-cart.php');

    $context['minicart'] = ob_get_contents();
    ob_end_clean();
    %}

    Sorry, i'm newbie at twig, only started yesterday.
    Lukas Gächter
    @gchtr
    That’s okay. Everyone started like you. And no, that’s the part you would use if you don’t want to convert mini-cart.php to Twig. You’d probably use that in the PHP file where you load the rest of the header. In the twig file where you’d want to display the minicart, you would then do {{ 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.
    Ricky Green
    @rgcouk
    Arg, i think i get it, slowly, ha, thanks for being understanding :D
    Ricky Green
    @rgcouk
    Last thing i'll annoy you with @gchtr Do you know why i'm getting random numbers put into my website, i've had a floating "1" for a while, since i've been doing those bits i have a "0" next to where the cart should be.
    Lukas Gächter
    @gchtr
    @rgcouk Could be a function output that returns true (1) or false (0), which is directly echoed. This could come from your theme, or from a plugin.
    Steve Pfisterer
    @stevep
    ob_get_clean() without ? Sorry my formatting got messed up
    Lukas Gächter
    @gchtr
    So that would be $context['minicart'] = ob_get_clean();, without the ob_end_clean(), I guess?
    Steve Pfisterer
    @stevep
    Maybe... I should have prefaced it with "trying using". Though if it was happening before it probably isn't the issue
    Steve Pfisterer
    @stevep
    Never mind I think @gchtr you're exactly right with your original solution. I think this issue with random 1s and 0s is somewhere else. I was googling the issue without reading, first thing on the morning ( for me )
    Ricky Green
    @rgcouk
    These are the links
    Lukas Gächter
    @gchtr
    You are resetting $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.
    Steve Pfisterer
    @stevep
    ahh yes, I think {{ mini-cart }} is subtracting null from null and echoing 0
    Ricky Green
    @rgcouk
    Yes, it was defiantly that, it's gone but the cart still not showing.
    Steve Pfisterer
    @stevep
    what did you change it to?
    Ricky Green
    @rgcouk
    minicart
    Steve Pfisterer
    @stevep
    also as @gchtr said, you need to move $context = Timber::get_context(); above where you’re grabbing the mini cart… your setting $context['minicart'] = ob_get_contents(); and then blowing away the $context variable when you do $context = Timber::get_context(); after
    Lukas Gächter
    @gchtr
    $context = Timber::get_context();
    
    ob_start();
    include ('woocommerce/cart/mini-cart.php');
    $context['minicart'] = ob_get_contents();
    ob_end_clean();
    
    $context['posts'] = Timber::get_posts();
    Ricky Green
    @rgcouk
    I've also do that.
    done*
    How do i copy code with syntax highlighting like that please
    'code'
    '''
    Lukas Gächter
    @gchtr
    It has to be backticks
    Ricky Green
    @rgcouk
    $context = Timber::get_context();
    
    ob_start();
        include ('woocommerce/cart/mini-cart.php');
        $context['minicart'] = ob_get_contents();
    ob_end_clean();
    
    $context['posts'] = Timber::get_posts();
    
    $templates = array( 'pages/index.twig' );
    
    if ( is_home() ) {
        array_unshift( $templates, 'pages/home.twig' );
    }
    Timber::render( $templates, $context );
    Steve Pfisterer
    @stevep
    if you set $context['minicart'] = 'something else'; does it output something else?
    can see if the issue is with the include or something else
    Ricky Green
    @rgcouk
    Okay, will try.
    Nope, nothing displays still.
    Steve Pfisterer
    @stevep
    is it possible that page.php is getting executed instead of index.php? becuase minicart isn’t added to the context in page.php
    or single.php
    youd probably want to add it to all wordpress template php files
    Ricky Green
    @rgcouk
    Argh!! :D
    Yes, it worked, then i had to change back from something else, i've git a fatal error now, undefined method WC_proudct_simple
    Steve Pfisterer
    @stevep
    It's rendering for me but that method looks like it has a spelling error
    Ricky Green
    @rgcouk
    Hmm, doesn't render for me, strange
    Steve Pfisterer
    @stevep
    It does on mobile but not desktop
    Ricky Green
    @rgcouk
    That's strange, what do you think could cause that?
    Steve Pfisterer
    @stevep
    Not sure but it does seem like a clue
    Ricky Green
    @rgcouk
    Well, im not sure if it's mobile or if it's when you add something to the basket because i've just opened it up on my phone and added a product to the basket to see if the cart updated and it gave me that error.
    Did you add anything to the cart ?
    Yep, it's something when the customer add's i've gone incognito and it rendered the website for me.