自定义订单已完成电子邮件通知:显示产品自定义字段

时间:2017-03-12 04:45:37

标签: php wordpress woocommerce orders email-notifications

我们正在使用WooCommerce Memberships插件与WooCommerce网站销售健身教学视频。

我们为客户销售的每个视频创建了一个WooCommerce产品,并为该产品创建了一个WooCommerce Membership,该产品在产品售出时激活。

每个视频都在单个WordPress页面上,根据匹配的会员计划进行限制。

我的问题是:
如何向客户发送与他们购买的产品相关的电子邮件通知,其中包含包含视频的Wordpress页面的URL?

我知道我们可以override WooCommerce template emails/customer-completed-order.php,但我不知道如何根据WooCommerce输出自定义字符串“包含视频页面的网址”购买产品。

您可以根据购买的WooCommerce产品帮助输出自定义字符串(包含视频页面的URL)吗?

由于

2 个答案:

答案 0 :(得分:1)

您可以在不使用专用WooCommerce动作挂钩编辑WooCommerce模板的情况下实现它,分2步(如果步骤1尚未完成)

  • 在管理产品页面常规设置元框中创建/保存自定义字段。

  • 在您的电子邮件中呈现相关视频页面的链接" 已完成"订购电子邮件通知。

以下是这个功能齐全且经过测试的代码:

# 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>';
    }
}

代码进入活动子主题(或主题)的function.php文件或任何插件文件中。

答案 1 :(得分:0)

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

请在主题的functions.php中尝试此代码段并进行必要的修改。

相关问题