Home > OS >  How to check if Woocommerce cart page is updated dynamically and show/update notifications
How to check if Woocommerce cart page is updated dynamically and show/update notifications

Time:12-22

I'm using the below code to show some basic notifications, however on the cart page I'm using the ajax auto update cart plugin, which updated the cart dynamically when the number of products is changed/removed.

How to make the notifications code work even when the cart is updated dynamically? As when dynamically the amount is reached to show a success message. Now I'm getting a negative number when the amount is passed rather than seeing a success message. The success message appears only after page refresh, also the negative number also disappears only after page refresh.

//Free shipping reminder
add_action( 'woocommerce_before_cart', 'show_left_amount_for_free_shipping' );
function show_left_amount_for_free_shipping() {

$total_cart_amount = WC()->cart->cart_contents_total;

$free_shipping_amount = 50;

$left_amount = $free_shipping_amount - $total_cart_amount;

if ( $left_amount > 0 ) {
    echo '<p >Добавете допълнително продукти на стойност <strong>' . wc_price( $left_amount ) . '</strong>, за да получите безплатна доставка.</p>';
    // Add fragments
    add_filter('woocommerce_add_to_cart_fragments', 'free_shipping_left_amount_fragment');
    function free_shipping_left_amount_fragment( $fragments ) {
      ob_start();
      ?>
        <p >Добавете допълнително продукти на стойност <strong><?php echo wc_price( $left_amount ); ?></strong>, за да получите безплатна доставка.</p>
      <?php
      $fragments['p.free-shipping-left-amount'] = ob_get_clean();
      return $fragments;
    }
}
}
//Refresh the left amount for free shipping when the cart is updated
add_filter('woocommerce_add_to_cart_fragments', 'refresh_left_amount_for_free_shipping');
function refresh_left_amount_for_free_shipping($fragments) {
    $total_cart_amount = WC()->cart->cart_contents_total;
    $free_shipping_amount = 50;
    $left_amount = $free_shipping_amount - $total_cart_amount;
    ob_start();
    ?>
    <p >Добавете допълнително продукти на стойност <strong><?php echo wc_price( $left_amount ); ?></strong>, за да получите безплатна доставка.</p>
    <?php
    $fragments['p.free-shipping-left-amount'] = ob_get_clean();
    return $fragments;
}
// Show success message when free shipping amount is reached
add_filter('woocommerce_add_to_cart_fragments', 'show_success_message_when_free_shipping_amount_is_reached');
function show_success_message_when_free_shipping_amount_is_reached($fragments) {
    $total_cart_amount = WC()->cart->cart_contents_total;
    $free_shipping_amount = 50;
    $left_amount = $free_shipping_amount - $total_cart_amount;
    ob_start();
    if($left_amount <= 0){
        ?>
        <p >Благодарим Ви! Вие достигнахте сумата за безплатна доставка.</p>
        <?php
    }
    $fragments['p.free-shipping-success-message'] = ob_get_clean();
    return $fragments;
}
//Remaining amount for a discount
add_action( 'woocommerce_before_cart', 'show_left_amount_for_discount' );
function show_left_amount_for_discount() {

$total_cart_amount = WC()->cart->cart_contents_total;

$discount_amount = 100;

$left_amount = $discount_amount - $total_cart_amount;

if ( $total_cart_amount > 50 && $total_cart_amount < 100 ) {
    echo '<p >Добавете допълнително продукти на стойност <strong>' . wc_price( $left_amount ) . '</strong>, за да получите 10% отсъпка.</p>';
    // Add fragments
    add_filter('woocommerce_add_to_cart_fragments', 'discount_left_amount_fragment');
    function discount_left_amount_fragment( $fragments ) {
      ob_start();
      ?>
        <p >Добавете допълнително продукти на стойност <strong><?php echo wc_price( $left_amount ); ?></strong>, за да получите 10% отсъпка.</p>
      <?php
      $fragments['p.discount-left-amount'] = ob_get_clean();
      return $fragments;
    }
}
}
//Refresh the left amount for discount when the cart is updated
add_filter('woocommerce_add_to_cart_fragments', 'refresh_left_amount_for_discount');
function refresh_left_amount_for_discount($fragments) {
    $total_cart_amount = WC()->cart->cart_contents_total;
    $discount_amount = 100;
    $left_amount = $discount_amount - $total_cart_amount;
    ob_start();
    ?>
    <p >Добавете допълнително продукти на стойност <strong><?php echo wc_price( $left_amount ); ?></strong>, за да получите 10% отсъпка.</p>
    <?php
    $fragments['p.discount-left-amount'] = ob_get_clean();
    return $fragments;
}


CodePudding user response:

Your calculations and other things are ok in your code, but your fragment logic is wrong. I have minimized your code and corrected the logical issues.

I have created a function vh_wc_get_custom_cart_messages() that will validate and create all the messages which I am wrapping with div.custom-wc-cart-messsages and this same class is being used with fragments.

In your old code, you'll be adding the fragments but the issue is your messages HTML classes were not constant classes.

vh_wc_get_custom_cart_messages() function will be called to display the message on the cart page on page load and also will be used with fragments so that you don't have to write the same messages and logic again n again.

Please check the below code and let me know how it works for you. I have tested the code on the dev website and it works fine for me.,

/**
 * This function will validate cart amount conditions
 * and will return text with HTML which we'll use with
 * 'woocommerce_before_cart' and 'woocommerce_add_to_cart_fragments'
 * to avoid writing code twice.
 */
function vh_wc_get_custom_cart_messages() {
    // Get cart contents total.
    $total_cart_amount = WC()->cart->get_cart_contents_total();

    $free_shipping_amount           = 50;
    $remianing_free_shipping_amount = $free_shipping_amount - $total_cart_amount;

    $discount_amount           = 100;
    $remaining_discount_amount = $discount_amount - $total_cart_amount;

    ob_start();

    // this next div class is what we'll use in fragment.
    echo '<div >';

    // Do validation and write messages.
    if ( $remianing_free_shipping_amount > 0 ) {
        echo '<p >Добавете допълнително продукти на стойност <strong>' . wc_price( $remianing_free_shipping_amount ) . '</strong>, за да получите безплатна доставка.</p>';
    } else {
        echo '<p >Благодарим Ви! Вие достигнахте сумата за безплатна доставка.</p>';
    }

    // Do validation and write messages.
    if ( $total_cart_amount > 50 && $total_cart_amount < 100 ) {
        echo '<p >Добавете допълнително продукти на стойност <strong>' . wc_price( $remaining_discount_amount ) . '</strong>, за да получите 10% отсъпка.</p>';
    }

    echo '</div>';

    $content = ob_get_clean();

    return $content;
}

/**
 * Display custom cart messages before the cart table.
 */
function vc_wc_custom_display_custom_cart_messages() {
    // wp_kses_post function allowed only valid HTML tags. We using it for XSS protection.
    echo wp_kses_post( vh_wc_get_custom_cart_messages() );
}
add_action( 'woocommerce_before_cart', 'vc_wc_custom_display_custom_cart_messages' );

/**
 * Add custom cart fragments
 *
 * @param array $fragments Array of fragments.
 * @return array
 */
function vc_wc_custom_cart_fragments( $fragments ) {
    $fragments['div.custom-wc-cart-messsages'] = wp_kses_post( vh_wc_get_custom_cart_messages() );

    return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'vc_wc_custom_cart_fragments' );
  • Related