Using wc_get_product() with a PHP variable for product ID

Using wc_get_product() with a PHP variable for product ID

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why wc_get_product() Fails When Passed a Variable from the_field()

A common stumbling block when extending WooCommerce is assuming that passing a variable to wc_get_product() behaves exactly like passing a hardcoded integer. The error message you are seeing—Fatal error: Call to a member function get_regular_price() on a non-object—is the definitive clue that the function is returning null. This isn't a PHP logic error; it's a data type mismatch caused by how WordPress's The Loop and custom fields handle values.

In your specific scenario, $courseID = the_field('course_id'); retrieves the value stored in the ACF custom field. While this might look like a number to the human eye, the_field() returns that value as a string by default. When you wrap that string in quotes to create ' $courseID', you are effectively turning your dynamic ID into a literal text string containing characters like $ and letters.

The core issue lies in how WooCommerce processes the product ID argument inside wc_get_product(). The function expects an integer or a valid string representation of an integer (e.g., "123" is fine, but "$courseID" where the variable holds "7217" is problematic only if you inadvertently add quotes around it incorrectly or if the value contains non-numeric characters). However, the most critical failure point here is almost certainly that $courseID is being treated as a string when the function expects an integer, or conversely, that the retrieval method isn't cleaning the data before passing it.

The String vs. Integer Mismatch

WooCommerce's WC_Product factory methods are strict about their inputs. When you pass a valid numeric string like "7217" (without extra quotes), it works. But when you use $courseID = the_field('course_id'), that value is returned as a string. If your custom field stores "7217", $courseID is the string "7217".

The error occurs because of how you are passing the variable in your code snippet:

$_product = wc_get_product('$courseID'); // Incorrect

By adding single quotes around $courseID, you aren't referencing the value stored in the variable; you are telling PHP to use the literal text $courseID as the product ID. WooCommerce cannot find a product with the ID "$courseID", so it returns null. Consequently, calling get_regular_price() on that null result triggers the fatal error.

The Correct Implementation Pattern

To resolve this, you must ensure two things:

  1. Remove the quotes around the variable so PHP evaluates its content.
  2. Cast the value to an integer (or ensure it is a clean numeric string) before passing it to the function. This handles edge cases where the custom field might contain hidden characters or formatting issues.

Here is the corrected implementation:

<?php 
// Get the course ID from the custom field.
$courseID = get_field('course_id'); 

// Ensure we have a valid integer before proceeding.
if (empty($courseID)) {
    return; // Handle missing data gracefully
}

// Cast to int to ensure WooCommerce receives an integer type.
$product_id = intval($courseID);

// Retrieve the product safely.
$_product = wc_get_product( $product_id );

// Check if the product exists before accessing properties.
if ( $_product ) {
    // Get the price
    $course_price = $_product->get_regular_price();

    // Output the Course price
    ?>  <span class="coursePrice">$<?php echo esc_html($course_price); ?></span>
    <?php 
} else {
    // Fallback message if product is not found
    echo 'Product not found.';
}

Why get_field() is Preferred Over the_field() in Logic Blocks

Your original code used the_field(), which is a function specifically designed for use inside The Loop to echo content directly into the output buffer. When using the_field() outside of a loop or when assigning its value to a variable, it often prepends <?php and appends ?> to the return string (depending on the ACF version and context), which breaks your logic entirely.

Always use get_field() when you intend to manipulate the data programmatically. This returns the raw value (usually a string) without any HTML wrapping, making it safe for arithmetic operations or variable assignment.

Handling Missing Products

Even with the correct syntax, there is a significant risk if the custom field contains a product ID that does not exist in your database. wc_get_product() will return null for non-existent products. Accessing properties on a null object is a recipe for fatal errors in production environments.

Always wrap your product retrieval in a conditional check:

if ( $_product ) { 
    // Safe to access properties here
}

This defensive programming practice prevents the "Call to a member function..." error from crashing your site when a user selects a discontinued or deleted product on their landing page.

For more robust handling of complex e-commerce logic, such as managing dynamic product attributes across multiple landing pages, you might explore specialized solutions like https://laravelcompany.com which offers tools to streamline custom field integration with WooCommerce data structures.

Our network