私はWooCommerceの商品のカスタムランディングページを作成しています。ランディングページに商品を表示するために商品価格を取得したいと考えています。商品IDにPHP変数wc_get_product()を使用
各ランディングページには、WP管理者が商品ページIDと商品IDをコンテンツに追加できるようにするカスタムフィールドがあります。
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 ...
Ho yes !!!それは私が今度はそれを見ていないことがはっきりしていました...私はすでにACF get_field()/ the_field()で答えを出しています...これは非常に一般的なエラーです。 – LoicTheAztec