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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41730193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:27:12  来源:igfitidea点击:

Using wc_get_product() with a PHP variable for product ID

phpwordpresswoocommerceadvanced-custom-fieldsproduct

提问by Andrew-ThinkUp

I'm building out custom landing pages for products in WooCommerce and I'd like to get the product price amongst other things in order to display them on the landing page.

我正在为 WooCommerce 中的产品构建自定义登陆页面,我想获得产品价格等信息,以便在登陆页面上显示它们。

Each landing page has some custom fields which allow the WP Admin to add in content, for the landing page as well as the product ID, which will then be used to generate the product price, checkout URL etc..

每个登陆页面都有一些自定义字段,允许 WP 管理员为登陆页面以及产品 ID 添加内容,然后将用于生成产品价格、结帐 URL 等。

I can't get the wc_get_product();to work with my custom field or a variable built off that. It only works when I use a straight ID. I think there's something I'm not understanding about how variables work within PHP. Here's my code.

我无法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

更新

I get the following error using wc_get_product( $courseID );or get_product( $courseID );:

使用wc_get_product( $courseID );or 时出现以下错误get_product( $courseID );

Fatal error: Call to a member function get_regular_price() on a non-object in ... 

采纳答案by Andrew-ThinkUp

I figured out the answer after running through the possible solution routes that @LoicTheAztec supplied in his response. None of these worked and so I assumed something else was up.

在浏览了@LoicTheAztec 在他的回复中提供的可能的解决方案路线后,我找到了答案。这些都没有奏效,所以我认为还有其他事情发生。

I use Advanced Custom Fields to add custom fields in the back end and I was using ACF's the_field()in order to create my variable. That is incorrect usage of that function as it's designed to display the field, (it's basically using php's echo). To work with these custom field's you need to use ACf's get_field()which is to use it to store a value, echo a value and interact with a value.

我使用高级自定义字段在后端添加自定义字段,我使用 ACFthe_field()来创建我的变量。这是该函数的错误用法,因为它旨在显示该字段(它基本上使用 php 的 echo)。为了与这些自定义字段的工作是你需要使用ACF的get_field()也就是用它来存储一个值,回声值和相互作用与价值。

Once I switched to setting my $courseID to this..

一旦我切换到将我的 $courseID 设置为这个..

$courseID = get_field('course_id'); 

Everything worked. My code worked, and all @LoicTheAztec's code approaches also worked.

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

回答by LoicTheAztec

Updaterelated to your recent comment. The 2 ways to explore:

与您最近的评论相关的更新2种探索方式:

1) Instead of you should try to use to get the product object (avoiding the error):

1)而不是你应该尝试使用来获取产品对象(避免错误):

$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) Alternatively if this doesn't work, you should try to use get_post_meta()function to get the product price (or any product meta data)this way:

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

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

This time you should get displayed the price with one or the other solutions.

这一次,您应该使用一种或另一种解决方案显示价格。



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

更新:可能您还需要将 $courseID 转换为整数变量。

Because you need to use your variable $courseIDinside wc_get_product()(without the 2 ') function this way:

因为您需要以这种方式$courseIDwc_get_product()(没有 2 ')函数内部使用您的变量:

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

This should work now.

这现在应该可以工作了。

回答by Tristup

You can try this out :

你可以试试这个:

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

回答by Owais Alam

You can also use this method

你也可以使用这个方法

$_product = wc_get_product( id );

Official API-docs: wc_get_product

官方 API 文档:wc_get_product