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

问题描述

尝试接收和显示自定义帖子类型 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,然后显示 if 用于测试,但不显示与订单关联的 wcfa_attachment自定义帖子类型帖子 ID。

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,'Meta_value' => true */ ));
            foreach((array)$attachments as $attachment)
            {
                $result[$attachment->ID] = $this->get_attachment($attachment->ID);  
            }       
            return $result;
            print_r ($result);
        }
    }
}

解决方法

您正在寻找自定义帖子类型的帖子 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); 
    }
}

它应该会更好地工作。