Skip Navigation

[Resolved] product details and order details relationship

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 7 years ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by Nicholas 7 years ago.

Assisted by: Beda.

Author
Posts
#515931

Hello I am trying to get the product id from the order id in woocommerce using toolset views.
https://wordpress.stackexchange.com/questions/97176/get-product-id-from-order-id-in-woocommerce

I would like to display the following on a product page: "The following customers bought this product. "

I already made the order post type public and set the post status to wc_completed inside the toolset view.

How would you guys set up the views filter to achieve this?

Maybe work with post type relationship, _product_id meta key, order_item_id or order_ids ?

I know how do achieve this with custom code, but I know it must be possible with toolset.

Here's the custom code that works for me. Maybe someone of you guys can use it.

// Add Shortcode
function wpcl_shortcode( $atts ) {
$output = '';
// Attributes
$customer_atts = shortcode_atts( array(
            'product' => get_the_id(),
'quantity' => false,
    ), $atts );
 
// Code
global $post, $wpdb;
$post_id = $customer_atts['product'];
$wpcl_orders = '';
$columns = array();
$customerquery = "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_itemmeta woim
LEFT JOIN {$wpdb->prefix}woocommerce_order_items oi
ON woim.order_item_id = oi.order_item_id
WHERE meta_key = '_product_id' AND meta_value = %d
GROUP BY order_id;";
$order_ids = $wpdb->get_col( $wpdb->prepare( $customerquery, $post_id ) );
$order_status = get_option( 'wpcl_order_status_select', array('wc-completed') );
if( $order_ids ) {
$args = array(
'post_type'       =>'shop_order',
'post__in'   => $order_ids,
'posts_per_page' =>  999,
'order'          => 'ASC',
'post_status' => $order_status,
);
$wpcl_orders = new WP_Query( $args );
}
if($wpcl_orders) {
$output .= '<table>';
foreach($wpcl_orders->posts as $wpcl_order) {
$order = new WC_Order($wpcl_order->ID);
$output .= '<tr>';
$output .= '<td>' . $order->billing_first_name . ' ' . $order->billing_last_name . '</td>';
$currency =get_woocommerce_currency_symbol();
$datetime=$order->order_date;
$time=strtotime($datetime);
            $output .= '<td>' . date("D, d M Y ",$time) ;
$output .= '<td>' .$currency. $order->order_total ;
if($customer_atts['quantity'] == true) {
if (sizeof($order->get_items())>0) { $singlecount = ''; foreach($order->get_items() as $item) { if( $item['product_id'] == $post_id ) { $productcount[] = $item['qty']; $singlecount+= $item['qty'];  }  } $items['order-qty'] = $singlecount;  $output .= '<td>' . $singlecount . '</td>'; }
}
$output .= '</tr>';
}
$output .= '</table>';
}
return $output;
}
add_shortcode( 'customer_list', 'wpcl_shortcode' );

Thank you for your help.
Cheers,
Nicholas

#516017

I think I replied to the exact same query here already:
https://toolset.com/forums/topic/get-product-id-from-order-id-in-woocommerce/#post-516004

Can we handle this issue in one single thread?

#516035

Hey Beda. Isn't this open for the community?

#516156

I do not understand what you mean.

Of course our Forums are open to the community and everyone visiting them.

What do you mean with your last reply?

Asking the same question in both the Types and Toolset forum is not helpful, as we Supporters reply in both anyway.

I very rarely see another user replying here in the Types forum, specially not on custom code issues related to Toolset, not to Types.

Also, here we will never handle Toolset issues, since it's the Types only forum.

Let's continue on the other thread if you do not mind 🙂

#516157

Sorry I didn't know this. I thought the questions ask here are free to answer for the community.
This is my first thread here.
Alright. Let's continue on the other thread.

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.