WooCommerce模板迁移问题:Stock代码移至Price后无法获取属性值
Hey Lucian, great question—this is a super common snag when moving code between WooCommerce templates, and it all comes down to context and dependency differences between the two files. Let’s break down what’s happening and how to fix it:
1. The Core Issue: Template Context Matters
When your code lived in stock.php, WooCommerce’s template system had already set up all the necessary data and variables before loading that file. Specifically:
- The
$productobject was fully initialized and ready to use. - Variables like
$availability(which holds the stock status class and text) were pre-defined by the logic that runs right beforestock.phploads.
But price.php loads at a slightly different point in the template hierarchy, and those prepped variables/setup steps don’t exist there. So when you copy-paste the code directly, it’s trying to access data that hasn’t been created yet.
2. Fixes to Get Your Code Working in Price.php
a. Ensure the $product Object is Valid
First, make sure you have a working $product object in price.php. Sometimes in certain template load scenarios, the global $product might not be set correctly. Add this at the top of your code snippet:
// Grab the global product object, or fetch it manually if needed global $product; if ( ! $product ) { $product = wc_get_product( get_the_ID() ); }
b. Manually Fetch Availability Data
Instead of relying on the $availability variable that stock.php provides, fetch it directly from the $product object. Replace any references to the existing $availability with this:
// Get availability data directly from the product $availability = $product->get_availability(); // Now you can safely use these values $availability_class = esc_attr( $availability['class'] ); $availability_text = esc_html( $availability['availability'] );
This is exactly what WooCommerce does under the hood in stock.php—you’re just replicating that logic in price.php instead of relying on the template’s pre-set variables.
c. Double-Check Template File Names & Paths
WooCommerce is case-sensitive on most servers, so make sure your files are named stock.php and price.php (all lowercase) inside your-theme/woocommerce/single-product/. If you’re using uppercase .PHP extensions, some servers might not load your custom template correctly, falling back to the default WooCommerce template instead.
d. Verify Hook Priority (If Using Hooks)
If your code is attached to a WooCommerce hook (like woocommerce_single_product_summary), make sure the priority is set high enough that the $product object exists when your code runs. For example, using a priority of 20 instead of 10 ensures it runs after the product data is initialized:
add_action( 'woocommerce_single_product_summary', 'your_custom_function', 20 ); function your_custom_function() { // Your code here }
3. Quick Test to Confirm
After making these changes, load a single product page and check if the esc_attr values and availability info show up. If not, enable WP_DEBUG in your wp-config.php to see if there are any PHP errors (like undefined variables) that can point you to the exact issue.
内容的提问来源于stack exchange,提问作者Lucian Tudorache

