1

我需要if在下面的代码中插入一条语句:

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;
}

这是针对 woocommerce 的。它按内容编号显示购物车。我需要一个if类似这样的声明:

if ( sizeof( $woocommerce->cart->cart_contents ) == 0 ) {
echo 'empty cart';
}
else ...

这样当购物车为空时,它会显示一条消息。我不知道如何将其写入该函数。

4

1 回答 1

1

替换这个

<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 if((sizeof($woocommerce->cart->cart_contents)) == 0): 
    echo 'empty cart';
?>
<?php else: ?>
  <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 endif; ?>
于 2013-05-16T15:28:53.483 回答