Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created May 29, 2026 09:08
Show Gist options
  • Select an option

  • Save xlplugins/1c51de0fd96bdf269f9c54e689293077 to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/1c51de0fd96bdf269f9c54e689293077 to your computer and use it in GitHub Desktop.
kalvio newsletter subscribe
<?php
/**
* Plugin Name: Plakatdyr — Klaviyo Consent Bridge for Aero Checkout
* Description: Ports the theme's newsletter consent checkbox (from woocommerce/checkout/form-coupon.php)
* into FunnelKit Aero (WFACP) checkout pages, so newsletter-terms reaches the backend on
* Aero orders too. Without this, Aero orders create Klaviyo profiles via the auto-tracking
* integration without consent flag → profile shows as Suppressed instead of Subscribed.
* Author: Plakatdyr Support
* Version: 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Inject the consent checkbox into Aero checkout pages only.
* Mirrors the theme HTML structure from woocommerce/checkout/form-coupon.php line 51-64
* so the existing backend handler that reads $_POST['newsletter-terms'] picks it up unchanged.
*/
add_action( 'wfacp_after_checkout_form_fields', function () {
if ( ! function_exists( 'wfacp_template' ) || ! wfacp_template() ) {
return;
}
$free_product_enabled = function_exists( 'plakatdyr_settings_checkout_is_free_newsletter_product_enabled' )
&& plakatdyr_settings_checkout_is_free_newsletter_product_enabled();
?>
<input type="hidden" id="newsletter-nonce" value="<?php echo esc_attr( wp_create_nonce( 'newsletter' ) ); ?>" />
<div class="form-row terms field klaviyo wfacp-form-control-wrapper" style="display:none;">
<input id="newsletter-input"
type="checkbox"
class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox is-checkbox-alt"
name="newsletter-terms"
value="yes" />
<label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox is-flex" for="newsletter-input">
<span class="has-text-left flex-1">
<?php if ( $free_product_enabled ) : ?>
<strong class="is-block">Ja tak - overrask mig med en GRATIS plakat (Vi vælger plakaten)</strong>
Derudover vil jeg gerne modtage nyhedsbrev og skræddersyet markedsføring inden for vores sortiment via e-mail. Læs mere i vores Samtykkeerklæring for elektronisk post.
<?php else : ?>
<strong class="is-block">Modtag skønne nyheder og tilbud</strong>
Få vores nyhedsbreve og automatiserede mails i din mailindbakke og sociale medier: Instagram, Facebook, Google, Pinterest. Når du tilmelder dig, behandler vi følgende oplysninger om dig - navn, kontaktdetaljer, købshistorik og adfærd på vores hjemmeside. Du kan altid afmelde dig, hvis du ikke længere ønsker at modtage vores nyhedsbrev og automations.
<?php endif; ?>
</span>
</label>
</div>
<?php
}, 50 );
/**
* Replicate the theme's CheckoutNewsletter.js behaviour on Aero pages:
* - on #billing_email blur → POST /wc-api/newsletter/check-status → show/hide .terms.klaviyo
* - on #newsletter-input change → POST /wc-api/cart/newsletter-product → trigger update_checkout
*
* The theme's compiled checkout.js targets form.woocommerce-checkout selectors that Aero doesn't
* always carry, so we run our own scoped version that doesn't depend on those classes.
*/
add_action( 'wp_footer', function () {
if ( ! function_exists( 'wfacp_template' ) || ! wfacp_template() ) {
return;
}
?>
<script>
(function ($) {
if (typeof jQuery === 'undefined') return;
var $body = $(document.body);
function checkNewsletterStatus() {
var email = $('#billing_email').val();
if (!email || email.length <= 3) return;
$.post('/wc-api/newsletter/check-status', {
email: email,
nonce: $('#newsletter-nonce').val()
}).done(function (resp) {
var $labelField = $('.terms.klaviyo');
if (!$labelField.length) return;
var isAlreadySubscribed = resp && resp.data;
$labelField.find('input').prop('checked', false).change();
if (!isAlreadySubscribed) {
$labelField.fadeIn();
} else {
$labelField.hide();
}
}, 'json');
}
// Free-poster signup mirror
$body.on('change', '#newsletter-input', function (e) {
e.preventDefault();
$.post('/wc-api/cart/newsletter-product', { signup: $(this).is(':checked') }, function () {
$body.trigger('update_checkout');
});
});
// Email blur → re-check subscription status
$body.on('blur', '#billing_email', checkNewsletterStatus);
// First run on page load (email might be pre-filled from session)
$(function () { setTimeout(checkNewsletterStatus, 500); });
// Re-run after Aero's AJAX updates rebuild the DOM
$body.on('updated_checkout wfacp_updated_checkout wfacp_form_updated', function () {
setTimeout(checkNewsletterStatus, 300);
});
})(jQuery);
</script>
<?php
}, 100 );
/**
* Belt-and-suspenders: store the opt-in flag on the order as meta during order creation.
* Most theme/Klaviyo backend handlers read $_POST['newsletter-terms'] directly on
* woocommerce_checkout_order_processed and that fires fine for Aero orders. This hook only
* adds a permanent record on the order so if anything reconciles after-the-fact it has the
* consent value available.
*/
add_action( 'woocommerce_checkout_create_order', function ( $order ) {
if ( isset( $_POST['newsletter-terms'] ) && 'yes' === sanitize_text_field( wp_unslash( $_POST['newsletter-terms'] ) ) ) {
$order->update_meta_data( '_newsletter_opt_in', 'yes' );
}
}, 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment