Search code examples
phpwordpresswoocommerceemail-notificationsdokan

Customize email subject in WooCommerce with vendor store-name (Dokan)


We found this question https://wordpress.stackexchange.com/questions/320924/woocommerce-order-processing-email-subject-not-changing and it's working fine. We now want to extend this by display the vendor on a order in the order completed mail.

But we are not able to output the vendor store name.

Is there a obvious error in our code?

add_filter( 'woocommerce_email_subject_customer_completed_order',

'change_completed_email_subject', 1, 2 );
function change_completed_email_subject( $subject, $order ) {
global $woocommerce;

// Order ID 
$order->get_items();
     
// Author id
$author_id = $product->post->post_author;
        
// Shopname
$vendor = dokan()->vendor->get( $author_id );
$shop_name = $vendor->get_shop_name();

// Blogname
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

// Output subject
$subject = sprintf( '%s, Deine %s Bestellung (#%s) wurde versendet! Vendor: %s', $order->billing_first_name, $blogname, $order->get_order_number(), $shop_name );
return $subject;
}

Update:

I already tried to get the name via $shop_name = dokan()->vendor->get( $author_id )->get_shop_name(); but no success.


Solution

    • The use of global $woocommerce is not necessary
    • You use $order->get_items();, but don't do anything with it
    • $product is not defined
    • Use $order->get_billing_first_name() VS $order->billing_first_name

    So you get:

    function filter_woocommerce_email_subject_customer_completed_order( $subject, $order ) {
        // Empty array
        $shop_names = array();
        
        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            // Get product object
            $product = $item->get_product();
    
            // Author id
            $author_id = $product->post->post_author;
            
            // Shopname
            $vendor = dokan()->vendor->get( $author_id );
            $shop_name = $vendor->get_shop_name();
            
            // OR JUST USE THIS FOR SHOPNAME
            // Shop name
            // $shop_name = dokan()->vendor->get( $author_id )->get_shop_name();
            
            // NOT in array
            if ( ! in_array( $shop_name, $shop_names ) ) {
                // Push to array
                $shop_names[] = $shop_name;
            }
        }
    
        // Blogname
        $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
    
        // Set subject
        $subject = sprintf( __( '%s, Deine %s Bestellung (#%s) wurde versendet! Vendor: %s', 'woocommerce' ), $order->get_billing_first_name(), $blogname, $order->get_order_number(), implode( ', ', $shop_names ) );
    
        // Return
        return $subject;
    }
    add_filter( 'woocommerce_email_subject_customer_completed_order', 'filter_woocommerce_email_subject_customer_completed_order', 10, 2 );