我正try 在WooCommerce checkout 的顶部放置两个自定义域(理想情况下是购物车页面,但我在网上找不到任何关于这一可能性的内容)

我有两个自定义字段出现在 checkout 页面上,但由于php知识非常有限,我试图创建一个下拉菜单等.表面上看起来一切都很好.这些字段显示在 checkout 页面上,但每当我输入信息并try 继续进行订单以测试这些值是否显示在感谢页面和邮箱上时,系统会要求我在这些必填字段中填写信息.出于某种原因,页面无法识别这些字段为空

我用的是这个post...https://www.businessbloomer.com/woocommerce-add-custom-checkout-field-php/

但我不得不对它进行了相当多的编辑,才走到了这一步.我相信这里面肯定有很多错误.

add_action('woocommerce_before_checkout_form', 'custom_add_custom_checkout_fields');

function custom_add_custom_checkout_fields($checkout)
{
    echo '<h3>Event Information</h3>';

    // Get the current user ID
    $user_id = get_current_user_id();

    // Fetch user meta data
    $saved_event_venue = $current_user->event_venue;
    $saved_hall_stand_number = $current_user->hall_stand_number;

    woocommerce_form_field('event_venue', array(
        'type' => 'select',
        'class' => array('form-row-wide'),
        'label' => 'Event/Expo & Venue',
        'options' => array(
            '' => 'Select an option',
            'event1' => 'Event 1 Venue',
            'event2' => 'Event 2 Venue',
            'event3' => 'Event 3 Venue'
            // Add more options as needed
        ),
        'required' => true,
        'default' => $saved_event_venue,
    ), $checkout->get_value('event_venue'));

    woocommerce_form_field('hall_stand_number', array(
        'type' => 'text',
        'class' => array('form-row-wide'),
        'label' => 'Hall & Stand Number',
        'placeholder' => 'Enter Hall & Stand Number',
        'required' => true,
        'default' => $saved_hall_stand_number,
    ), $checkout->get_value('hall_stand_number'));
}

// Validate custom fields
add_action('woocommerce_checkout_process', 'custom_validate_checkout_fields');

function custom_validate_checkout_fields()
{
    if (!$_POST['event_venue']) {
        wc_add_notice('Please select an Event/Expo & Venue.', 'error');

        if (!$_POST['hall_stand_number']) {
            wc_add_notice('Please enter the Hall & Stand Number.', 'error');
        }
    }
}

// Save custom fields to order
add_action('woocommerce_checkout_update_order_meta', 'custom_save_checkout_fields');

function custom_save_checkout_fields($order_id)
{
    if ($_POST['event_venue'])
        update_post_meta($order_id, '_event_venue', esc_attr($_POST['event_venue']));


    if ($_POST['hall_stand_number'])
        update_post_meta($order_id, '_hall_stand_number', esc_attr($_POST['hall_stand_number']));
}

// Display custom fields on order received page and order emails
add_action('woocommerce_thankyou', 'custom_new_checkout_field_thankyou');

function custom_new_checkout_field_thankyou($order_id)
{
    if (get_post_meta($order_id, 'event_venue', true))
        echo '<p><strong>Event/Expo &Venue:</strong> ' . get_post_meta($order_id, '_Event_venue', true) . '</p>';

    if (get_post_meta($order_id, 'hall_stand_number ', true))
        echo '<p><strong>Hall - Stand Number:</strong> ' . get_post_meta($order_id, '_hall_stand_number', true) . '</p>';
}

add_action('woocommerce_admin_order_data_after_billing_address', 'bbloomer_show_new_checkout_field_order');

function custom_new_checkout_field_order($order)
{
    $order_id = $order->get_id();

    if (get_post_meta($order_id, '_event_venue', true))
        echo '<p><strong>Event/Expo - Venue:</strong> ' . get_post_meta($order_id, '_event_venue', true) . '</p>';

    if (get_post_meta($order_id, '_hall_stand_number', true))
        echo '<p><strong>Hall - Stand Number:</strong> ' . get_post_meta($order_id, '_hall_stand_number', true) . '</p>';
}



