【问题标题】:Send a custom email to seller when product is sold in Woocommerce在 Woocommerce 中销售产品时向卖家发送自定义电子邮件
【发布时间】:2018-02-27 19:40:31
【问题描述】:

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

这是我目前所拥有的

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

}

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    我已经完全重新审视了您的代码:

    • 主要代码位于由 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 文件中。

    代码没有经过真正的测试,但不会出错。它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      相关资源
      最近更新 更多