M
M
makboriska2022-01-18 19:50:06
WooCommerce
makboriska, 2022-01-18 19:50:06

How to change a tag after ajax request?

I have an online store on woocommerce, ajax adding goods to the cart works, everything is good, but I want that if the cart is empty, when a person clicks on the cart, he will see the modal window "Cart is empty", and if he added a product, then it would be transferred to the cart in fact, the catch is that this whole thing should work on ajax

HTML cart output

<a href="/" class="cart-btn header-top-item flex-container">
                            <div class="cart-icon"></div>
                            <div class="text-col">
                                <span class="title">Shopping Cart</span>
                                <div class="cart__subtotal">
                <span class="subtitlew">
                                    <?php
                                    $total_cart = WC()->cart->get_displayed_subtotal();
                                    echo  $total_cart;
                                    ?>
                                </span>
                                    <a href="/store/" class="url__nice"></a>
                                    <span class="subtitle"><?php echo get_woocommerce_currency_symbol(); ?></span>
                                </div>
                            </div>
                        </a>


And here is the ajax request

add_filter('woocommerce_add_to_cart_fragments', 'header_add_to_cart_fragment');

function header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;
    ob_start();
    ?>
    <a href="/store/checkout" class="url__nice"></a>
    <span class="subtitlew">(<?php echo WC()->cart->get_cart_total(); ?>)</span>
    <?php
    $fragments['.subtitlew'] = ob_get_clean();
    return $fragments;
}


Is it possible to somehow change completely html when adding a product to the cart?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Newto, 2022-01-19
@Newto

$.post('/basket', 'checkbusket':1, function(data){
  if(data == 'пусто'){  
    вашаФункцияМодальногоОкна('сообщение');
  } else {
    location='/basket';
  }
})

And in /busket check whether there is something in the basket or not, and depending on the check, give 'empty' or 'is'.
ps 'empty' and 'is' written for clarity, so, of course, you don't need to do it, give 1 or 0.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question