首页  >  问答  >  正文

不确定为什么未定义的变量通知尝试获取产品“id”;将 ACF 字段添加到自定义列时?

我已在管理仪表板中的 WooCommerce 产品页面中添加了一个自定义列,但是当尝试通过为帖子类型 -> 产品添加 2 个自定义 ACF 字段设置来填充列时,我收到了有关其中每个产品上列出的调试的错误通知自定义列。

注意:未定义的变量:产品位于 /./././wp-content/themes/bpa/functions.php 第 923 行

注意:尝试获取非对象的产品“id” /./././wp-content/themes/bpa/functions.php 第 923 行

有人可以帮忙解释一下我做错了什么以获得未定义的变量吗?

第 923 行是:

$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

是因为我使用了旧的厕所方法还是什么?

完整功能:

// Populate column
function woo_product_rmreference_column_data( $column ) {
    global $post;

    if ( $column == 'rm_reference' ) {
            
        // 
            $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
        
            // Get ACF Fields
            $reference = get_field( 'property_reference', $product_id );
            $address = get_field( 'location', $product_id );

            // Output
            echo ($reference . $address) ? '<div>'.$reference.' - '.$address.'</div>' : '<div>Not found!</div>';
    }
}
add_action( 'manage_product_posts_custom_column' , 'woo_product_rmreference_column_data', 10, 2 );

P粉450744515P粉450744515215 天前362

全部回复(1)我来回复

  • P粉668113768

    P粉6681137682024-02-22 10:56:00

    供任何人观看。如果使用$product和$post,那么你需要使用global $product;和全局 $post 来访问它们。

    global $product;
    
    $product_id = $product->get_id();

    回复
    0
  • 取消回复