Steve
Steve

Reputation: 1775

Customizing order completed email notification: Displaying product custom field

We're using WooCommerce Memberships plugin with a WooCommerce website that sells fitness instruction videos.

We've created a WooCommerce Product for each video our client is selling and also a WooCommerce Membership matching that product, which is activated when the product is sold.

Each video is on an individual WordPress page, restricted based on matching Membership Plan.

My question is:
How to send to customers an email notification relating to the product they've purchased, containing the URL to the Wordpress page containing the video?

I know we can override WooCommerce template emails/customer-completed-order.php, but I don't know how to output a custom string "containing the URL to the video page" based on what WooCommerce product is purchased.

Can you help with outputting a custom string (containing the URL to the video page) based on what WooCommerce product is purchased?

Thanks

Upvotes: 1

Views: 499

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254438

You can achieve it without editing the WooCommerce templates using dedicated WooCommerce action hooks in 2 steps (if step 1 is not done yet):

  • Creating/Saving a custom field in the admin product pages general setting metabox.

  • Rendering the link to the related video page in your email "completed" order email notification.

Here is this functional and tested code:

# 1) Creating/Saving a custom field in the admin product pages general setting metabox.

// Inserting product general settings custom field (set the related video page ID)
add_action( 'woocommerce_product_options_general_product_data', 'product_general_settings_custom_field_create' );
function product_general_settings_custom_field_create() {
    echo '<div class="options_group">';

    woocommerce_wp_text_input( array(
        'type'              => 'text',
        'id'                => 'video_page_id', // we save the related page ID
        'label'             => __( 'Video page ID', 'woocommerce' ),
        'placeholder'       => '',
        'description'       => __( 'Insert page ID', 'woocommerce' ),
    ) );

    echo '</div>';
}

// Saving the custom field value when submitted (saving the related video page ID)
add_action( 'woocommerce_process_product_meta', 'product_general_settings_custom_field_save' );
function product_general_settings_custom_field_save( $post_id ){
    $wc_field = $_POST['video_page_id'];
    if( !empty( $wc_field ) )
        update_post_meta( $post_id, 'video_page_id', esc_attr( $wc_field ) );
}

# 2) Rendering the related video page link in your email "completed" order email notification.

// Displaying in completed order email notification the related video page permalink
add_action('woocommerce_order_item_meta_end', 'displaying_a_custom_link_in_completed_order_email_notification', 10, 4);
function displaying_a_custom_link_in_completed_order_email_notification($item_id, $item, $order, $html){
    // For completed orders status only
    if ( $order->has_status('completed') ){
         // Get the custom field value for the order item
        $page_id = get_post_meta( $item['product_id'], 'video_page_id', true);
        $page_id = '324';
        // Get the page Url (permalink)
        $page_permalink = get_permalink( $page_id );
        // Get the page title (optional)
        $page_title = get_the_title( $page_id );
        // Displaying the page link
        echo '<br><small>' . __( 'Watch your video: ', 'woocommerce' ). '<a href="'.$page_permalink.'">' . $page_title . '</a></small>';
    }
}

The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Upvotes: 1

mujuonly
mujuonly

Reputation: 11841

function render_product_description($item_id, $item, $order){
    $_product = $order->get_product_from_item( $item );
    echo "<a href='.$_product->post->video_url.'>" . $_product->post->video_url. "</a>"; 

}

add_action('woocommerce_order_item_meta_end', 'render_product_description',10,3);

Please try this code snippet in your theme's functions.php with necessary modification.

Upvotes: 0

Related Questions