Update cart status without leaving the page | Woocommerce 3.4+

Asked

Viewed 461 times

4

<?php global $woocommerce; 
    if ( sizeof( WC()->cart->get_cart() ) < 1 ) { ?>
    <div style="width: 25%;" class="footer-section <?php echo esc_html($woo);?>">
        <a href="<?php echo get_home_url();?>" title="Main"><i class="fa  fa-home"></i></a>
    </div>
<?php } else { ?>
    <div style="width: 20%;" class="footer-section <?php echo esc_html($woo);?>">
        <a href="<?php echo get_home_url();?>" title="Main"><i class="fa  fa-home"></i></a>
    </div>
<?php } ?>    

Cart status only updates if you refresh the page.
I want the status to be updated, how to make this confirmation without changing page or using F5?

Thanks in advance!

1 answer

2


you can use the "woocommerce_add_to_cart_fragments" hook that Woocommerce triggers when updating the cart. The function below will replace the HTML element with a new one.

functions.php:

function meu_tema_add_to_cart_fragment( $fragments ) {
    global $woocommerce;
    $w = ($woocommerce->cart->cart_contents_count<1) ? 'width:25%' : 'width:20%';
    ob_start();
    ?>

    <div style="<?php echo $w ?>" class="footer-section <?php echo esc_html($woo);?>">
        <a href="<?php echo get_home_url();?>" title="Main"><i class="fa  fa-home"></i></a>
    </div>

    <?php
    $fragments['.footer-section'] = ob_get_clean();
    return $fragments;
}

add_filter( 'woocommerce_add_to_cart_fragments', 'meu_tema_add_to_cart_fragment' );

Reference: https://docs.woocommerce.com/document/show-cart-contents-total/

  • Leonel, Thanks for your comment! Both post are Working Great. The problem is using a Function Outside functions.php.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.