3

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).

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
xojijog684
  • 190
  • 2
  • 8

1 Answers1

3

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.

Terminator-Barbapapa
  • 3,063
  • 2
  • 5
  • 16