• Hi,

    I have a page for logged-in-users, where they can add a text to an order. I have already added a custom smart tag (WooOrderID) but I now need to let the user select which order to add the text to. To achieve this, I would like a dropdown, where the user can select the order by it’s order-id (say the user has three orders, the dropdown would show 700, 734, 811).

    How do I auto-populate a dropdown?

    Here’s the code for getting the order-ids into a smart tag (it only get’s the oldest id):

    
    // Register wpForms SmartTag
    function wpf_dev_register_smarttag( $tags ) {
     
    	$tags['woo_orderid'] = 'WooOrderID';
         
        return $tags;
    }
    add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );
     
    //Pull Woo data from UserID 
    function wpf_dev_process_smarttag( $content, $tag ) {
    
    	$userID = get_current_user_id();
    	
    		if ( 'woo_orderid' === $tag ) {
            
    			$order= wc_get_orders(array(
        			'customer_id' => $userID,
        			'return' => 'ids',
    			));	
    
    			foreach ($order as $ids => $oids) {
    				$oids;
    			}
    
    			$content = str_replace( '{woo_orderid}', $oids, $content );
        }	
    	
    	return $content;
    }
    add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 2 );
    // End wpf_dev_register_smarttag function
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter webarkitekterne

    (@webarkitekterne)

    I have refined the code a bit, and now get the associated product name from the order-id as well. I still need to find a way to get this into a wpForms dropdown.

    Anyone?

    
    // Register wpForms SmartTag
    function wpf_dev_register_smarttag( $tags ) {
     
    	$tags['woo_orderid'] = 'WooOrderID';
         
        return $tags;
    }
    add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );
     
    //Use UserID to pull users Woocommerce order data
    function wpf_dev_process_smarttag( $content, $tag ) {
    
    	$userID = get_current_user_id();
    	
    	if( $userID > 0 ) {
            
    			$orders= wc_get_orders(array(
        			'customer_id' => $userID, //get_current_user_id(),
        			'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 . ' (Order ID: ' . $oids . ')');
    				}
    			}
    			
    	$content = str_replace( '{woo_orderid}', $productname . ' (Ordre ID: ' . $oids . ')', $content );
    	
    	return $content;
    	}
    	
    	else {
    		return;
    	}
    }
    add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 2 );
    // End wpf_dev_register_smarttag function
    
    Plugin Support Ethan Choi

    (@ethanchoi)

    Hi @webarkitekterne,

    Currently Smart Tags aren’t processed within the Dropdown fields, and can only be used as the default value of text fields.

    I apologize for the incovenience with this.

    Thread Starter webarkitekterne

    (@webarkitekterne)

    Can I create a dropdown in a function, grap the form ID insert the dropdown as HTML (perpaps as a shortcode) and let the dropdown-data be submitted together with the rest of the form? If so, where do I find the form ID (the generic ID so the function can be used on multiple forms)?

    Plugin Support Ethan Choi

    (@ethanchoi)

    Hi @webarkitekterne,

    I apologize as we don’t have a way to do what you’ve described. As customizations like this is outside of our scope for support, if you’d like to look into custom development options, we highly recommend using Codeable.

    Thanks!

    Thread Starter webarkitekterne

    (@webarkitekterne)

    I’m doing the custom development. I don’t need another to do the development. I’m asking for help to do the custom development (just like any other custom developer might do) without wasting time reverse engineering your plugin.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Auto-populate dropdown with Order-IDs from Woocommerce’ is closed to new replies.