【发布时间】:2017-06-03 11:13:12
【问题描述】:
我正在为 WooCommerce 中的产品构建自定义登录页面,我想获取产品价格等信息,以便在登录页面上显示它们。
每个着陆页都有一些自定义字段,允许 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>
更新
我使用 wc_get_product( $courseID ); 或 get_product( $courseID ); 收到以下错误:
Fatal error: Call to a member function get_regular_price() on a non-object in ...
【问题讨论】:
标签: php wordpress woocommerce advanced-custom-fields product