如何在这个php函数中添加if语句

时间:2013-05-16 15:13:17

标签: php wordpress numbers cart woocommerce

我需要在下面的代码中插入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 ...

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

1 个答案:

答案 0 :(得分: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; ?>