Fatal error: Call to a member function get_id() on boolean
Fatal error: Call to a member function get_id() on boolean
在我的 wordpress 网站中出现以下错误
Fatal error: Call to a member function get_id() on boolean in /home/customer/www/ae.testing.com/public_html/wp-content/themes/movedo-child/functions.php on line 33
之前工作正常。但现在我没有得到什么确切的问题。如果有人有想法,请告诉我
function.php如下:
<?php
function movedo_child_theme_setup() {
}
add_action( 'after_setup_theme', 'movedo_child_theme_setup' );
function display_price_in_variation_option_name( $term ) {
echo $product = wc_get_product();
$id = $product->get_id();
if ( empty( $term ) || empty( $id ) ) {
return $term;
}
if ( $product->is_type( 'variable' ) ) {
$product_variations = $product->get_available_variations();
} else {
return $term;
}
foreach($product_variations as $variation){
if(count($variation['attributes']) > 1){
return $term;
}
foreach($variation['attributes'] as $key => $slug){
if("attribute_" == mb_substr( $key, 0, 10 )){
$taxonomy = mb_substr( $key, 10 ) ;
$attribute = get_term_by('slug', $slug, $taxonomy);
if($attribute->name == $term){
$term .= " (" . wp_kses( wc_price($variation['display_price']), array()) . ")";
}
}
}
}
return $term;
}
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
在线“$id = $product->get_id();”我遇到了这个错误。
这个问题看起来很简单,你正在连接到 'after_setup_theme',它会为每个 wordpress 请求触发。在这种情况下,您假设您在主查询上下文中始终有一个产品,但这并不总是正确的。
简单地关闭错误 wrap $id = $product->get_id();变成这样的 if:
if(!empty($product)){
$id = $product->get_id();
}
...
但要真正做到防弹,您应该问问自己什么时候真正想研究 wc_get_product 以获得当前产品。我假设您可能只希望在产品详细信息页面上看到它。
既然你挂钩到'after_setup_theme' conditional tags 就可以用了。
更改您的代码,以便在它未在正确的页面中执行时立即退出,执行如下操作:
function display_price_in_variation_option_name( $term ) {
if (!is_product()){
return;
}
echo $product = wc_get_product();
$id = $product->get_id();
...
Wc_get_product 以某种方式返回 false。
尝试
global $product; //
在我的 wordpress 网站中出现以下错误
Fatal error: Call to a member function get_id() on boolean in /home/customer/www/ae.testing.com/public_html/wp-content/themes/movedo-child/functions.php on line 33
之前工作正常。但现在我没有得到什么确切的问题。如果有人有想法,请告诉我
function.php如下:
<?php
function movedo_child_theme_setup() {
}
add_action( 'after_setup_theme', 'movedo_child_theme_setup' );
function display_price_in_variation_option_name( $term ) {
echo $product = wc_get_product();
$id = $product->get_id();
if ( empty( $term ) || empty( $id ) ) {
return $term;
}
if ( $product->is_type( 'variable' ) ) {
$product_variations = $product->get_available_variations();
} else {
return $term;
}
foreach($product_variations as $variation){
if(count($variation['attributes']) > 1){
return $term;
}
foreach($variation['attributes'] as $key => $slug){
if("attribute_" == mb_substr( $key, 0, 10 )){
$taxonomy = mb_substr( $key, 10 ) ;
$attribute = get_term_by('slug', $slug, $taxonomy);
if($attribute->name == $term){
$term .= " (" . wp_kses( wc_price($variation['display_price']), array()) . ")";
}
}
}
}
return $term;
}
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
在线“$id = $product->get_id();”我遇到了这个错误。
这个问题看起来很简单,你正在连接到 'after_setup_theme',它会为每个 wordpress 请求触发。在这种情况下,您假设您在主查询上下文中始终有一个产品,但这并不总是正确的。
简单地关闭错误 wrap $id = $product->get_id();变成这样的 if:
if(!empty($product)){
$id = $product->get_id();
}
...
但要真正做到防弹,您应该问问自己什么时候真正想研究 wc_get_product 以获得当前产品。我假设您可能只希望在产品详细信息页面上看到它。 既然你挂钩到'after_setup_theme' conditional tags 就可以用了。
更改您的代码,以便在它未在正确的页面中执行时立即退出,执行如下操作:
function display_price_in_variation_option_name( $term ) {
if (!is_product()){
return;
}
echo $product = wc_get_product();
$id = $product->get_id();
...
Wc_get_product 以某种方式返回 false。 尝试
global $product; //