Update cart status without leaving the page | Woocommerce 3.4+

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 refreshes if you refresh the page. Do I want the status to be updated, how can I make this confirmation without changing pages or using F5?

Thanks in advance!

    
asked by anonymous 15.06.2018 / 12:27

1 answer

2

You can use the "woocommerce_add_to_cart_fragments" hook that Woocommerce fires when you update 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: link

    
20.06.2018 / 15:36