当前位置: 首页 > 知识库问答 >
问题:

在特定订单状态通知电子邮件中获取自定义post类型id

丰超
2023-03-14

尝试接收并显示自定义post类型WCFA_Attachmentid以仅在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自定义post类型post 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, '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个答案

劳昊明
2023-03-14

您正在查找的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); 
    }
}

它最好能起作用。

 类似资料:
  • 我已经在我的WooCommerce回拨订单()中创建了自定义订单状态: 现在我想收到一封电子邮件,每当收到一个订单,已经给出了状态栏。我根据这篇有用的文章创建了一个插件:如何添加自定义的WooCommerce电子邮件 这个链接包含我的插件源代码和functions.php代码。 我在function.php中添加了钩子: 当订单更改为“延迟订单”状态时,不会发生任何情况。 有什么想法吗? 我已经尝

  • 我添加了自定义状态: 我添加了自定义电子邮件: 我可以在电子邮件设置中看到电子邮件,在订单状态下拉菜单中看到状态。现在,每当订单状态更改为时,我就需要发送我的新电子邮件。过渡钩似乎从未开火。 我也试过: 这似乎也不起作用...有什么想法吗?

  • 我创建了一个自定义电子邮件类并将其添加到Woocommerce中。当我转到WooCommerce中的电子邮件设置时,我可以在那里看到我的模板,当我手动触发它时,电子邮件就会到达目标电子邮件帐户。现在我遇到的问题是,我在我的类中添加了一个动作,该动作应该检测订单状态更改,并在订单被设置为我的自定义状态时执行我的触发器功能: add_action('woocommerce_order_status_w

  • 在Woocommerce中,我使用“在管理订单中显示Woocommerce自定义签出字段值使其可编辑”回答代码,在管理订单页面中显示自定义字段值,效果良好。 我的问题:是否可以在电子邮件通知中显示该自定义字段值?

  • 我在这个相关的答案中找到了对我的问题的部分和可行的答案: send-an-email-notification-when-order-status-changhe-fron-pendig-to-cancelled 我正在考虑使用提供的解决方案,但想看看我是否可以更改电子邮件通知,明确地说,“未决付款订单现在取消”,所以它不同于常规取消的订单。 我该怎么做?

  • 我在我的WooCommerce安装中创建了一个自定义订单状态,称为Quote。 现在我想收到一封电子邮件,每当收到一个订单,已经给出了状态栏。我根据这篇有用的文章创建了一个插件:http://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/ 我的插件基本上是从文章复制的,我只是更改了电子邮件的内容。我想改变的是什么触发了电子邮