求助:如何修复get_product已弃用报错?自定义主题更新问题
Hey there, let's work through this WooCommerce deprecation warning step by step. The issue is straightforward: WooCommerce 3.0+ phased out the old get_product() function in favor of wc_get_product(), and your custom theme still has lingering references to the outdated method. Here's how to track down and fix every instance:
Step 1: Locate all uses of get_product in your theme
You mentioned modifying price.php and abstract-wc-product.php, but there are almost certainly other files in your theme calling the deprecated function. Here's how to find them all:
- Open your theme's directory in a code editor (like VS Code, Sublime Text, or PhpStorm)
- Use the global search tool (Ctrl+Shift+F in VS Code) to search for
get_product(across every file in your theme folder - Pay extra attention to these common locations:
- The theme's root
functions.phpfile - WooCommerce template overrides (usually stored in a
woocommercesubfolder of your theme) - Custom product templates (like
single-product.php,archive-product.php, or theme-specific product blocks) - Include/module folders (often named
incormodules)
- The theme's root
Step 2: Replace get_product with wc_get_product everywhere
For every match you find, swap the old function call with the new one. Examples of the replacement:
- Before:
$product = get_product( $product_id ); - After:
$product = wc_get_product( $product_id );
Don't miss subtle variations either, like:
get_product( get_the_ID() )→wc_get_product( get_the_ID() )get_product( $post->ID )→wc_get_product( $post->ID )
Step 3: Pinpoint remaining warnings with debug mode
If the warning still pops up after your first pass, enable WordPress debug mode to get the exact file and line number triggering the issue:
- Open your site's
wp-config.phpfile (in the root WordPress directory) - Find the line
define('WP_DEBUG', false);and replace it with:define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); define('WP_DEBUG_DISPLAY', true); - Refresh the page with the warning. You'll now see the full path to the file and line where
get_productis still being used—this will help you track down any missed instances.
Step 4: Clear all caches
After making your changes, clear every layer of cache to ensure the updates take effect:
- Your WordPress caching plugin's cache (if you use tools like WP Rocket, W3 Total Cache, or LiteSpeed Cache)
- Your browser's cache (use Ctrl+Shift+R for a hard refresh)
- Any server-level cache (check your hosting control panel if you're unsure)
Bonus Tips
- Backup first: Always make a copy of your theme files before modifying them, so you can revert quickly if something breaks.
- Loop in your theme developer: Since this is a custom theme built by your boss, let them know about the deprecated function. Updating the theme's core code will prevent this issue from coming back if the theme gets updated later.
内容的提问来源于stack exchange,提问作者Kristoffer Bålerud Helgesen

