Using custom post type to feed a dropdown (Woo order-id into wpforms)
Using the plugin wpForms, I can auto-populate a dropdown with Custom Post Types (they call it “Dynamic Post Type Source”). Say I wanted to use this functionality to auto-populate a dropdown with order-ids from WooCommerce, so a logged-in-user can choose between the users orders.
I’ve written the code for getting a logged-in-user order-id’s, so how would I make a CPT that would not really create new posts, but instead be populated with a logged-in-users order-ids at run time (while the user is at a page with the form dropdown on it)?
Already asked wpForms for support, they did not want to help.
Code for getting the User Order-ids:
function woo_get_orderids_per_user( $content) {
$userID = get_current_user_id();
if( $userID > 0 ) {
$orders= wc_get_orders(array(
'customer_id' => $userID,
'return' => 'ids',
));
foreach ($orders as $oids) {
$order = wc_get_order( $oids );
$items = $order->get_items();
foreach ( $items as $item ) {
$product = wc_get_product( $item['product_id'] );
$productname = $product->get_name();
echo('<br> Product name: ');
print_r($productname . ' (ordrenummer: ' . $oids . ')');
}
}
$content = str_replace( '{woo_orderid}', $productname . ' (Ordre ID: ' . $oids . ')', $content );
return $content;
}
else {
return;
}
}
Leave an answer