add_action('woocommerce_email_after_order_table', 'custom_new_checkout_field_emails', 20, 4);

function bbloomer_show_new_checkout_field_emails($order, $sent_to_admin, $plain_text, $email ) {
    if (get_post_meta($order->get_id(), '_license_no', true))
        echo '<p><strong>Event/Expo - Venue:   </strong> ' . get_post_meta($order->get_id(), '_event_venue', true) . '</p>';

    if (get_post_meta($order->get_id(), '_hall_stand_number', true))
        echo '<p><strong>Hall - Stand Number:</strong> ' . get_post_meta($order->get_id(), '_hall_stand_number', true) . '</p>';
}

推荐答案

Updated

主要错误是您的第一个函数中没有定义$current_user变量.此外,您不能使用woocommerce_before_checkout_form钩子,因为它将在 checkout 表单之外显示您的自定义 checkout 字段,并且它们将不起作用.

还有其他一些错误和遗漏的东西.请try 以下操作:

// Utility function: Get Event venue select field options
function get_event_venue_options() {
    // Add more options as needed to the array
    return array(
        'event1'    => __('Event 1 Venue', 'woocommerce'),
        'event2'    => __('Event 2 Venue', 'woocommerce'),
        'event3'    => __('Event 3 Venue', 'woocommerce'),
    );
}

// Utility function: Get customer selected Event venue data (for display)
function get_customer_event_venue_data( $order ) {
    $options = get_event_venue_options(); // Get event venue options
    $event_data  = []; // Initializing

    if ( $event_venue = $order->get_meta('_event_venue') ) {
        $event_data[__('Event/Expo & Venue', 'woocommerce')] = $options[$event_venue];
    }

    if ( $hall_stand_number = $order->get_meta('_hall_stand_number') ) {
        $event_data[__('Hall - Stand Number', 'woocommerce')] = $hall_stand_number;
    }
    return $event_data;
}

// Utility function: Save customer selected Event venue data 
function save_customer_event_venue_data( $object, $type = 'order' ) {
    $prefix = $type === 'order' ? '_' : '';

    if ( isset($_POST['event_venue']) ) {
        $object->update_meta_data($prefix.'event_venue', esc_attr($_POST['event_venue']));
    }
    if ( isset($_POST['hall_stand_number']) ) {
        $object->update_meta_data($prefix.'hall_stand_number', sanitize_text_field($_POST['hall_stand_number']));
    }
}

// Display the fields in checkout before order notes
add_action( 'woocommerce_before_order_notes', 'display_custom_checkout_fields', 10, 1 );
function display_custom_checkout_fields( $checkout ) {
    $event_venue_options = get_event_venue_options();

    echo '<div class="event-info">
    <h3>' . __('Event Information', 'woocommerce') . '</h3>';

    woocommerce_form_field('event_venue', array(
        'type' => 'select',
        'class' => array('form-row-wide'),
        'label' => __('Event/Expo & Venue', 'woocommerce'),
        'options' => array_merge( ['' => __('Select an option', 'woocommerce')], $event_venue_options ),
        'required' => true,
    ), WC()->customer->get_meta('event_venue') );

    woocommerce_form_field('hall_stand_number', array(
        'type' => 'text',
        'class' => array('form-row-wide'),
        'label' => __('Hall & Stand Number', 'woocommerce'),
        'placeholder' => __('Enter Hall & Stand Number', 'woocommerce'),
        'required' => true,
    ), WC()->customer->get_meta('hall_stand_number') );

    echo '</div>';
}

// Validate custom fields
add_action( 'woocommerce_checkout_process', 'validate_custom_checkout_fields' );
function validate_custom_checkout_fields() {
    if ( isset($_POST['event_venue']) && empty($_POST['event_venue']) ) {
        wc_add_notice('Please select an Event/Expo & Venue.', 'error');
    }
    if ( isset($_POST['hall_stand_number']) && empty($_POST['hall_stand_number']) ) {
        wc_add_notice('Please enter the Hall & Stand Number.', 'error');
    }
}

