在 WooCommrece 注册页面上添加时事通讯订阅复选框表单 [英] Add a newsletter subscription checkbox form on WooCommrece Register Page

查看:27
本文介绍了在 WooCommrece 注册页面上添加时事通讯订阅复选框表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SendGrid 插件,它允许创建一个时事通讯订阅表单,该表单将发送电子邮件通知订阅时事通讯(

2) 在我的帐户 > 帐户详细信息(部分):

<块引用>

现在你的插件已经过时了,而且 StackOverFlow 上的规则是当时的一个问题,你将不得不处理 Sendgrid Newsletter 集成(因为它无论如何都太宽泛了).>

I'm using SendGrid plugin which allows to create a Newsletter Subscription Form that will send email notification to subscribe to newsletter (https://sendgrid.com/docs/for-developers/sending-email/wordpress-subscription-widget/).

I want that when a new user sign-up they will have an option to subscribe on newsletter through checkbox on Register Page (http://prntscr.com/nmq8h2).

解决方案

The SendGrid plugin is not anymore maintained since a while (so it seems really outdated).

Updated: Now to add a checkbox for a news letter subscription in Woocommerce registration form (and on My account > Account details section), you will use the following:

// Remove "(optional)" label for this checkbox
add_filter( 'woocommerce_form_field' , 'remove_optional_custom_field_label', 10, 4 );
function remove_optional_custom_field_label( $field, $key, $args, $value ) {
    if( 'receive_newsletter' === $key && is_wc_endpoint_url( 'edit-account' ) ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}

// Display a custom checkbox in My Account > Account details
add_action( 'woocommerce_register_form', 'add_account_newsletter_checkbox_field' );
add_action( 'woocommerce_edit_account_form', 'add_account_newsletter_checkbox_field' );
function add_account_newsletter_checkbox_field() {
    woocommerce_form_field( 'receive_newsletter', array(
        'type'  => 'checkbox',
        'class' => array('form-row-wide'),
        'label' => __( 'Subscribe to our newsletter?', 'woocommerce' ),
        'clear' => true,
    ), get_user_meta(get_current_user_id(), 'receive_newsletter', true ) );

}


// Save registration checkbox field value
add_action( 'woocommerce_created_customer', 'save_account_registration_field' );
function save_account_registration_field( $customer_id ) {
    $value = isset( $_POST['receive_newsletter'] ) ? '1' : '0';
    update_user_meta( $customer_id, 'receive_newsletter', $value );

}

// Save checkbox field value for My Account > Account details
add_action( 'woocommerce_save_account_details', 'save_account_details_newsletter_checkbox_field', 10, 1 );
function save_account_details_newsletter_checkbox_field( $user_id ) {
    $value = isset( $_POST['receive_newsletter'] ) ? '1' : '0';
    update_user_meta( $user_id, 'receive_newsletter', $value );
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


1) On WooCommerce Registration form:

2) On My account > Account details (section):

Now as this your plugin is outdated and as the rule on StackOverFlow is one question at the time, you will have to handle the Sendgrid Newsletter integration (as it's anyway something too broad).

这篇关于在 WooCommrece 注册页面上添加时事通讯订阅复选框表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