我想知道是否有人能帮我做点什么?
我在网上找到此代码:
// Add a WooCommerce Cart Total shortcode [cart_total]
add_filter(\'woocommerce_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\'); ?>"><i class="fa fa-shopping-cart "></i> <?php echo $woocommerce->cart->cart_contents_count;?></a>
<?php
$fragments[\'a.cart-contents\'] = ob_get_clean();
return $fragments;
}
add_shortcode( \'cart_total\',\'cart_total_function\' );
function cart_total_function( $cart_total ){
global $woocommerce;
$cart_total = \'<a class="cart-contents" href="#"><i class="fa fa-shopping-cart "></i> \'.$woocommerce->cart->cart_contents_count.\'</a>\';
/* Original code */
// $cart_total = \'<a class="cart-contents" href="\'.$woocommerce->cart->get_cart_url().\'" title="View your shopping cart">\'.sprintf(_n(\'%d item\', \'%d item(s) \', $woocommerce->cart->cart_contents_count, \'woothemes\'), $woocommerce->cart->cart_contents_count).\' \'.$woocommerce->cart->get_cart_total().\'</a>\';
return $cart_total;
}
这允许我在我的网站上的任何地方使用[cart\\u total]快捷码。它工作得很好,但我想做的是,当购物车为空时,不要吐出任何东西,因此基本上只有当有人向购物车中添加了一些东西时,输出才会显示出来。这纯粹是出于美学原因。
我很确定我需要利用if 语句来检查cart\\u contents\\u计数,但无法确定将if语句放置在何处。我已经尝试了大约12种不同的方法,但都没有成功。几乎所有人都在抛出错误。实际上,我只知道足够危险的代码,所以不知道我在做什么是对的还是错的。只有当它起作用时,我才知道我“得到”了它!!;-)
在此方面的任何帮助都将不胜感激。
非常感谢。
致以最良好的祝愿,
做记号
SO网友:Sam
您可以检查购物车内容,然后显示购物车,如果它有一些项目
// Add a WooCommerce Cart Total shortcode [cart_total]
add_filter(\'woocommerce_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\'); ?>"><i class="fa fa-shopping-cart "></i> <?php echo $woocommerce->cart->cart_contents_count;?></a>
<?php
$fragments[\'a.cart-contents\'] = ob_get_clean();
return $fragments;
}
add_shortcode( \'cart_total\',\'cart_total_function\' );
global $woocommerce;
if ( $woocommerce->cart->get_cart_contents_count() != 0 ) {
function cart_total_function( $cart_total ){
global $woocommerce;
$cart_total = \'<a class="cart-contents" href="#"><i class="fa fa-shopping-cart "></i> \'.$woocommerce->cart->cart_contents_count.\'</a>\';
/* Original code */
// $cart_total = \'<a class="cart-contents" href="\'.$woocommerce->cart->get_cart_url().\'" title="View your shopping cart">\'.sprintf(_n(\'%d item\', \'%d item(s) \', $woocommerce->cart->cart_contents_count, \'woothemes\'), $woocommerce->cart->cart_contents_count).\' \'.$woocommerce->cart->get_cart_total().\'</a>\';
return $cart_total;
}
}