Home > Software design >  Check if order note contains specific words and display message in WooCommerce customer completed or
Check if order note contains specific words and display message in WooCommerce customer completed or

Time:04-05

I'm trying to add a specific function to my customer-completed-order template file in WooCommerce.

I want that function to check if an order note is containing specific word "Loren ipsum" and if yes, show a my selected message.

This is how my customer-completed-order.php email template file looks like:

<?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.7.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
    
        $comment_obj   = get_comment( $comment_id );
        $customer_note = $comment_obj->comment_content;
        $word = "Loren ipsum";
        $mystring = $customer_note;
        $order_id = $order->get_id();

        // ACF field, will give an error if you don't use this specific plugin
        //$pristatysimePatys = get_field('pristatysime_patys', $order_id);

        if (strpos($mystring, $word) !== false) {
            $message = "Your order number " . $order_id . " is sended. " . $customer_note ." .";
        } elseif ($pristatysimePatys) {
            $message = "Your order number " . $order_id . " is sended. The custom field checkbox in order are selected.";
        } else {
            $message = "There are no Loren Ipsum in order notes and custom field with checkbox is not selected.";
        }
        printf($message);
    
    ?>
</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 );

But I do a mistake somewhere, because even if the words "Loren ipsum" is in order note, it will show my else result in the email. Any advice?

CodePudding user response:

Some notes regarding your code attempt:

  • No need to overwrite template files, you can use the woocommerce_email_order_details hook and then target the correct email notification via $email->id
  • get_comment() retrieves comment data given a comment ID or comment object. To get order notes you can use wc_get_order_notes() or $order->get_customer_note() to get the customer note during checkout.

So you get:

function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
    // Target specific email notification
    if ( $email->id == 'customer_completed_order' ) {
        // Get order id
        $order_id = $order->get_id();

        // Get order notes
        $order_notes = wc_get_order_notes( array(
            'order_id'  => $order_id,
            'order_by'  => 'date_created',
            'order'     => 'ASC',
        ));
        
        // Notes is NOT empty
        if ( ! empty( $order_notes ) ) {
            foreach ( $order_notes as $order_note ) {
                // DEBUG information, delete afterwards
                echo '<p style="color:red;font-size:20px;">' . $order_note->content . '</p>';

                // PHP 8
                if ( str_contains( $order_note->content, 'Loren ipsum' ) ) {
                    echo '<p style="color:blue;font-size:20px;">My text 1</p>';
                }

                // Before PHP 8
                if ( strpos( $order_note->content, 'Loren ipsum' ) !== false ) {
                    echo '<p style="color:blue;font-size:20px;">My text 2</p>';
                }
            }
        }

        // OR get customer note
        $customer_note = $order->get_customer_note();
        
        // NOT empty
        if ( ! empty ( $customer_note ) ) {
            echo '<p style="color:green;font-size:20px;">' . $customer_note . '</p>';
        }
    }
}
add_action ('woocommerce_email_order_details', 'action_woocommerce_email_order_details', 1, 4 );

Code goes in functions.php file of the active child theme (or active theme)

  • Related