0

I am working on a woocommerce based website. I have used the suggested code snippet found here to add a cart-contents link to my header. The code is as follows:

In your functions.php

<?php

// Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php)
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );

function woocommerce_header_add_to_cart_fragment( $fragments ) {
    ob_start();
    ?>
    <a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a> 
    <?php

    $fragments['a.cart-contents'] = ob_get_clean();

    return $fragments;
}

The code is ajaxified so that automatically adds the number and price of the product to the link.

in your theme file

<a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>

The issue I am facing is that I would like to add an additional class to the link like so:

 <a class="cart-contents fi-shopping-cart" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>

This additional class is for a font icon created by zurb found here. All that is required for the font icon to be displayed is that you add it to a div or li as a class. The issue is that ever time I refresh the page the font icon shows for a split second and then disappears. I would really appreciate the help. Thank you.

1 Answer 1

2

You need to add your class of fi-shopping-cart to both instances of the cart link element: In the shorter, single-line snippet you've used your theme file, and in the the custom code you added to your functions.php file. If the custom class is present in only one snippet, it will be replaced when the other is loaded in.

The updated code is below. After making your changes, empty your cart, then add an item again. Woocommerce caches data, so the changes may not appear to work at first.

Here's the code for functions.php - the only difference is that the icon class has been added as a class to the element:

<?php

// Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php)
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );

function woocommerce_header_add_to_cart_fragment( $fragments ) {
    ob_start();
    ?>
    <a class="cart-contents fi-shopping-cart" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a> 
    <?php

    $fragments['a.cart-contents'] = ob_get_clean();

    return $fragments;
}

And here's the updated code for the cart link element for your theme file -you've already updated this, but it's noted here for completeness:

<a class="cart-contents fi-shopping-cart" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
1
  • Thank you @Chris, I had added the classes to both links before but did not clear the cart so the changes did not apply. Thank you very much for your help, I appreciate it CHEERS!!
    – steamfunk
    Jun 19, 2015 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.