2017-01-18 43 views
4

我正在爲WooCommerce中的產品構建自定義着陸頁,並且希望在產品價格中包含其他內容以便在着陸頁上顯示它們。將wc_get_product()與產品ID的PHP變量一起使用

每個着陸頁有一些自定義字段允許的WP管理員在添加內容,目標網頁以及產品ID,將被用來生成產品的價格,結賬URL等。

我無法讓wc_get_product();與我的自定義字段或由其構建的變量一起工作。它只在我使用直接ID時才起作用。我認爲我不瞭解PHP中的變量是如何工作的。這是我的代碼。

<?php 

//Gets the course ID from the custom field entered by user 
$courseID = the_field('course_id'); 

// This line is where the problem is... 
$_product = wc_get_product('$courseID'); 

// If I replace the line above with this line 
// $_product = wc_get_product('7217'); 
// everything works great, but that does not let 
// each landing page function based on the custom fields where the user determines 
// the product ID they are selling on that landing page. 


// Get's the price of the product 
$course_price = $_product->get_regular_price(); 

// Output the Course price 
?> <span class="coursePrice">$<?php echo $course_price;?></span> 

Update

我得到使用wc_get_product($courseID);get_product($courseID);以下錯誤:

Fatal error: Call to a member function get_regular_price() on a non-object in ... 

回答

1

我經歷可能運行後想出答案@LoicTheAztec在回覆中提供的解決方案路線。這些都沒有工作,所以我認爲還有其他事情。

我使用高級自定義字段在後端添加自定義字段,並且我使用ACF的the_field()來創建我的變量。這是該函數的不正確用法,因爲它被設計爲顯示字段,(它基本上使用php的回聲)。一起工作,這些自定義字段的,你需要使用ACF的get_field()這是use it to store a value, echo a value and interact with a value.

一旦我切換到我的$ courseID設置這個..

$courseID = get_field('course_id'); 

一切工作。我的代碼工作,所有@ LoicTheAztec的代碼方法也工作。

+0

何哉!這是如此明顯,以至於我這次沒有看到它......我已經用ACF get_field()/ the_field()做出了答案......這是一個非常常見的錯誤。 – LoicTheAztec

4

Update related to your recent comment. The 2 ways to explore:

1)替代,你應該嘗試使用,以獲得產品對象(避免錯誤):

$courseID = the_field('course_id'); 

// Optionally try this (uncommenting) 
// $courseID = (int)$courseID; 

// Get an instance of the product object 
$_product = new WC_Product($courseID); 

2)另外,如果這不工作,你應該嘗試使用get_post_meta()函數來獲取產品價格(或任何產品的元數據)這樣:

<?php 
//Gets the course ID from the custom field entered by user 
$courseID = the_field('course_id'); 

// Get the product price (from this course ID): 
$course_price = get_post_meta($courseID, '_regular_price', true); 

// Output the Course price 
?> <span class="coursePrice">$<?php echo $course_price;?></span> 

這次你應該用一個或其他解決方案顯示價格。


Update: May be Also you need to convert $courseID to an integer variable.

因爲你需要使用你的變量$courseIDwc_get_product()(不含2 ')函數是這樣的:

<?php 

//Gets the course ID from the custom field entered by user 
$courseID = the_field('course_id'); 

// Optionally try this (uncommenting) 
// $courseID = (int)$courseID; 

// Here 
$_product = wc_get_product($courseID); 

$course_price = $_product->get_regular_price(); 

// Output the Course price 
?> <span class="coursePrice">$<?php echo $course_price;?></span> 

這應該現在的工作。

+0

我應該提到,我已經嘗試過這種方式,但沒有使用'''',它會在非對象上調用時產生錯誤。 '致命錯誤:調用一個非對象的成員函數get_regular_price()...' –

+0

感謝您的幫助!如果沒有它,我將無法消除其他選擇。非常感激! –

1

你可以試試這個:

$courseID = the_field('course_id'); 
$product = get_product($courseID); 
+0

刪除引號時,使用較舊的'get_product();'函數仍然不起作用。看到我對@LoicTheAztec的迴應如下 –

0

您也可以使用此方法

$_product = wc_get_product(id); 

官方API的文檔:wc_get_product

相關問題