I have a function that outputs text after the main_content
action hook in WordPress, but Woocommerce is telling me that I have an CRITICAL Uncaught Error: Call to a member function get_id() on null in .../function.php
.
I've tried passing in the global $product
, and checking if it is a product before running the function. The function seems to work fine, but I'm just trying to get rid of the fatal errors.
Is there something obvious I'm missing?
Here's my function:
add_action('woocommerce_after_main_content', 'display_prewired_notes');
function display_prewired_notes() {
global $product; //Tried global variable
$product_id = $product->get_id(); //getting ID
$product_name = $product->get_name(); //get name
if (is_product() && has_term('prewired-pickguard', 'product_cat', $product_id)) { ?>
//My HTML here
<?php
}
if (is_product() && $product_id == 6599) { ?>
//More HTML for this specific product
<?php
}
};
Edit:
I've tried a few things based on @Martin 's suggestions, and I still can't get this to work.
I've tried:
1:
<?php
global $product
function display_prewired_notes($product) { // Pass it in
$product_id = $product->get_id(); //getting ID
$product_name = $product->get_name(); //get name
And I get atal error: Uncaught Error: Call to a member function get_id() on string
2:
Removing the global $product
entirely, I get:
Uncaught Error: Call to a member function get_id() on null
3:
Removing the global $product
and keeping the $product
as a parameter: Fatal error: Uncaught Error: Call to a member function get_id() on string