前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >woocommerce在checkout页面自定义输入内容

woocommerce在checkout页面自定义输入内容

作者头像
IT不难
发布2024-05-16 17:02:15
830
发布2024-05-16 17:02:15
举报
文章被收录于专栏:IT不难技术家园IT不难技术家园

前沿

对接第三方支付的过程中,有几个字段需要客户端上传。需要在checkout页面让客户输入然后提交。

自定义输入
自定义输入

代码

上述样式的自定义代码

代码语言:javascript
复制
// 添加自定义字段到结账页面
add_action('woocommerce_after_order_notes', 'custom_checkout_fields');

function custom_checkout_fields($checkout)
{
    echo '' . __('Custom Fields') . '';

    woocommerce_form_field('cardNo', array(
        'type' => 'text',
        'class' => array('form-row-wide'),
        'label' => __('Card Number'),
        'required' => true
    ), $checkout->get_value('cardNo'));

    woocommerce_form_field('cardExpireMonth', array(
        'type' => 'text',
        'class' => array('form-row-first'),
        'label' => __('Card Expiry Month'),
        'required' => true
    ), $checkout->get_value('cardExpireMonth'));

    woocommerce_form_field('cardExpireYear', array(
        'type' => 'text',
        'class' => array('form-row-last'),
        'label' => __('Card Expiry Year'),
        'required' => true
    ), $checkout->get_value('cardExpireYear'));

    woocommerce_form_field('cardSecurityCode', array(
        'type' => 'text',
        'class' => array('form-row-wide'),
        'label' => __('Security Code'),
        'required' => true
    ), $checkout->get_value('cardSecurityCode'));

    woocommerce_form_field('cardType', array(
        'type' => 'select',
        'class' => array('form-row-wide'),
        'label' => __('Card Type'),
        'options' => array(
            'visa' => __('VISA'),
            'mastercard' => __('MASTERCARD'),
            'ae' => __('AE')
        ),
        'required' => true
    ), $checkout->get_value('cardType'));

    echo '';
}

// 验证并保存输入的值
add_action('woocommerce_checkout_process', 'custom_checkout_fields_validation');

function custom_checkout_fields_validation()
{
    if (empty($_POST['cardNo'])) {
        wc_add_notice(__('Card Number is a required field.'), 'error');
    }

    if (empty($_POST['cardExpireMonth'])) {
        wc_add_notice(__('Card Expiry Month is a required field.'), 'error');
    }

    if (empty($_POST['cardExpireYear'])) {
        wc_add_notice(__('Card Expiry Year is a required field.'), 'error');
    }

    if (empty($_POST['cardSecurityCode'])) {
        wc_add_notice(__('Security Code is a required field.'), 'error');
    }

    if (empty($_POST['cardType']) || $_POST['cardType'] == '') {
        wc_add_notice(__('Card Type is a required field.'), 'error');
    }
}

// 保存输入的值
add_action('woocommerce_checkout_update_order_meta', 'save_custom_checkout_fields');

function save_custom_checkout_fields($order_id)
{
    if (!empty($_POST['cardNo'])) {
        update_post_meta($order_id, 'Card Number', sanitize_text_field($_POST['cardNo']));
    }
    if (!empty($_POST['cardExpireMonth'])) {
        update_post_meta($order_id, 'Card Expiry Month', sanitize_text_field($_POST['cardExpireMonth']));
    }
    if (!empty($_POST['cardExpireYear'])) {
        update_post_meta($order_id, 'Card Expiry Year', sanitize_text_field($_POST['cardExpireYear']));
    }
    if (!empty($_POST['cardSecurityCode'])) {
        update_post_meta($order_id, 'Security Code', sanitize_text_field($_POST['cardSecurityCode']));
    }
    if (!empty($_POST['cardType'])) {
        update_post_meta($order_id, 'Card Type', wc_clean($_POST['cardType']));
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024年05月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前沿
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档