skip to Main Content

I’m trying to have the mailpoet optin prechecked on the checkout page.
(I want to use the automated mailpoet WooCommerce mails te send some extra category related shipping information after a couple of days)

I’m using the following JS

jQuery(document).ready(function( $ ){
  $('#mailpoet_woocommerce_checkout_optin').prop('checked', true);
});

De optin is checked only for a short while (unchecked after order is refreshed on load and after a change).

Has anybody got a solution for this?

2

Answers


  1. I don’t know if you still need the solution. But I found a solution. Just place it in a woocommerce hook.

    // define the woocommerce_review_order_before_submit callback
    function action_woocommerce_review_order_before_submit(  ) { ?>
        <script type="text/javascript">
            jQuery(document).ready(function($) {
                $( '#mailpoet_woocommerce_checkout_optin' ).prop( "checked", true );
            });
        </script>
    <?php };
    
    // add the action
    add_action( 'woocommerce_review_order_before_submit', 'action_woocommerce_review_order_before_submit', 10, 0 );
    
    Login or Signup to reply.
  2. There are JS events that are fired on the checkout page anytime something happens that would cause the order summary to be updated (new product added, address changes that affect shipping, etc). They are update_checkout and updated_checkout. In order to do this properly you need to listen for those events and also ensure that if the user manually unchecked the box after you set it for them, that it doesn’t get automatically checked again.

    The problem I was running into with all of the solutions like the previous two offered here is that either it would be checked on page load but then not persist if the update_checkout event was fired, OR even worse… the user would uncheck the box and then if the update_checkout event was fired it would re-check the box for them.

    This is my solution…

    <?php
    
    /**
     * Defaults the MailPoet opt-in checkbox on the WooCommerce and/or
     * Cartflows checkout pages to a checked state. Also properly handles
     * the checkbox when 'update_checkout' events are fired.
     *
     * This code could either be placed in the functions.php of the active theme,
     * a custom plugin, or be injected via plugins like WP Code or Code Snippets
     */
    
    add_action('wp_footer', function() {
        if ( function_exists('is_checkout') && is_checkout() ) {
            ob_start(); ?>
    
            <script type="text/javascript" id="default-checkout-optin-true">    
                (function($) {
                    let isFirstRun = true;
                    let wasAlreadyChecked = false;
                    
                    function checkTheBox() {
                        document.getElementById("mailpoet_woocommerce_checkout_optin").checked = true;
                    }
                    
                    function getTheCheckboxValue() {
                        return document.getElementById("mailpoet_woocommerce_checkout_optin").checked;
                    }
                    
                    /**
                     * this event fires after the ajax request from 'update_checkout' is complete
                     * and the checkout pages has been updated 
                     */
                    $(document).on('updated_checkout', function() {
                        /**
                         * this is the inital checkout load, check the box
                         */
                        if (isFirstRun) { 
                            checkTheBox();
                            isFirstRun = false;
                            wasAlreadyChecked = true;
                        } 
                        /**
                         * not the first 'update_checkout' cycle, persist the
                         * checkbox state if it was already checked before the
                         * event was fired 
                         */
                        else if (!isFirstRun && wasAlreadyChecked) {
                            checkTheBox();
                        }
                    });
                    
                    /**
                     * update_checkout event is fired when anything changes
                     * on the checkout page that would cause possible cart
                     * recalculations. 
                     */
                    $(document).on('update_checkout', function() {
                        /**
                         * if the event fired and it's NOT the first run,
                         * set wasAlreadyChecked to the current checkbox value.
                         * This is so that if the user manually unchecked the box
                         * our 'updated_checkout' event listener above won't re-check
                         * it 
                         */
                        if (!isFirstRun) {
                            wasAlreadyChecked = getTheCheckboxValue();
                        }
                    });
                })(jQuery);
            </script>
    
            <?php echo ob_get_clean();
        }
    }, 999999); // high priority number to ensure this injected into the footer after jQuery
    

    I also created a gist for this…
    https://gist.github.com/reggieofarrell/b41bc9c3ea2dc70b0236767d42e0fc6c

    I realize this may not jive with GDPR rules. This is for those of us that aren’t selling in Europe though.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search