首頁  >  問答  >  主體

不確定為什麼未定義的變數通知嘗試取得產品「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 天前363

全部回覆(1)我來回復

  • P粉668113768

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

    供任何人觀看。如果使用$product和$post,那麼你需要使用global $product;和全域 $post 來存取它們。

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

    回覆
    0
  • 取消回覆