Пользовательская электронная почта WooCommerce для продукта TAG - PullRequest
1 голос
/ 09 января 2020

Я хочу добавить уникальный текст в электронное письмо Woocommerce "заказ завершен", но ТОЛЬКО в том случае, если купленный продукт имеет определенный тег продукта. Если купленный предмет имеет тег «сбор средств», я хочу, чтобы в электронном письме содержался текст с благодарностью за поддержку наших сборщиков средств. Длинная история, но мы не можем использовать категории для этого, и мы должны использовать теги.

Я попытался отредактировать файл customer-complete-order. php, и это работает, однако я не очень хорошо с PHP код и нет примеров того, как вызывать теги.

Я также пробовал редактировать функции. php на основе того, что сделал кто-то другой, и это насколько я понял.

function woocommerce_custom_email_per_product_depending_on_product_tag( $email_heading, $order ) {
global $woocommerce;
$items = $order->get_items();
foreach ( $items as $item ) {
    $product_tag = $item['product_tag'];
    if ( $product_tag == fundraiser ) {
        $email_body = 'Thanks for buying a fundraiser.';
    }
    return $email_heading;
}   }

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

<?php
/**
 * Customer completed order email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-completed-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/
 * @package WooCommerce/Templates/Emails
 * @version 3.5.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: Site title */ ?>
<p><?php esc_html_e( 'We have finished processing your order.', 'woocommerce' ); ?></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 );

?>
<p>
<?php esc_html_e( 'Thanks for shopping with us.', 'woocommerce' ); ?>
</p>
<?php

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

1 Ответ

1 голос
/ 10 января 2020
function woocommerce_custom_email_per_product_depending_on_product_tag( $order, $sent_to_admin, $plain_text, $email ) {
    $items = $order->get_items();

    foreach ( $items as $item ) {               
        // get an array of the WP_Term objects for a defined product ID
        $terms = wp_get_post_terms( $item['product_id'], 'product_tag' );

        foreach($terms as $term){
            $term_names[] = $term->name; // Product tag Name

            //$term->term_id; Product tag Id
            //$term->slug; Product tag slug
        }
    }

    if ( $email->id == 'customer_processing_order' && in_array('Fundraiser', $term_names) ) {
        echo 'Thanks for buying a fundraiser.';
    }
}
add_action( 'woocommerce_email_before_order_table', 'woocommerce_custom_email_per_product_depending_on_product_tag', 10, 4 );
...