我一直在编辑Woocommerce上的结帐页面,并想要设置一个复选框,其本质是说“如果您同意接收以后的电子邮件,请选中该复选框”。
我缺乏编码经验,所以我很难将一些代码拼凑在一起以实现此目的。
理想情况下,我打算设置此复选框,以便选中该复选框时,客户输入的电子邮件地址将被收集并添加到MailChimp电子邮件列表中,但是,这对我来说甚至接近实现也很复杂(尽管如果有人可以给我一些有关如何实现此目标的指示,我不会抱怨。
但实际上,目前我一直想做的就是在我的functions.php中设置一些代码,以便如果有人在方框中打勾,我会收到一封电子邮件,表明有人已经同意,然后我可以手动将他们的电子邮件添加到Mailchimp。
尽管这不是我想要实现的理想解决方案,但我认为我会觉得它作为编码新手更加简单,但是...我仍在努力!
这是我目前所拥有的:
add_action( 'woocommerce_after_checkout_billing_form', 'add_content_after_billing', 15 );
function add_content_after_billing () {
echo '<input type="checkbox" name="emailconfirm" value="confirm" checked="checked"> Leave this box ticked if you give permission to be contacted via email in the future.';
}
if( $_POST ){
if ( (int)$_POST['emailconfirm'] == 1 ){
mail("*********@gmail.com","SubscribeAgreed","Email sub allowed");
}
}
您可能已经猜到了,这不起作用,我也没有收到电子邮件!
任何人都可以提供的帮助非常感谢。
答案 0 :(得分:1)
您可以尝试以下代码,该代码将在结帐页面和“我的帐户”上的“编辑帐单邮寄地址”上显示一个复选框。如果客户尚未订阅电子邮件,则该字段将显示在结帐中。如果客户订阅并下订单,则订阅电子邮件将发送到特定的电子邮件地址:
// Display a subscribing email checkbox in checkout and on My account "Edit billing address".
add_action('woocommerce_billing_fields', 'checkout_email_confirmation_checkbox', 20, 1);
function checkout_email_confirmation_checkbox ( $fields ) {
if( is_checkout() )
$label = __("I want and agree to receiving future emails");
else
$label = __("Email subscription");
$is_registered = get_user_meta( get_current_user_id(), 'billing_emailing', true );
// Avoid displaying the checkbox in checkout when user is already registered
if( is_checkout() && $is_registered )
return $fields; // Exit
$fields['billing_emailing'] = array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => $label,
'required' => false,
'clear' => true
);
return $fields;
}
// Send an email when a customer has subscribed
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 20, 2 );
function custom_checkout_field_update_order_meta( $order_id, $data ) {
// Get an instance of the WC_Order Object
$order = wc_get_order($order_id);
// get user data to check if user has been already registered
$billing_emailing = get_user_meta( $order->get_customer_id(), 'billing_emailing', true );
// Checking email registration and sending an email to a specific address
if ( $order->get_meta('_billing_emailing') && ! $billing_emailing ) {
// User name + email
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
$user_email = $order->get_billing_email();
$headers .= 'From: ' . $first_name . ' ' . $last_name . ' <' . $user_email . '>\r\n';
// HERE set the recipient email address
$to = 'name@email.com';
// Here the email subject
$subject = 'Email subscription';
// Here the email message
$message = 'First name: ' . $first_name . '\r\n';
$message = 'Last name: ' . $last_name . '\r\n';
$message = 'Email address: ' . $user_email . '\r\n';
// Send email:
wp_mail( $to, $subject, $message, $headers );
// Update user meta data (to avoid repetitions)
update_user_meta( $order->get_customer_id(), 'billing_emailing', true );
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
如果客户尚未订阅,则结帐页面中会显示复选框:
在“我的帐户”>“地址”>“编辑帐单地址”中显示复选框,允许客户退订。
现在此代码并不完美 (或不完整)。
但是有免费的Mailchimp插件,可以使集成和所有操作自动化。
例如,您可以使用MailChimp for WordPress插件,该插件还集成了Woocommerce。
您将可以在结帐页面(所有内容将自动执行)中设置邮件复选框。
答案 1 :(得分:0)
Woocommerce有一个插件,可让您有一个复选框,要求用户在您的结帐页面上进行订阅。
无需编码;-)
https://docs.woocommerce.com/document/newsletter-subscription/
“总有一种简单的方法”
迈克尔