Tcmxc
Tcmxc

Reputation: 491

Send a custom email to seller when product is sold in Woocommerce

I have a site that allows users(sellers) to enter products on the frontend. When there product sells I would like to email seller to let them know product has been sold.

Heres what I have so far

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

}

Upvotes: 1

Views: 1189

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253783

I have completely revisited your code:

  • The main code is in an utility function called by the 2 hooked functions
  • function hooked in woocommerce_new_order that will trigger paid orders.
  • function hooked in woocommerce_order_status_changed that will trigger validated orders on update status change.

The code:

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

Code goes in function.php file of your active child theme (or theme).

The code is not really tested, but doesn't make errors. It should work.

Upvotes: 1

Related Questions