From b462cd58bc6341df7934a3ac953961393d405cb5 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 15 Jul 2024 15:37:26 +0100 Subject: [PATCH] Store API: Ensure Store API recalculates cart totals prior to running validation hooks (#49455) * Recalc totals on checkout route before validation * changelog --- .../changelog/fix-calc-totals-before-running-validation | 4 ++++ plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php | 6 ++++++ 2 files changed, 10 insertions(+) create mode 100644 plugins/woocommerce/changelog/fix-calc-totals-before-running-validation diff --git a/plugins/woocommerce/changelog/fix-calc-totals-before-running-validation b/plugins/woocommerce/changelog/fix-calc-totals-before-running-validation new file mode 100644 index 00000000000..9d91b03c455 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-calc-totals-before-running-validation @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Ensure the Store API recalculates cart totals prior to running validation hooks. diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php b/plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php index 90add13712c..4e1e472bed7 100644 --- a/plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php +++ b/plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php @@ -188,6 +188,12 @@ class Checkout extends AbstractCartRoute { * @return \WP_REST_Response */ protected function get_route_post_response( \WP_REST_Request $request ) { + /** + * Before triggering validation, ensure totals are current and in turn, things such as shipping costs are present. + * This is so plugins that validate other cart data (e.g. conditional shipping and payments) can access this data. + */ + $this->cart_controller->calculate_totals(); + /** * Validate items etc are allowed in the order before the order is processed. This will fix violations and tell * the customer.