Добавьте название продукта в тему электронной почты в WooCommerce - PullRequest
1 голос
/ 15 апреля 2019

Как я могу добавить название продукта в строке темы этого письма. Есть ли список доступных фильтров для строк темы электронной почты? Я хотел бы иметь возможность сделать это без необходимости писать пользовательские php. Я хочу сделать что-то вроде этого:

Завершенный заказ - [{product_name}] ({order_number}) - {order_date} Ниже код пробован и добавлен в functions.php, но тема не отображается:

add_filter( 'woocommerce_email_subject_customer_completed_order', 'customer_completed_order_subject', 10, 2 ); 
function customer_completed_order_subject($string,$order){
$fname = $order->billing_first_name;
$lanme = $order->billing_last_name;
$email = $order->billing_email;
$items = $order->get_items();
foreach($items as $meta_key => $items){
    $product_name = $items['name'];
    $product_id = $items['product_id'];
}
$subject_line = get_field('email_subject',$product_id);
$subject_line = str_replace('{product}',$product_name,$subject_line);
$subject_line = str_replace('{biller_fname}',$fname,$subject_line);
$subject_line = str_replace('{biller_lastname}',$lanme,$subject_line);
$subject_line = str_replace('{biller_email}',$email,$subject_line);
$subject_line = str_replace('{blog_name}',get_bloginfo('name'),$subject_line);
return $subject_line;
}

1 Ответ

1 голос
/ 15 апреля 2019

Вы не используете правильный хук, и есть ошибки ... Вместо этого используйте следующее:

add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 10, 2 );
function add_custom_email_format_string( $string, $email ) {
    $order       = $email->object; // Get the instance of the WC_Order OBJECT
    $order_items = $order->get_items(); // Get Order items
    $order_item  = reset($order_items); // Get the irst order item

    // Replace placeholders with their respective values
    $string = str_replace( '{biller_fname}', $order->billing_first_name(), $string );
    $string = str_replace( '{biller_lname}', $order->billing_last_name(), $string );
    $string = str_replace( '{biller_email}', $order->billing_email(), $string );
    $string = str_replace( '{product_name}', $order_item->get_name(), $string );
    $string = str_replace( '{blog_name}', get_bloginfo('name'), $string );

    return $string; 
}

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

Примечание: В заказе может быть много предметов, так много наименований товаров.В приведенном выше коде я оставляю только первое название продукта…

Если вы хотите обрабатывать несколько названий продуктов, вы будете использовать:

add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 10, 2 );
function add_custom_email_format_string( $string, $email ) {
    $order          = $email->object; // Get the instance of the WC_Order OBJECT
    $products_names = array();

    // Loop through order items
    foreach( $order->get_items() as $item ) {
        $products_names[] = $item->get_name();
    };

    // Replace placeholders with their respective values
    $string = str_replace( '{biller_fname}', $order->billing_first_name(), $string );
    $string = str_replace( '{biller_lname}', $order->billing_last_name(), $string );
    $string = str_replace( '{biller_email}', $order->billing_email(), $string );
    $string = str_replace( '{product_name}', implode(' ', $products_names), $string );
    $string = str_replace( '{blog_name}', get_bloginfo('name'), $string );

    return $string;
}

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


Связано: Добавление пользовательского заполнителя для темы электронной почты в WooCommerce

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...