• Phil

    (@owendevelopment)


    Working on a site with an unusual setup and experiencing something weird during testing.

    We have WP Multisite (subfolders) running with 2 Woocommerce stores (2x site-activated) and switches betweens stores/carts/accounts etc. Main store is on site 1 and wholesale store on site 2.

    I’m implementing AJAX in the header cart to auto-update the values without refresh.

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

    What I’m finding is:

    1. Once visited cart on site 1, and switch to site 2 store, cart link is for site 1. Clicking the link should take me to site/site2/basket
    2. On Safari (Mac), the cart values for a fresh user are £0.001 on both carts

    My question is, can I duplicate the above code into 2 functions, one for each basket, so there could be $fragments1 and $fragments2 so I could have both carts saving different fragments?

    If I removed the function, the values and links work correctly. Seems the carts and cart links are saved/cached but I need both carts independant.

  • The topic ‘Weird issue with Cart Ajax Fragments – multiple $fragments?’ is closed to new replies.