Search code examples
phpjquerywordpresswoocommercecart

Remove word from Woocommerce count span in cart header


Does somebody know, where I can find the .php file in woocommerce, where I can remove the word "item" or "items" from the last span?

I've tried it with some jQuery Code but it only works when I load the page completely. When I click add to cart or remove from cart an item, the cart only reload in woocommerce without my .js file to remove the two words.

Can anybody help me?

Thank you

$('.count').html($('.count').html().replace(' items',''));
$('.count').html($('.count').html().replace(' item',''));
<a class="cart-contents" href="http://*****.de/warenkorb/" title="View your shopping cart">
<span class="amount">0,00&nbsp;€</span>
<span class="count">0 items</span><!--Here I want to remove the Word items to show just the number-->
</a>


Solution

  • After a few days of breaking my head about this i've found a solution (I'm so happy and angry too because when you know the answer the solution is so easy).

    First you have to find the file woocommerce/templates/cart/mini-cart.php to overwrite our function.

    When you've found it you have to find following line:

    <?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s &times; %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?></li>
    

    After you've found it you have to insert following code under the line:

    <?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 esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'storefront' ); ?>">
                <span class="count"><?php echo sprintf (_n( '%d', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?></span>
            </a> 
    <?php
    
            $fragments['a.cart-contents'] = ob_get_clean();
    
            return $fragments;
        }
    ?>
    

    Now you have to save the file and reload the page and put something in your cart (or remove) to update your cart. Know it should be done! :-)

    If you want to add your price to the header too you also have to add above <span class="count"> following lines of code:

    <span class="amount"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span>
    

    If you have any questions you can always comment me…