Добавить настраиваемую ссылку в уведомление по электронной почте Woocommerce о новом заказе

Я создаю сайт-ресторан. В предыдущей версии сайта в письме, которое ресторан получал, когда кому-то нужна была доставка, у меня была ссылка на карты Google. Ресторану легко использовать это для перехода к клиенту ресторана. Теперь я меняю сайт и буду работать с woocommerce, но мне также нужна ссылка на карты Google в письме администратора.

Код со старого сайта. (Гиперссылка со ссылкой на карты Google под названием «Map It».)

<a href="http://maps.google.com/maps?q=nieuwstraat+15+brussel+1000+Belgium" 
class="m_8816063166882312894map-it-link" target="_blank" 
data-saferedirecturl="https://www.google.com/url? 
q=http://maps.google.com/maps? 
q%3Dnieuwstraat%2B15%2Bbrussel%2B1000%2BBelgium&amp">
Map It</a>

Чтобы сделать это в woocommerce, мне нужно изменить шаблон woocommerce admin-new-order, чтобы добавить гиперссылку под адресом, но я не знаю, как это сделать.

Шаблон admin-new-order.php:

<?php
/**
 * Admin new order email
 *
 * This template can be overridden by copying it to 
yourtheme/woocommerce/emails/admin-new-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://docs.woocommerce.com/document/template-structure/
 * @author WooThemes
 * @package WooCommerce/Templates/Emails/HTML
 * @version 2.5.0
 */

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

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

 <p><?php printf( __( 'You have received an order from %s. The order is as 
follows:', 'woocommerce' ), $order->get_formatted_billing_full_name() ); ?> 
</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 );

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

Как я могу это исправить?

Изменить: гиперссылка на карты Google должна основываться на адресе выставления счета из заказа woocommerce, а не на жестко заданном примере: Nieuwstrat 15 Brussel

Основываясь на отличном ответе LoicTheAztec, я использовал, слегка адаптированный:

add_action ('woocommerce_email_order_details', 
'including_global_email_data', 2, 4 );
function including_global_email_data( $order, $sent_to_admin, $plain_text, 
$email ){
    // Set global email variable
    $GLOBALS['email_data'] = array( 'email' => $email, 'admin' => 
$sent_to_admin,'order' =>$order );
}

add_filter ('woocommerce_email_footer_text', 'custom_email_footer_text', 20, 
1 );
function custom_email_footer_text( $footer_text ){
    // Get global variables
    $refNameGlobalsVar = $GLOBALS;
    $sent_to_admin = $refNameGlobalsVar['email_data']['admin'];
    $email         = $refNameGlobalsVar['email_data']['email'];
    $order         = $refNameGlobalsVar['email_data']['order'];

    // Only for admin "New order" email notification
    if( $email->id === 'new_order' ) // Or also:  if( $sent_to_admin )
    {
    $urltemp.=$order->billing_address_1.' '.$order->billing_address_2.' 
'.$order->billing_city.' '.$order->billing_postcode.' '.$order- 
    >billing_country;
    $urltemp=str_replace(" ","+",$urltemp);
    $urltemp='http://maps.google.com/maps?q='.$urltemp;
    $footer_text.='<br><a href="'.$urltemp.'" 
class="m_8816063166882312894map-it-link" target="_blank" data- 
   saferedirecturl="'.$urltemp.'&amp">Map It</a>';
    }
    return $footer_text;
}

person gilberke    schedule 15.05.2018    source источник


Ответы (1)


Обновление: если вам нужно добавить это в нижний колонтитул для уведомления администратора "Новый заказ", используйте следующее:

add_action ('woocommerce_email_order_details', 'including_global_email_data', 2, 4 );
function including_global_email_data( $order, $sent_to_admin, $plain_text, $email ){
    // Set global email variable
    $GLOBALS['email_data'] = array( 'email' => $email, 'admin' => $sent_to_admin );
}

add_filter ('woocommerce_email_footer_text', 'custom_email_footer_text', 20, 1 );
function custom_email_footer_text( $footer_text ){
    // Get global variables
    $refNameGlobalsVar = $GLOBALS;
    $sent_to_admin = $refNameGlobalsVar['email_data']['admin'];
    $email         = $refNameGlobalsVar['email_data']['email'];

    // Only for admin "New order" email notification
    if( $email->id === 'new_order' ) // Or also:  if( $sent_to_admin )
    {
    $footer_text .= '<br><a href="http://maps.google.com/maps?q=nieuwstraat+15+brussel+1000+Belgium" class="m_8816063166882312894map-it-link" target="_blank" data-saferedirecturl="https://www.google.com/url?q=http://maps.google.com/maps?q%3Dnieuwstraat%2B15%2Bbrussel%2B1000%2BBelgium&amp">Map It</a>';
    }
    return $footer_text;
}

Код находится в файле function.php вашей активной дочерней темы (или активной темы). Проверено и работает.

person LoicTheAztec    schedule 15.05.2018
comment
Большое спасибо, это мне уже очень помогает! Но мне нужна информация о платежном адресе заказа вместо информации о статическом адресе в ссылке. Nieuwstraat 15 Брюссель 1000 Бельгия является лишь примером. - person gilberke; 15.05.2018