phpwordpresswoocommerce

Show consent to receive ads in WooCommerce emails


I'm using a code that adds a checkbox with consent to send ads:

//Additional consent checkbox for receiving promotional materials
add_action('woocommerce_review_order_before_submit', 'add_custom_privacy_checkbox', 5);
function add_custom_privacy_checkbox() {
    ?>
    <p class="form-row privacy-accept">
        <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
            <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="custom_privacy" id="custom_privacy">
            <span class="woocommerce-terms-and-conditions-checkbox-text">
                I agree to receive <a href="/advertisement/">promotional materials</a> from the website.
            </span>
        </label>
        <input type="hidden" name="custom_privacy_field" value="1">
    </p>
    <?php
}
//We save the consent received in the meta-data of the order.
add_action('woocommerce_checkout_update_order_meta', 'save_custom_privacy_consent');
function save_custom_privacy_consent($order_id) {
    if (isset($_POST['custom_privacy'])) {
        update_post_meta($order_id, '_custom_privacy_consent', 'yes');
    } else {
        update_post_meta($order_id, '_custom_privacy_consent', 'no');
    }
}
//We display on the order page that the consent has been received.
add_action('woocommerce_admin_order_data_after_billing_address', 'display_custom_privacy_consent_in_admin', 10, 1);
function display_custom_privacy_consent_in_admin($order) {
    $consent = get_post_meta($order->get_id(), '_custom_privacy_consent', true);
    $status = ($consent === 'yes') ? 'Received' : 'Not received';
    
    echo '<p>Consent to receive promotional materials: ' . $status . '</p>';
}

Now I need to show the result in the WooCommerce emails after the Billing Address. I added the code:

//We show in the emails that the consent has been received.
add_action('woocommerce_email_footer', 'display_custom_privacy_consent_in_emails', 10, 1);
function display_custom_privacy_consent_in_emails($order) {
    $consent = get_post_meta($order->get_id(), '_custom_privacy_consent', true);
    $status = ($consent === 'yes') ? 'Received' : 'Not received';
    
    echo '<p>Consent to receive promotional materials: ' . $status . '</p>';
}

But now, when placing an order, the message:

An error occurred while processing your order.

is constantly being displayed and the order is not being processed. If woocommerce_email_footer is changed to another hook, then the order is processed correctly.

How can I change the code to fix this error?
How can I show the result after the billing address?


Solution

  • Here is how to get your order from your email footer. Add this in your theme functions.php. Tested and works.

    add_action( 'woocommerce_email_footer', function( $email ) {
        //Email contains object of the order just for safety we want to confirm its WC_Order
        //$order is object containing all order info if you need more data from it.
        $order = $email->object;
        if ( $order instanceof WC_Order ) {
            $consent = get_post_meta($order->get_id(), '_custom_privacy_consent', true);
            $status = ($consent === 'yes') ? 'Received' : 'Not received';
            echo '<p>Consent to receive promotional materials: ' . $status . '</p>';
        }
    }, 5 );