我正在使用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') %}
有没有人可以解决这个问题?
提前致谢
答案 0 :(得分: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') %}