Search code examples
ajaxwordpresswoocommercecartstorefront

Woocommerce storefront_cart_link override has no effect


I'm trying to trim down a few things for a custom WooCommerce theme as a child of Storefront. I started by replacing the storefront_header_cart function to remove the full cart list, which worked as expected:

if ( ! function_exists( 'storefront_header_cart' ) ) {
    function storefront_header_cart() {
        if ( storefront_is_woocommerce_activated() ) {
            if ( is_cart() ) {
                $class = 'current-menu-item';
            } else {
                $class = '';
            }
            ?>
        <ul id="site-header-cart" class="site-header-cart menu">
            <li class="<?php echo esc_attr( $class ); ?>">
                <?php storefront_cart_link(); ?>
            </li>
        </ul>
            <?php
        }
    }
}

Then I wanted to change the text in the contents link itself. I did the exact same thing to override the default storefront behavior...

if ( ! function_exists( 'storefront_cart_link' ) ) {
    function storefront_cart_link() {
        ?>
            <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'storefront' ); ?>">
                <?php /* translators: %d: number of items in cart */ ?>
                <span class="count"><?php echo wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'storefront' ), WC()->cart->get_cart_contents_count() ) ); ?></span>
            </a>
        <?php
    }
}

... but it does nothing discernible. The full original link is displayed:

<a class="cart-contents" href="http://localhost/cart/" title="View your shopping cart">
    <span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>0.00</span> <span class="count">0 items</span>
</a>

Why the incongruent behavior?

  • WordPress: 5.1.1
  • WooCommerce: 3.5.6
  • Storefront: 2.4.5

Solution

  • This seems to be related to ajaxified cart fragments on Storefront header cart count.

    1. First to empty cart completely and check that it's not related to caching.
    2. The involved hook on ajaxified cart fragments is woocommerce_add_to_cart_fragments, so you should try to find some related hooked function code in Storefront source code.

    See those related thread around Ajax on header cart count: