WooCommerce:获取自定义 post 类型 ID 或特定订单状态电子邮件通知

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

正在尝试接收和显示自定义 post 类型 wcfa_attachment ID 仅显示在 ID 为 wc_order_status_email_731 的订单状态更改通知电子邮件中。

例如:

$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 */ ));

我能够定位 wc_order_status_email_731 并拉取 order_id 然后显示是否用于测试而不是自定义 post 类型 post id 用于 wcfa_attachment与订单关联。

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);
        }
    }
}

您要查找自定义 post 类型的 post ID 就是 $attachment->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;
}

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); 
    }
}

它应该更好用。