Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce issue: Uncaught Error: Call to a member function get_type() on bool? [duplicate]

I am currently trying to implement this solution in WooCommerce provided by @LoicTheAztec.

Override External Product URL to "Add to Cart" product button

However, when I entered it into functions.php, this is the error message I get.

Error message that appears

Your PHP code changes were rolled back due to an error on line 102 of file wp-content/themes/astra-child/functions.php. Please fix and try saving again.

Uncaught Error: Call to a member function get_type() on bool in wp-content/themes/astra-child/functions.php:102
Stack trace:
#0 wp-includes/class-wp-hook.php(289): redirect_simple_product_with_external_url('https://lms.sur...')
#1 wp-includes/plugin.php(206): WP_Hook->apply_filters('https://lms.sur...', Array)
#2 wp-content/plugins/woocommerce/includes/class-wc-frontend-scripts.php(559): apply_filters('woocommerce_add...', 'https://lms.sur...', NULL)
#3 wp-content/plugins/woocommerce/includes/class-wc-frontend-scripts.php(444): WC_Frontend_Scripts::get_script_data('wc-add-to-cart')
#4 wp-content/plugins/woocommerce/includes/class-wc-frontend-scripts.php(613): WC_Frontend_Scripts::localize_script('wc-add-to-cart')
#5 wp-includes/class-wp-hook.php(287): WC_Frontend_Scrip

This is the line 102 in question:

if ( 'simple' == $product->get_type() && ! empty( $external_url ) ){

How can I fix this?


Edit: Fixed in the linked answer (updated).

like image 930
xojijog684 Avatar asked Sep 16 '25 14:09

xojijog684


1 Answers

This means your $product variable is empty. And get_type() expects the product object. Check in your code why the $product variable does not contain the product object.

like image 137
Terminator-Barbapapa Avatar answered Sep 19 '25 04:09

Terminator-Barbapapa