Run calculate_shipping() on usermeta value update
-
I am working on a custom WooCommerce buildout for a client. It uses a custom shipping method plugin that calculates shipping costs based on API return values. Additionally, every customer has a piece of usermeta called
freight_inclusive
. If they have this enabled, shipping costs should zero out, but the cost of each item will slightly increase. This setting is toggle-able in the user admin panels.Toggling this changes the prices of each product correctly, BUT the shipping costs are not zeroed out until something in the cart is changed. That is, the shipping plugin is correctly responding to the usermeta value, but it doesn’t seem to run
calculate_shipping()
until the cart changes somehow (even hitting the ‘Update Cart’ button doesn’t work if nothing in the cart has changed). Instead, I want to trigger cart total recalculation whenever the usermeta value is updated, as below:add_action( 'edit_user_profile_update', 'save_freight_inclusive_toggle_field' ); function save_freight_inclusive_toggle_field( $user_id ) { if ( !current_user_can( 'edit_user', $user_id ) ) return false; update_user_meta( absint( $user_id ), 'freight_inclusive', wp_kses_post( $_POST['freight_inclusive'] ) ); //something like: WC()->cart->calculate_totals(); //or: WC_Shipping_Method->calculate_costs(); }
If I run the commented-out line, I get a 500 error. What is the proper way to instruct WooComm to re-calculate cart totals – or more specifically, to re-run the
calculate_shipping()
function from my custom shipping plugin – in this case?
- The topic ‘Run calculate_shipping() on usermeta value update’ is closed to new replies.