Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用wc_get_product()和产品ID的PHP变量_Php_Wordpress_Woocommerce_Advanced Custom Fields_Product - Fatal编程技术网

使用wc_get_product()和产品ID的PHP变量

使用wc_get_product()和产品ID的PHP变量,php,wordpress,woocommerce,advanced-custom-fields,product,Php,Wordpress,Woocommerce,Advanced Custom Fields,Product,我正在为WooCommerce中的产品建立自定义登录页,我希望获得产品价格以及其他信息,以便在登录页上显示它们 每个登录页都有一些自定义字段,允许WP管理员为登录页以及产品ID添加内容,然后使用这些内容生成产品价格、结帐URL等 我无法获取wc_get_product()使用自定义字段或基于该字段构建的变量。它只在我使用一个直接ID时才起作用。我想关于变量在PHP中的工作方式,我还不太了解。这是我的密码 <?php //Gets the course ID from the custo

我正在为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>

更新与您最近的评论相关的内容探索的两种方法:

1) 相反,您应该尝试使用来获取产品对象(避免错误):

2) 或者,如果这不起作用,您应该尝试使用函数以以下方式获取产品价格(或任何产品元数据):

<?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>
<?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>
现在应该可以了。

您可以尝试以下方法:

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

我在浏览了@LoicTheAztec在其回复中提供的可能的解决方案之后找到了答案。所有这些都不起作用,所以我认为发生了其他事情

我使用高级自定义字段在后端添加自定义字段,并使用ACF的
the_field()
创建变量。这是对该函数的不正确使用,因为它是用来显示字段的(它基本上使用php的echo)。要使用这些自定义字段,您需要使用ACf的
get_field()
,它将

有一次我将$courseID设置为这个

$courseID = get_field('course_id'); 

一切顺利。我的代码有效,所有@LoicTheAztec的代码方法也有效

我应该提到的是,我已经在没有
'
的情况下尝试过这个方法,它会对非对象的调用产生错误<代码>致命错误:对…中的非对象调用成员函数get_regular_price()感谢您的帮助!如果没有它,我不可能排除其他选项。非常感谢!使用较旧的
get_product()函数在删除引号时仍然不起作用。请看我对@LoicTheAztec belowHo的回复是!!!这是如此明显,以至于我这次没有看到它…我已经用ACF get_field()/the_field()给出了答案…这是一个非常常见的错误。
$courseID = get_field('course_id');