Z
Z
z1mak2016-10-29 15:38:27
WordPress
z1mak, 2016-10-29 15:38:27

How to update posts on Woocommerce cart page?

Hi everyone, I'm having a problem updating the cart page.

The store sells products that come in boxes, and there is a need for customers to form their own "boxes" with various products.

A rough description and implementation is here.

The question is -
For example, we added a minimum amount of purchases in the store and decided to show this in a message on the cart page if the amount is not reached, so that the person could not go to the calculation page.

wc_add_notice( sprintf( __( "Минимальная сумма  заказа %s $", 'woocommerce' ), $min_amount ), 'error' );

And everything works well, except for one thing, after adding products to the basket page, the recalculation occurs using ajax, but the message does not disappear. If you disable ajax, you still need to press the update button 2 times to display the message correctly (the previous amount is remembered in the message).
4b16a75a3b17435fa21000dc2318f1b1.png
It is necessary that the message disappears when the basket is recalculated. Now the first click does not lead to the desired result, you need to press the button 2 times. And the result is remembered past.
From what I've seen, this is a fairly well-known problem for people who use error messages to check their cart.
Here is the approximate code to understand the needs.
Code with only the minimum amount of purchases.
add_filter( 'woocommerce_update_cart_action_cart_updated', 'woocommerce_check_cart_quantities_for_class' , 5, 1);

function woocommerce_check_cart_quantities_for_class() {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$subtotal = WC()->cart->get_cart_subtotal();// выводиться в виде "$0.936,00" -> 936$
$min_amount = "$0.050,00";// 50$

    if($subtotal < $min_amount){

        wc_add_notice( sprintf( __( "Минимальная сумма  заказа %s $", 'woocommerce' ), $min_amount ), 'error' );
    
    }else{
        wc_add_notice( sprintf( __( "Теперь вы можете переходить на страницу расчета", 'woocommerce' ), ), 'notice' );
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question