Commit 0081a328d04 for woocommerce
commit 0081a328d048524eb026fe15babb932ca582632f
Author: Michal Iwanow <4765119+mcliwanow@users.noreply.github.com>
Date: Tue Nov 24 13:47:06 2026 +0100
Read missing line totals as zero when building cart shipping packages (#71317)
* Read missing line totals as zero when building cart shipping packages
The tax lookup at the start of the totals calculation that follows
add_to_cart() can build the cart shipping packages before the new line
has line_total, so wp_list_pluck() in WC_Cart::get_shipping_packages()
logged "Undefined array key line_total". Sum the line totals with
array_column(), which gives the same contents_cost without the warning.
* Say in the testdox that the flat rate case is a control
diff --git a/plugins/woocommerce/changelog/fix-wooplug-8248-shipping-packages-before-totals b/plugins/woocommerce/changelog/fix-wooplug-8248-shipping-packages-before-totals
new file mode 100644
index 00000000000..c8d08494fe4
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-wooplug-8248-shipping-packages-before-totals
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix a PHP warning logged when a product is added to the cart with local pickup chosen and taxes enabled.
diff --git a/plugins/woocommerce/includes/class-wc-cart.php b/plugins/woocommerce/includes/class-wc-cart.php
index c86e0856e20..da8f26d45fa 100644
--- a/plugins/woocommerce/includes/class-wc-cart.php
+++ b/plugins/woocommerce/includes/class-wc-cart.php
@@ -1777,7 +1777,7 @@ class WC_Cart extends WC_Legacy_Cart {
array(
array(
'contents' => $this->get_items_needing_shipping(),
- 'contents_cost' => array_sum( wp_list_pluck( $this->get_items_needing_shipping(), 'line_total' ) ),
+ 'contents_cost' => array_sum( array_column( $this->get_items_needing_shipping(), 'line_total' ) ),
'applied_coupons' => $this->get_applied_coupons(),
'user' => array(
'ID' => get_current_user_id(),
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
index d8a3a3526fe..27fc7e01a4f 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
@@ -3305,4 +3305,19 @@ class WC_Cart_Test extends \WC_Unit_Test_Case {
$this->assertStringContainsString( 'Usage limit for coupon', implode( ' ', $this->get_guest_coupon_check_errors( 'cart_email_change', 'cart-before@example.com' ) ), 'The use is found through the email recorded with it' );
}
+
+ /**
+ * @testdox get_shipping_packages() sums the line totals that exist and reads no missing line_total.
+ */
+ public function test_get_shipping_packages_contents_cost_skips_lines_without_line_total(): void {
+ $cart = WC()->cart;
+ $first_key = $cart->add_to_cart( WC_Helper_Product::create_simple_product()->get_id() );
+ $new_key = $cart->add_to_cart( WC_Helper_Product::create_simple_product()->get_id(), 2 );
+ // A line added during the totals calculation has no line_total until the calculation sets it.
+ unset( $cart->cart_contents[ $new_key ]['line_total'] );
+
+ $packages = $cart->get_shipping_packages();
+
+ $this->assertEquals( $cart->cart_contents[ $first_key ]['line_total'], $packages[0]['contents_cost'] );
+ }
}
diff --git a/plugins/woocommerce/tests/php/src/Utilities/ShippingUtilTest.php b/plugins/woocommerce/tests/php/src/Utilities/ShippingUtilTest.php
index f84ed24c232..42948bcd866 100644
--- a/plugins/woocommerce/tests/php/src/Utilities/ShippingUtilTest.php
+++ b/plugins/woocommerce/tests/php/src/Utilities/ShippingUtilTest.php
@@ -445,6 +445,51 @@ class ShippingUtilTest extends WC_Unit_Test_Case {
);
}
+ /**
+ * @testdox Adding a new cart line with taxes on raises no line_total warning and keeps the chosen method in the tax lookup. Flat rate is a control: only a pickup choice builds the packages there.
+ *
+ * @testWith [ "local_pickup:2", "local_pickup" ]
+ * [ "flat_rate:1", "flat_rate" ]
+ *
+ * @param string $rate_id Chosen and listed rate id.
+ * @param string $method_id Method id expected from wc_get_chosen_shipping_method_ids().
+ */
+ public function test_adding_new_cart_line_with_taxes_on_reads_no_missing_line_total( string $rate_id, string $method_id ): void {
+ update_option( 'woocommerce_calc_taxes', 'yes' );
+ WC()->session->set( 'previous_shipping_methods', array( 0 => array( $rate_id ) ) );
+ WC()->session->set( 'chosen_shipping_methods', array( 0 => $rate_id ) );
+ $seen = array();
+ add_filter(
+ 'woocommerce_cart_totals_get_item_tax_rates',
+ function ( $rates ) use ( &$seen ) {
+ $seen[] = wc_get_chosen_shipping_method_ids();
+ return $rates;
+ }
+ );
+ $warnings = array();
+ // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- Records warnings PHPUnit would otherwise turn into exceptions that add_to_cart() catches.
+ set_error_handler(
+ function ( $errno, $errstr ) use ( &$warnings ) {
+ unset( $errno );
+ $warnings[] = $errstr;
+ return true;
+ }
+ );
+
+ try {
+ $item_key = WC()->cart->add_to_cart( \WC_Helper_Product::create_simple_product()->get_id() );
+ } finally {
+ restore_error_handler();
+ }
+
+ $this->assertNotFalse( $item_key );
+ foreach ( $warnings as $warning ) {
+ $this->assertStringNotContainsString( 'line_total', $warning );
+ }
+ $this->assertNotEmpty( $seen );
+ $this->assertSame( array( $method_id ), $seen[0], 'The chosen method must be kept in the tax lookup before the new line has totals.' );
+ }
+
/**
* @testdox filter_chosen_shipping_methods returns an empty array for a value that is not an array.
*/