Handle invalid/missing variation data in variable products (avoid fatal error)
-
When displaying checkout fees info on a variable product page, the code in class-alg-wc-checkout-fees-info.php loops over $the_product->get_available_variations() and uses $product_variation[‘variation_id’] and wc_get_product( $product_variation[‘variation_id’] ) without validating the result.This can cause:
- PHP 8+ – “Undefined array key ‘variation_id’” if an entry in the variations array doesn’t have that key.
- Fatal error – “Call to a member function get_price() on false” when wc_get_product() returns false (e.g. deleted variation, invalid ID) and that value is passed to wc_get_price_including_tax() / wc_get_price_excluding_tax().
Recommendation: Add defensive checks before using each variation, so invalid or missing data is skipped instead of breaking the page:
- Ensure variation_id exists and is not empty (e.g. isset( $product_variation[‘variation_id’] ) or ! empty( $product_variation[‘variation_id’] )).
- After $variation_product = wc_get_product( $product_variation[‘variation_id’] );, check that $variation_product is a valid product object (e.g. if ( ! $variation_product ) { continue; }) before calling any WooCommerce price functions.
The same pattern appears when building $products_array for variable products (around lines 109–125). Applying these checks there would make the plugin robust against missing or invalid variation data and compatible with PHP 8+.Thank you.
You must be logged in to reply to this topic.