// Save customer event venue data as order metadata and user meta data
add_action( 'woocommerce_checkout_update_customer', 'save_event_venue_data_as_metadata', 10, 1 );
add_action( 'woocommerce_checkout_create_order', 'save_event_venue_data_as_metadata', 10, 1 );
function save_event_venue_data_as_metadata( $object ) {
    if ( is_a($object, 'WC_Order') ) {
        save_customer_event_venue_data( $object );
    } else {
        save_customer_event_venue_data( $object, 'customer' );
    }
}

// Display Event data on orders, admin orders and email notifications
add_action('woocommerce_after_order_details', 'display_event_data_on_orders', 10, 1);
add_action('woocommerce_admin_order_data_after_shipping_address', 'display_event_data_on_orders', 10, 1 );
function display_event_data_on_orders( $order ) {
    $event_data  = get_customer_event_venue_data( $order );
    $event_title = __('Event details', 'woocommerce');
    if ( ! empty($event_data) ) {
        // On admin orders
        if( is_admin() ) {
            echo '<h3>'.$event_title.'</h3><p>';
            foreach ( $event_data as $label => $value ) {
                echo '<strong>' . $label .':</strong> ' . $value  . '<br>';
            }
            echo '</p>';
        } 
        // On customer orders and thankyou
        else {
            echo '<h2 class="woocommerce-column__title">'.$event_title.'</h2>
            <table class="woocommerce-table woocommerce-table--event-details shop_table event_details"><tfoot>';
            foreach ( $event_data as $label => $value ) {
                echo '<tr class="event-venue"><th>'.$label.'</th><td>'.$value.'</td></tr>';
            }
            echo '</tfoot></table>';
        } 
    }
}

// Display Event data on email notifications
add_action('woocommerce_email_after_order_table', 'display_event_data_on_email_notifications', 10, 1);
function display_event_data_on_email_notifications( $order ) {
    $event_data  = get_customer_event_venue_data( $order );
    $event_title = __('Event details', 'woocommerce');
    if ( ! empty($event_data) ) {
        echo '<style>
        .event-info table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
            color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
        .event-info table th, table.tracking-info td{text-align: left; border-top-width: 4px;
            color: #737373; border: 1px solid #e4e4e4; padding: 12px; width:58%;}
        .event-info table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
        </style>';

        echo '<div class="event-info"><h2>'.$event_title.'</h2>';
        echo '<table cellspacing="0" cellpadding="6"><tbody>';
        foreach ( $event_data as $label => $value ) {
            echo '<tr"><th>'.$label.'</th><td>'.$value.'</td></tr>';
        }
        echo '</tbody></table></div><br>';
    }
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.


You will get (screenshots):

  • 在 checkout 页面上:

enter image description here


  • 在客户订单和感谢页面上:

enter image description here


  • 在管理订单上:

enter image description here


  • 关于邮箱通知:

enter image description here


相关:Add custom meta data into emails as a html styled table with a title in Woocommerce

Php相关问答推荐

Laravel迁移返回SQL错误,但SQL有效且工作正常

在Woocommerce变量产品中的标签附近显示所选属性选项名称

使用PHP阅读XML提要

从WooCommerce购物车和 checkout 页面自定义发货标签

允许在WooCommerce管理员优惠券列表中显示自定义优惠券类型

如何在php中生成包含第100秒的时间序列?

WooCommerce常规价格增加额外的附加平价

Apache只允许index.php-不从根目录工作

使用MySQLi使用连接字符串设置字符集

如何在execute语句中使用存储过程参数?

在Laravel中创建辅助函数时的约定

Font Awesome 加载图标未显示在 WooCommerce 管理员列表中

如何在 Woocommerce 中自动删除旧的已完成订单

WooCommerce 按产品运输插件:仅收取较高的运输费用

计算添加到购物车的点击次数并将其显示在 WooCommerce 管理产品列表中

防止使用woocommerce_checkout_process下单

如果有很多重定向,PHP curl 的行为不像命令行 curl

是否有适用于 Windows 的真正 64 位版本的 PHP 5.6?

为什么非贪婪匹配会消耗整个模式,即使后面跟着另一个非贪婪匹配

如何在php中获取大于50的年龄