12

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.

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

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.

<?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 );:

Fatal error: Call to a member function get_regular_price() on a non-object in ... 
piet.t
  • 11,718
  • 21
  • 43
  • 52
Andrew-ThinkUp
  • 521
  • 1
  • 7
  • 15

3 Answers3

15

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

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

$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:

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

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

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

piet.t
  • 11,718
  • 21
  • 43
  • 52
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I should have mentioned that I tried this already without the `''` and it generates an error to a call on a non-object. `Fatal error: Call to a member function get_regular_price() on a non-object in ...` – Andrew-ThinkUp Jan 19 '17 at 15:06
  • Thanks for your help! I wouldn't have been able to eliminate the other options without it. Much appreciated! – Andrew-ThinkUp Jan 20 '17 at 20:55
2

You can try this out :

$courseID = the_field('course_id');
$product = get_product( $courseID );
Tristup
  • 3,603
  • 1
  • 14
  • 26
  • Using the older `get_product();` function still does not function when removing the quotes. See my response to @LoicTheAztec below – Andrew-ThinkUp Jan 19 '17 at 15:08
2

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.

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.

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

$courseID = get_field('course_id'); 

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

Andrew-ThinkUp
  • 521
  • 1
  • 7
  • 15
  • Ho yes!!! that was so evident that I didn't see it this time… I have already make an answer with ACF get_field() / the_field()… This is a very common error. – LoicTheAztec Jan 21 '17 at 02:14