0

我正在为 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') %}

有没有人解决这个问题?

提前致谢

4

1 回答 1

1

我尝试了一些东西并自己得到了它。

这是我的解决方案:

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') %}
于 2016-10-27T20:58:08.257 回答