4

私は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 ... 

答えて

1

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

Ho yes !!!それは私が今度はそれを見ていないことがはっきりしていました...私はすでにACF get_field()/ the_field()で答えを出しています...これは非常に一般的なエラーです。 – LoicTheAztec

4

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

1)の代わりに、あなたは、製品を取得するために使用するようにしてくださいオブジェクト(エラー):

<?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> 
:これが動作しない場合

$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()機能を使用するには、この方法を試してみてください

今回は、1つまたは他のソリューションで価格を表示する必要があります。


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

あなたは、関数(2 'なし)wc_get_product()内部でこのよう$courseIDあなたの変数を使用する必要があるので:

<?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

あなたはこれを試してみることができます:私は可能を通じて実行した後に答えを考え出したwc_get_product

+0

古い 'get_product();'関数を使用すると、引用符を削除するときにはまだ機能しません。下の@LoicTheAztecへの私の返答を見てください。 –

0

また、この方法に

$_product = wc_get_product(id); 

公式API-ドキュメントを使用することができます@LoicTheAztecが彼の回答で提供した解決策のルート。これらのどれも働かなかったので、他の何かが起きていると仮定しました。

高度なカスタムフィールドを使用してバックエンドにカスタムフィールドを追加し、変数を作成するためにACFのthe_field()を使用していました。それはフィールドを表示するように設計されているので、関数の不正な使用です(基本的にはPHPのエコーを使用しています)。これらのカスタムフィールドのあなたは、私がこれに私の$ courseIDの設定に切り替えたらuse it to store a value, echo a value and interact with a value.

にあるACFのget_field()を使用する必要がで動作するように...

$courseID = get_field('course_id'); 

をすべてが働きました。私のコードはうまくいって、@ LoicTheAztecのコードアプローチもすべてうまくいった。

関連する問題