将客户添加到MailChimp、重定向到结账和填写表单数据的注册表单

时间:2020-12-12 作者:Neal Jones

你可以看到我在做什么this test page - 顶部按钮标记为;MAILCHIMP+结帐”;这就是我正在研究的。

我正在努力完成3件事(目前2和3正在我的测试页面上工作):

将表单数据发送到MailChimp重定向到签出自动用表单数据填充签出字段我可以使用插件制作一个表单,将数据发送到MailChimp,效果很好,但是当我添加代码时;3 MailChimp插件失败。我使用的插件是MailChimp for WordPress 我用它制作的表格结构如下:

<p>
    <label>First Name</label>
    <input type="text" name="FNAME" placeholder="First Name"
    required="">
</p>
<p>
    <label>Last Name</label>
    <input type="text" name="LNAME" placeholder="Last Name"
    required="">
</p>
<p>
    <label>Phone Number</label>
    <input type="tel" name="PHONE" placeholder="Phone" required="">
</p>
<p>
    <label>Email address: 
        <input type="email" name="EMAIL" placeholder="Email" required />
</label>
</p>
<p>
    <input type="submit" value="Give me 2 Weeks FREE" onclick=""/>
</p>
我用来完成2&;的代码;3在这里:

<script>
    var regForm1 = jQuery(\'#mc4wp-form-1\'),
         regBtn1 = jQuery(\'#mc4wp-form-1 input[type=submit]\');

    regBtn1.on(\'click touch\', function(e1) {
        e1.preventDefault();

        billing_first_name = regForm1.find(\'.mc4wp-form-fields > p:nth-child(1) > input:nth-child(2)\').val()
         billing_last_name = regForm1.find(\'.mc4wp-form-fields > p:nth-child(2) > input:nth-child(2)\').val()
                     billing_phone = regForm1.find(\'.mc4wp-form-fields > p:nth-child(3) > input:nth-child(2)\').val()           
             billing_email = regForm1.find(\'.mc4wp-form-fields > p:nth-child(4) > label:nth-child(1) > input:nth-child(1)\').val()
                
        // Add Cookies
         if (billing_first_name && billing_last_name && billing_phone && billing_email) {

              alert(billing_first_name + " - " + billing_last_name + " - " + billing_phone + " - " + billing_email);

            Cookies.set("billing_first_name", billing_first_name, {
               path: \'/\',
               expiration: 30
            })
            Cookies.set("billing_last_name", billing_last_name, {
               path: \'/\',
               expiration: 30
            })
            Cookies.set("billing_phone", billing_phone, {
                path: \'/\',
                expiration: 30
            })
            Cookies.set("billing_email", billing_email, {
                path: \'/\',
                expiration: 30
            })
            
            var url = `?add-to-cart=18074`;
            alert(url);
            window.location = url; 
            } else {
                alert("Please fill in all fields");
            }
        });
    // Fill in the cookies and immediately remove
    (function($) {
        $(document).ready(function() {
            const billing_first_name = Cookies.get("billing_first_name");
            const billing_last_name = Cookies.get("billing_last_name");
            const billing_email = Cookies.get("billing_email");
            const billing_phone = Cookies.get("billing_phone");

        if (billing_first_name) {
            $("#billing_first_name").val(billing_first_name)
            Cookies.remove("billing_first_name")
        }
        if (billing_last_name) {
            $("#billing_last_name").val(billing_last_name)
            Cookies.remove("billing_last_name")
        }
        if (billing_phone) {
            $("#billing_phone").val(billing_phone)
            Cookies.remove("billing_phone")
        }
        if (billing_email) {
            $("#billing_email").val(billing_email)
            Cookies.remove("billing_email")
        }
    })
    })(jQuery)
    
</script>
有什么想法可以让这一切顺利进行吗?我需要让一些东西等待一些东西,还是需要一种全新的方法?

1 个回复
最合适的回答,由SO网友:Neal Jones 整理而成

我得到了这方面的帮助,修复涉及函数中的两个函数。php和一个插件。

功能:

// Add to Cart for optin Form

add_action( \'template_redirect\', \'website_add_to_cart_on_custom_page\');
 
function website_add_to_cart_on_custom_page(){
 
    if( is_page( \'homepagee\' ) ) { // is a page slug
 
        WC()->cart->add_to_cart( 18074 ); // add to cart product with ID
 
    }
 
}


// Autofill checkout fields from URL
 
add_filter( \'woocommerce_checkout_fields\' , \'website_prefill_checkout_fields\');
  
function website_prefill_checkout_fields ( $checkout_fields ) {
  
    /**
     * Query string fields to populate in checkout,
     *
     * Ex.
     * \'fname\' => is query parameter name,
     * \'billing_first_name\' => matching field of checkout 
     *
     * You can add other fields in the same way
     */
    $query_fields = array(
         
        \'fname\' => \'billing_first_name\',
        \'lname\' => \'billing_last_name\',
        \'email\' => \'billing_email\',
        \'phone\' => \'billing_phone\',
    );
 
    // We will loop the above array to check if the field exists in URL or not.
    // If it exists we will add it to the checkout field\'s default value
 
    foreach ( $query_fields as $field_slug => $match_field ) {
         
        // Check if it is in URL or not, An not empty.
        if ( isset( $_GET[ $field_slug ] ) && ! empty( $_GET[ $field_slug ] ) ) {
 
            // Sanitize the value and store it in a variable.
            $field_value = sanitize_text_field( $_GET[ $field_slug ] );
 
            // Check if match field exists in checkout form or not.
            if( isset( $checkout_fields[\'billing\'][ $match_field ] ) ){
 
                // Assign the pre-fill value to checkout field
                $checkout_fields[\'billing\'][ $match_field ][\'default\'] = $field_value;
            }
        }       
    }
 
 
    // Return the fields
    return $checkout_fields;
}

add_action( \'woocommerce_before_checkout_form\', \'woocommerce_checkout_login_form\', 10 );
我们使用的插件是https://wordpress.org/plugins/mailchimp-for-wp/ 并在表格下>;表单行为在本节中,我们为“是”选择了“是”;成功注册后是否隐藏表单"E;然后在;成功注册后重定向到URL“;领域

在该领域,我们遵循以下格式:

https://domain.com/checkout/?fname={data key="FNAME"}&lname={data key="LNAME"}&email={data key="EMAIL"}&phone={data key="PHONE"} 
希望这对以后的人有所帮助。

相关推荐

在第三方API响应后发送Gravit Forms通知

我有一个重力表单,在提交时将数据发送给第三方API。它与以下代码连接并通过API保存数据。然而,我现在正试图弄清楚,只有当API响应返回为失败或状态代码201以外的任何内容时,才如何发送管理员通知电子邮件。add_action( \'gform_after_submission_1\', \'post_to_third_party\', 10, 2 ); function post_to_third_party( $entry, $form ) { $post_url =