2

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
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Randy Hall
  • 7,716
  • 16
  • 73
  • 151
  • @LoicTheAztec I thought this initially, but I do not see any asynchronous calls going out on the page load that would account for this. I'll dig a little deeper, but without a call to get the fragment I'm unsure how this could be happening. – Randy Hall Mar 16 '19 at 13:38
  • 1
    @LoicTheAztec I think that solves the future problem I'd run into once I was updating the cart, but currently on page load I cannot find any async call to get the fragment. Also, updating as suggested does not change the link on page load. I'm going to update the OP with several pieces of information in a moment that will probably be helpful. – Randy Hall Mar 16 '19 at 13:42
  • 1
    @LoicTheAztec I take that back... it is working. Might have been a caching issue combined with that. Please post as answer. – Randy Hall Mar 16 '19 at 13:44

1 Answers1

3

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:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399