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