'Wooocommce Shipping Plugin, how to get error message to show around the shipping calculator?

UPDATE: I've solved it by storing the messages I want to show in the cookie (WC()->session).

And querying that in the method outside of the class.

However, I'd still like to understand why add_action("hook", [$this, "method"]) didn't work inside of the class.

I'm working on a Shipping plugin and would like to show error messages during calcuate_shipping()

I've tried to add_action to various hook like "woocommerce_after_shipping_calculator" or "woocommerce_after_cart" or "woocommerce_after_shipping_calculator"

with:

add_action("woocommerce_after_shipping_calculator", [$this, show_error()])

inside the calculate_shipping

I can echo some text if I defined the add_action outside my plugin class and use a normal method, but then I'm not sure how I could grab that error msg without using update_option, for that particular time the user requested.

I know I can use wc_notice(msg, 'error') but that message is above the cart box and can be out of screen if the user is focused around the calculator.

From what I understand, woocommerce_after_shipping_calculator hook should come after calculate_shipping is called on my plugin, so it should display something?

Many thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source