WooCommerce: Get custom post type IDs or specific order status email notification

Viewed 315

Attempting to receive and display custom post type wcfa_attachment ID to only be displayed on a order status change notification email with the id of wc_order_status_email_731.

For example:

$attachments = get_posts( array( 'numberposts' => -1, 'post_parent' => $order_id, 'post_type'=> 'wcfa_attachment', 'order' => 'ASC', 'orderby' => 'ID', 'suppress_filters' => false/*,  'meta_key' => 'wcaf_is_active', 'meta_value' => true */ ));

I am able to target wc_order_status_email_731 and pull the order_id then display if for testing but not the custom post type post id's for wcfa_attachment associated with the order.

function add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $email->id == 'wc_order_status_email_731' ) {
        
        $order_id = $order->get_id();

        /// works to print the order id so we know its being received
        print_r ($order_id);
       
        function get_attachments($order_id)
        {
            $result = array();
        
            $attachments = get_posts( array( 'numberposts' => -1, 'post_parent' => $order_id, 'post_type'=> 'wcfa_attachment', 'order' => 'ASC', 'orderby' => 'ID', 'suppress_filters' => false/*,  'meta_key' => 'wcaf_is_active', 'meta_value' => true */ ));
            foreach((array)$attachments as $attachment)
            {
                $result[$attachment->ID] = $this->get_attachment($attachment->ID);  
            }       
            return $result;
            print_r ($result);
        }
    }
}
1 Answers

The post ID that you are looking for your custom post type, is simply $attachment->ID.

Now you should never embed a function in another function. Just add them apart.

Then you can call your function in the other function like:

function get_attachments( $order_id ) {
    $result = array();
    
    $attachments = get_posts( array( 'numberposts' => -1, 'post_parent' => $order_id, 'post_type'=> 'wcfa_attachment', 'order' => 'ASC', 'orderby' => 'ID', 'suppress_filters' => false/*,  'meta_key' => 'wcaf_is_active', 'meta_value' => true */ ));
    foreach((array)$attachments as $attachment){
        $result[$attachment->ID] = $this->get_attachment($attachment->ID);  
    }
    return $result;
}

function add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $email->id == 'wc_order_status_email_731' ) {
       
       $order_id = $order->get_id();
    
        /// works to print the order id so we know its being received
        print_r ($order_id);
        
        $attachments = get_attachments( $order_id ); // Call the function
        
        print_r ($attachments); 
    }
}

It should better work.

Related