0

I am having the a custom product type as shown below

enter image description here

now when a customer purchases that specific product i want to retrieve allowed number of listing field.

i can show that field in product page without any issue by using

echo get_post_meta(get_the_ID(), '_listing number', true);

but as i want this value assigned to a specific user . i think flow should be like below

  1. get the list of active purchases ( order_id's)
  2. from the order id retrieve the product purchased
  3. then go inside the product and get the number of listing value

This is what i have researched for 1.

$order= wc_get_orders(array(
    'customer_id' => get_current_user_id(),
    'return' => 'ids',
        'status' => 'completed',
));

code above retrieves the active order id's correctly.

for the 2nd i got a link but it's not working for me . it returns a Boolean strangly Get Product id from order id in Woocommerce

$order = wc_get_order( $order_id );
$items = $order->get_items();

then if you loop through the items, you can get all the relevant data:

foreach ( $items as $item ) {
    $product_name = $item->get_name();
    $product_id = $item->get_product_id();
    $product_variation_id = $item->get_variation_id();
}

this is the error : Fatal error: Uncaught Error: Call to a member function get_items() on boolean

for the 3rd i expect below code to work : get_post_meta(get_the_ID(), '_listing number', true);

kindly let me know in case the info is insufficient thanks

  • Your error suggests that there is no order present, which is why the method is not available. wc_get_order() is returning null. Try another order_id and see if it works. – Alex Uleberg Jul 1 '18 at 10:33
  • never got the notification. I have the orders present ..it returns a boolean strangely – Latheesh V M Villa Jul 8 '18 at 17:47
  • wc_get_order() should not be returning a boolean, that means that the order id you passed into the function does not exist. Find an order id you know exists and try with that to see if it works. Then figure out why the variable you are passing in has the wrong order id. – Alex Uleberg Jul 31 '18 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.