• Resolved doncullen

    (@doncullen)


    I saw this: https://github.com/mailchimp/mc-woocommerce/wiki/Filter-on-newsletter-field

    The documentation there for that filter was very limited. There was no sample code, it did not indicate what parameters were expected, and what the response from the function should be. So it’s pretty unclear as to how to use the filter.

    First thing I tried:

    add_filter('mailchimp_woocommerce_newsletter_field', 'rephrase_mcfield');
    function rephrase_mcfield ($checkbox, $status, $label) {
    	$label = 'This is a test';
    	return $label;
    }

    But nothing happened. So I checked the source code and saw that it was doing a getOption for label, so I tried this:

    add_filter('mailchimp_woocommerce_newsletter_field', 'rephrase_mcfield');
    function rephrase_mcfield ($options) {
    	//$checkbox, $status, $label
    	$options['label'] = 'This is a test';
    	return $options;
    }

    That didn’t work either. I figured it might expect only html returned from the function, so I tried this:

    add_filter('mailchimp_woocommerce_newsletter_field', 'rephrase_mcfield');
    function rephrase_mcfield () {
    	$checkbox = '<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="mailchimp_woocommerce_is_subscribed_checkbox" id="mailchimp_woocommerce_is_subscribed" checked="">This is a test change';
    	return $checkbox;
    }

    Nothing happened as a result of that code. So I’m at a loss as to how exactly that filter should be used. Is there any additional information on how the filter should be used?

    Basically my goal here is to change the label for the newsletter subscription checkbox. I don’t need to modify the HTML, just the label is fine.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter doncullen

    (@doncullen)

    After studying WordPress documentation, I’m understanding filters better and how they work. Based on this understanding, I now understand the function is expecting HTML since only the first parameter can be modified while the rest of the parameters are strictly informational. So with that information in mind, I re-coded it to this:

    add_filter('mailchimp_woocommerce_newsletter_field', 'rephrase_mcfield', 10, 3);
    function rephrase_mcfield ($checkbox_html, $status, $label) {
    	$updatedlabel = 'This is a test';
    	$checkbox_html = str_ireplace($label, $updatedlabel, $checkbox_html);
    	return $checkbox_html;
    }

    Basically it’ll take what the current HTML is, search for the current label provided by the function in the HTML, replace that current label with the updated label within the HTML, and then return the updated HTML back to the function.

    However nothing is happening with the label. So presumably this is not working as expected.

    As a temporary workaround, I’ve directly modified mailchimp-for-woocommerce/public/partials/mailchimp-woocommerce-my-account.php and updated the label, which works for now. However when the plugin is next updated, that change will be overwritten, so any feedback is appreciated.

    Plugin Author ryanhungate

    (@ryanhungate)

    @doncullen sorry to hear you’re having all that trouble. This file you’re talking about ( the mailchimp-woocommerce-my-account.php file) actually doesn’t have any filters / actions being used at the moment. The filters you’re looking at in the wiki documentation were related to the checkout and not the my account page.

    Thanks so much for bringing this up – we will put this on the next release for an upgrade.

    Plugin Support khungate

    (@khungate)

    Hi @doncullen, thank you for your patience. Our apologies, we updated our Wiki in a recent release and forgot to ping you back on your question. Here is some more detailed information that should help you: https://github.com/mailchimp/mc-woocommerce/wiki/Filter-on-newsletter-field

    Don’t hesitate to get back in touch if we need to reopen or answer any other questions.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to use mailchimp_woocommerce_newsletter_field?’ is closed to new replies.