How to add an if statement into this php function

I need to insert an if statement in the code below:

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

This is for woocommerce. It displays cart by content number.
I need an if statement similar to this:

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

So that when the cart is empty it displays a message. I don’t know how to write this into that function.

Related posts

Leave a Reply

1 comment

  1. Replace this:

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


    With this:

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