Displaying Vendor Details in WooCommerce Emails

Hello everyone,

I am currently facing an issue with displaying vendor details in the order confirmation emails sent by WooCommerce when a customer places an order. My goal is to ensure that both the customer and the vendor receive each other’s contact details after the purchase. While the vendor can see the customer’s information by checking the order details in their dashboard, this is not sufficient for my needs.

Steps Taken:

  1. Adding Vendor Information to Order Meta Data: I added the following code to the functions.php file to save the vendor’s information in the order meta data when the order is created:
add_action('woocommerce_checkout_create_order', 'add_vendor_info_to_order_meta', 20, 2);

function add_vendor_info_to_order_meta($order, $data) {
    // Get the vendor ID associated with the order item
    foreach ($order->get_items() as $item_id => $item) {
        $product_id = $item->get_product_id();
        $vendor_id = get_post_field('post_author', $product_id);

        if ($vendor_id) {
            $vendor_name = get_user_meta($vendor_id, 'nickname', true); // or 'first_name' and 'last_name'
            $vendor_phone = get_user_meta($vendor_id, 'billing_phone', true);

            // Save this information in the order meta data
            $order->update_meta_data('_vendor_name', $vendor_name);
            $order->update_meta_data('_vendor_phone', $vendor_phone);
        }
    }
}
  1. Modifying the Email Template to Display Vendor Information: I edited the customer-processing-order.php file in the woocommerce/emails folder of my theme to display the vendor’s information in the email:
<?php
/**
 * Customer processing order email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-processing-order.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates\Emails
 * @version 3.7.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/*
 * @hooked WC_Emails::email_header() Output the email header
 */
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<?php /* translators: %s: Customer first name */ ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $order->get_billing_first_name() ) ); ?></p>
<?php /* translators: %s: Order number */ ?>
<p><?php printf( esc_html__( 'Just to let you know &mdash; we\'ve received your order #%s, and it is now being processed:', 'woocommerce' ), esc_html( $order->get_order_number() ) ); ?></p>

<?php

/*
 * @hooked WC_Emails::order_details() Shows the order details table.
 * @hooked WC_Structured_Data::generate_order_data() Generates structured data.
 * @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
 * @since 2.5.0
 */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

/*
 * @hooked WC_Emails::order_meta() Shows order meta data.
 */
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );

/*
 * @hooked WC_Emails::customer_details() Shows customer details
 * @hooked WC_Emails::email_address() Shows email address
 */
do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );

// Add vendor information to the email
$vendor_name = $order->get_meta('_vendor_name');
$vendor_phone = $order->get_meta('_vendor_phone');

if ($vendor_name && $vendor_phone) {
    echo '<h2>' . __('Vendor Information', 'woocommerce') . '</h2>';
    echo '<p>' . __('Vendor Name', 'woocommerce') . ': ' . esc_html($vendor_name) . '</p>';
    echo '<p>' . __('Vendor Phone', 'woocommerce') . ': ' . esc_html($vendor_phone) . '</p>';
}

/**
 * Show user-defined additional content - this is set in each email's settings.
 */
if ( $additional_content ) {
    echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
}

/*
 * @hooked WC_Emails::email_footer() Output the email footer
 */
do_action( 'woocommerce_email_footer', $email );

Problem Encountered:

The vendor information does not appear in the email as expected. I have verified that the meta data is being added to the order, but it does not seem to be retrieved correctly in the email template.

What I am looking for:

Advice or solutions to fix this issue and ensure that the vendor information is correctly displayed in the WooCommerce order confirmation emails. It is important for me that both the customer and the vendor receive each other’s contact details after the purchase. The current solution of the vendor seeing the customer’s details by checking the order in their dashboard is not sufficient for my needs.

Thank you in advance for your help!

Hi,

I recommend simply using the WooCommerce email hooks, you don’t have to save the vendor details before the email is sent – HivePress already saves the vendor ID in the order “hp_vendor” meta field. In the email hook, if the order object is available, you can do something like:

$vendor_id=$order->get_meta('hp_vendor');

if($vendor_id) {
    $vendor=\HivePress\Models\Vendor::query()->get_by_id($vendor_id);

    if($vendor) {
        echo $vendor->get_name();
        // access any vendor field similarly
    }
}

Hi Ihor,

Thank you for your previous advice. I have followed your recommendations and used the WooCommerce email hooks to add vendor details to the order confirmation email. However, I am still encountering an issue: the vendor name appears correctly, but the phone number does not show up in the email. The phone number is entered via a HivePress attribute form, and it is displayed correctly in the HivePress interface.

Here are the steps and code snippets I have used:

Code to Synchronize Vendor Phone Attribute

php

Copy code

add_action('hivepress/v1/models/vendor/update', 'sync_vendor_phone_to_user_meta', 10, 2);

function sync_vendor_phone_to_user_meta($vendor_id, $vendor) {
    $user_id = $vendor->get_user_id();
    $vendor_phone = $vendor->get_attribute('telephone'); // Ensure this slug is correct

    if ($user_id && $vendor_phone) {
        update_user_meta($user_id, 'billing_phone', $vendor_phone);
    }
}

Code to Add Vendor Details to WooCommerce Email

php

Copy code

add_action('woocommerce_email_order_details', 'add_vendor_details_to_email', 20, 4);

function add_vendor_details_to_email($order, $sent_to_admin, $plain_text, $email) {
    // Retrieve the vendor ID from the order meta
    $vendor_id = $order->get_meta('hp_vendor');

    if ($vendor_id) {
        // Get vendor details using the HivePress model
        $vendor = \HivePress\Models\Vendor::query()->get_by_id($vendor_id);

        if ($vendor) {
            $vendor_name = $vendor->get_name();
            $vendor_phone = $vendor->get_attribute('telephone'); // Using HivePress attribute

            ?>

            <h2><?php _e('Vendor Information', 'woocommerce'); ?></h2>
            <p><?php _e('Vendor Name', 'woocommerce'); ?>: <?php echo esc_html($vendor_name); ?></p>
            <p><?php _e('Vendor Phone', 'woocommerce'); ?>: <?php echo esc_html($vendor_phone); ?></p>
            
            <?php
        }
    }
}

Issue Encountered

While the vendor name displays correctly, the phone number does not appear in the email. The phone number is entered and saved via a HivePress form attribute with the label “telephone”. Here are the steps I’ve taken:

  1. Ensured the phone number is entered in the vendor’s profile using HivePress.
  2. Used the code snippets above to synchronize the phone number with the user meta and display it in the email.
  3. Confirmed that the attribute slug “telephone” is used correctly.

Despite these steps, the phone number remains blank in the emails. Could you please help me identify what might be going wrong or suggest a different approach to ensure the phone number is displayed correctly?

Thank you for your assistance!
Sans titre

Please try:

$vendor->get_user__id();

Instead of:

$vendor->get_user_id();

Accessing fields of related models (the user of the vendor profile) requires a double underscore (this allows using single underscores in the model field names without conflicts) so this may resolve the issue.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.