Avoid fatal in `wc_get_chosen_shipping_method_ids` (#50774)

This ensures the WC() session object is initialized before attempting to
get chosen shipping methods. If it's not initialized,
`wc_get_chosen_shipping_method_ids` will return an empty array.

Fixes #50283
This commit is contained in:
Corey McKrill 2024-08-21 10:28:54 -07:00 committed by GitHub
parent 0322426dce
commit b430971093
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 1 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Ensure session object is initialized before attempting to get chosen shipping methods

View File

@ -413,7 +413,12 @@ function wc_cart_round_discount( $value, $precision ) {
*/ */
function wc_get_chosen_shipping_method_ids() { function wc_get_chosen_shipping_method_ids() {
$method_ids = array(); $method_ids = array();
$chosen_methods = WC()->session->get( 'chosen_shipping_methods', array() ); $chosen_methods = array();
if ( is_callable( array( WC()->session, 'get' ) ) ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods', array() );
}
foreach ( $chosen_methods as $chosen_method ) { foreach ( $chosen_methods as $chosen_method ) {
if ( ! is_string( $chosen_method ) ) { if ( ! is_string( $chosen_method ) ) {
continue; continue;
@ -421,6 +426,7 @@ function wc_get_chosen_shipping_method_ids() {
$chosen_method = explode( ':', $chosen_method ); $chosen_method = explode( ':', $chosen_method );
$method_ids[] = current( $chosen_method ); $method_ids[] = current( $chosen_method );
} }
return $method_ids; return $method_ids;
} }