WooCommerce Cart URL bug

admin2025-06-06  4

I use simple function to display WooCommerce cart in menu. However I can't resolve one issue. When I change the cart page URL in WooCommerce -> Settings -> Advanced -> Cart Page the cart URL is not updated. I guess that it has something deal with an Ajax function, but this the reference code from the official WooCommerce docs. When I add item to the cart the cart URL is updated. How to update cart URL without adding items?

My code:

<?php
    /**
    * Cart icon with total price and items count
    */
    function deo_woocommerce_cart_icon() {
        ?>
        <div class="nav__cart">
            <a class="nav__cart-url" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php echo esc_attr__( 'View your shopping cart', 'furosa' ); ?>">
                <span class="nav__cart-icon-holder">
                    <i class="ui-cart nav__cart-icon"></i>
                    <span class="nav__cart-count"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
                </span>
            </a>
        </div>
        <?php
    }

    /**
    * Show cart contents / total Ajax
    */  
    function deo_woocommerce_header_add_to_cart_fragment( $fragments ) {

        ob_start();
        deo_woocommerce_cart_icon();
        $fragments['.nav__cart-url'] = ob_get_clean();

        return $fragments;
    }
    add_filter( 'woocommerce_add_to_cart_fragments', 'deo_woocommerce_header_add_to_cart_fragment' );

I use simple function to display WooCommerce cart in menu. However I can't resolve one issue. When I change the cart page URL in WooCommerce -> Settings -> Advanced -> Cart Page the cart URL is not updated. I guess that it has something deal with an Ajax function, but this the reference code from the official WooCommerce docs. When I add item to the cart the cart URL is updated. How to update cart URL without adding items?

My code:

<?php
    /**
    * Cart icon with total price and items count
    */
    function deo_woocommerce_cart_icon() {
        ?>
        <div class="nav__cart">
            <a class="nav__cart-url" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php echo esc_attr__( 'View your shopping cart', 'furosa' ); ?>">
                <span class="nav__cart-icon-holder">
                    <i class="ui-cart nav__cart-icon"></i>
                    <span class="nav__cart-count"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
                </span>
            </a>
        </div>
        <?php
    }

    /**
    * Show cart contents / total Ajax
    */  
    function deo_woocommerce_header_add_to_cart_fragment( $fragments ) {

        ob_start();
        deo_woocommerce_cart_icon();
        $fragments['.nav__cart-url'] = ob_get_clean();

        return $fragments;
    }
    add_filter( 'woocommerce_add_to_cart_fragments', 'deo_woocommerce_header_add_to_cart_fragment' );
Share Improve this question edited Oct 22, 2018 at 9:32 Alexander asked Oct 22, 2018 at 7:23 AlexanderAlexander 1531 gold badge2 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Found, that was a PHP cache bug. If I open page in a new tab, it updates the URL. But if I stay on the same page, the URL stays in the cache, no matter how many times you reload the page.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749180973a317039.html

最新回复(0)