在Woocommerce中销售产品时向卖家发送自定义电子邮件

采取

我有一个网站,允许用户(卖方)在前端输入产品。当有产品销售时,我想给卖家发电子邮件,让他们知道产品已售出。

这是我到目前为止的

add_filter ('woocommerce_payment_complete', 'send_seller_email');

function send_seller_email($order_id) { 

$order = wc_get_order( $order_id );
$items = $order->get_items();

    //foreach loop to get sellers email from order start

    foreach ( $items as $item ) {

        $product_name = $item->get_name();
        $product_id = $item->get_product_id();
        $sent_to =  wp_get_object_terms( $product_id, 'soldby', false );
        $seller_send_email = $sent_to[0]->name;
        $seller_email = get_user_by('ID', $seller_send_email);
        $email_to_sent_sold_conformation .= $seller_email->user_email; 

    }//foreach loop to get sellers email end


    //Send seller email start
        $to = $email_to_sent_sold_conformation;

        $subject = 'Sold! ship now: ' . get_the_title($product_id);

        $message = '';
        $message .= '<p>' . get_the_excerpt($product_id) . '…</p>';
        $message .= '<p><a href="' . get_permalink($product_id) . '"></a></p>';

        wp_mail($to, $subject, $message );
     //Send seller email end

}
LoicTheAztec

我已经完全回顾了您的代码:

  • 主代码在一个实用程序函数中,由2个挂钩函数调用
  • 挂钩的功能woocommerce_new_order将触发已付款的订单。
  • 挂钩的功能woocommerce_order_status_changed将在更新状态更改时触发经过验证的订单。

编码:

// Utility function sending the email notification
function send_seller_email( $order ) {
    $data = array();

    // Loop through each order item
    foreach ( $order->get_items() as $item_id => $item ) {
        if( get_post_meta( $order->get_id(), '_sent_to_seller_'.$item_id, true ) )
            continue; // Go to next loop iteration

        $product_id = $item->get_product_id();

        // Untested part
        $soldby =  wp_get_object_terms( $product_id, 'soldby', false );
        if( empty($soldby) ) continue; // Go to next loop iteration
        $seller_id = $soldby[0]->name;
        $seller = get_user_by( 'ID', $seller_id );
        $seller_email = $seller->user_email;

        // Set the data in an array (avoiding seller email repetitions)
        $data[$seller_email][] = array(
            'excerpt' => get_the_excerpt($product_id),
            'title'    => get_the_title($product_id),
            'link'    => get_permalink($product_id),
        );
        // Update order to avoid notification repetitions
        update_post_meta( $order->get_id(), '_sent_to_seller_'.$item_id, true );
    }

    if( count($data) == 0 ) return;

    // Loop through custom data array to send mails to sellers
    foreach ( $data as $email_key => $values ) {
        $to = $email_key;
        $subject_arr = array();
        $message = '';

        foreach ( $values as $value ) {
            $subject_arr[] = $value['title'];
            $message      .= '<p><a href="'.$value['link'].'">'.$value['title'].'</a></p>';
            $message      .= '<p>'.$value['excerpt'].'…</p>';
        }
        $subject = 'Sold! ship now: '.implode( ', ', $subject_arr );

        // Send email to seller
        wp_mail( $to, $subject, $message );
    }
    exit();
}

add_action('woocommerce_new_order', 'new_order_seller_notification', 10, 1 );
function new_order_seller_notification( $order_id ) {
    $order = wc_get_order( $order_id );

    if( ! ( $order->has_status('processing') || $order->has_status('completed') ) )
        return; // Exit

    send_seller_email( $order );
}

add_action( 'woocommerce_order_status_changed', 'order_status_seller_notification', 20, 4 );
    function order_status_seller_notification( $order_id, $status_from, $status_to, $order ) {

    if( ! ( $status_to == 'processing' || $status_to == 'completed' ) )
        return; // Exit

    send_seller_email( $order );
}

代码进入您的活动子主题(或主题)的function.php文件中。

该代码未经真正测试,但不会出错。它应该工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

产品标签的WooCommerce自定义电子邮件

WooCommerce-每个产品的自定义电子邮件

在Woocommerce管理员电子邮件通知中显示产品自定义字段值

在Woocommerce订单和电子邮件中显示产品变体的自定义字段

在WooCommerce“订单已完成”电子邮件中添加自定义产品字段

如何在新订单电子邮件中显示WooCommerce自定义产品元?

在woocommerce电子邮件中输出产品自定义字段(图像)

在woocommerce订单电子邮件中显示多个产品的自定义字段

当订单状态更改为自定义订单状态时,从WooCommerce发送电子邮件

为特定的Woocommerce电子邮件通知向BCC添加自定义电子邮件

在WooCommerce中自定义订单状态更改时发送电子邮件通知

WooCommerce管理订单中的自定义操作按钮,用于发送电子邮件

在WooCommerce电子邮件订购项目中显示可变产品的产品自定义字段

雷鸟:在发送电子邮件时,从身份列表中删除“从地址自定义”

Woocommerce新订单电子邮件通知中的自定义“回复”电子邮件标题

EspoCRM:在自定义实体的流中写入评论时发送自定义电子邮件

根据 WooCommerce 电子邮件通知中的产品类别添加自定义文本“每个项目”

将产品自定义字段添加到WooCommerce已完成的订单电子邮件中

在Woocommerce中的特定电子邮件通知上为特定产品添加自定义文本

在WooCommerce的电子邮件和订单页面中保存并显示产品自定义数据

在WooCommerce电子邮件通知上显示自定义产品图片

根据产品属性添加自定义woocommerce电子邮件

在 woocommerce 电子邮件标题中输出产品自定义字段

在Woocommerce电子邮件下载中用自定义元替换产品名称

WooCommerce电子邮件未通过WooCommerce外部的自定义WordPress页面模板发送

发送带有保存在Woocommerce 3中的其他帐户字段值的自定义电子邮件

发送给管理员的 WooCommerce 电子邮件通知中的自定义格式地址部分

自定义电子邮件丢失的产品和小计

通过宏向多个收件人发送自定义电子邮件