不确定为什么未定义的变量通知尝试获取产品'id”;将 ACF 字段添加到自定义列时?
P粉450744515
P粉450744515 2024-02-21 13:30:15
0
1
360

我已在管理仪表板中的 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粉450744515
P粉450744515

全部回复(1)
P粉668113768

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

global $product;

$product_id = $product->get_id();
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!