尝试使用 Timber 在 Ajax 之后实现 Woocommerce 更新购物车 link

Try to realize Woocommerce pdate cart link after Ajax with Timber

我正在为 Wordpress 使用很棒的 Timber 插件,我尝试使用 Twig 模板来实现这一点 Gist,但我没有让它工作。

我的代码在 functions.php 中看起来像这样:

function my_wc_cart_count() {


    $count = WC()->cart->cart_contents_count;
    ?><a class="cart-contents" href="<?php echo wc_get_cart_url(); ?>"
         title="<?php _e('View your shopping cart'); ?>"><?php
    if ($count > 0) {
        ?>
        <span class="cart-contents-count"><?php echo esc_html($count); ?></span>
        <?php
    }
    ?></a><?php


}

add_action('mytheme_header_action', 'my_wc_cart_count');

这是我的 base.twig:

{% do action('mytheme_header_action') %}

有人解决这个问题吗?

提前致谢

我尝试了一些东西,然后自己搞定了。

这是我的解决方案:

add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');

function woocommerce_header_add_to_cart_fragment( $fragments )
{
    global $woocommerce;
    ob_start(); ?>

    <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> <?php echo $woocommerce->cart->get_cart_total(); ?></a>

    <?php
    $fragments['a.cart-contents'] = ob_get_clean();
    return $fragments;
}


function my_wc_cart_count() {
    global $woocommerce;

    ?>

    <div class="header_cart">
        <h5><a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php _e('Shopping Cart', 'home-shopper'); ?></a></h5>
        <div class="cart_contents">
            <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> <?php echo $woocommerce->cart->get_cart_total(); ?></a>
        </div>
    </div>
    <?php


}

add_action('mytheme_header_action', 'my_wc_cart_count');

在 base.twig 中仍然如此:

{% do action('mytheme_header_action') %}