如果字段为空则预填充 WooCommerce 结帐字段

问题描述 投票:0回答:1

我在 WooCommerce 结帐页面中遇到以下问题:

我们有2种付款方式,货到付款和网上信用卡。

支付处理商需要客户提供电子邮件地址,这是强制性的。

由于将电子邮件字段设为必填字段,转化率将会大幅下降。

因此,我希望“电子邮件”字段的工作方式类似于,如果客户没有输入他的电子邮件地址,那么我们应该使用标准电子邮件(例如“[电子邮件受保护]”自动填充该字段或发送到付款处理方的数据)

如果客户输入他的电子邮件地址,那么一切都会顺利完成。

谢谢

尝试了我在 stackoverflow 上找到的一些东西,但我在任何地方都看不到这个确切的问题..

woocommerce field checkout
1个回答
0
投票

相反,有一种更有效的方法:下订单时,在创建订单时,付款之前,如果帐单电子邮件为空且所选付款方式不是 COD,我们会设置标准电子邮件地址替换作为帐单电子邮件。
然后,付款后,我们会删除此电子邮件替换。

代码:

// Add a replacement email to the order, just before the payment
add_action( 'woocommerce_checkout_create_order', 'auto_fill_empty_billing_email', 20, 2 );
function auto_fill_empty_billing_email( $order, $data ) {
    // If email is empty and if the payment method is not COD
    if( ! $order->get_billing_email() && $order->get_payment_method() !== 'cod' ) {
        // Below Define the billing email replacement
        $replacement_email = '[email protected]';
        // Set the replacement billing email
        $order->set_billing_email( sanitize_email($replacement_email));
    }
}

// Remove the replacement email just after the payment
add_action( 'woocommerce_pre_payment_complete', 'remove_replacement_email'  );
function remove_replacement_email( $order_id ) {
    // Below Define the billing email replacement
    $replacement_email = '[email protected]';
    // Get the order object
    $order = wc_get_order($order_id);
    // If the billing email is the replacement email
    if( $order->get_billing_email() === $replacement_email ) {
        // Remove the replacement email
        $order->set_billing_email('');
        $order->save();
    }
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.