我必须建立一个网站来销售课程注册,并且要做,所以我正在使用 WooCommerce。 单身人士和为其员工进行多次注册的公司都会报名参加课程,如果是单身人士,我希望能够填写标准 WooCommerce 结账等字段。否则,如果它是一家公司(我必须注册更多取决于同一课程)公司数据的标准字段和员工数据的字段,则根据产品中选择的数量重复。订单完成后,我需要查看订单页面和电子邮件中的所有数据。 我尝试了这个插件,它可以工作,但是我无法根据数量显示一组字段,您认为可以以某种方式添加此功能吗?否则,我发现了这个代码
function person_details( $checkout ) {
global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
$i = 0;
for( $k=1; $k<= $count; $k++ ) {
$i++;
print ('Dati iscritto n. '.$i.'');
woocommerce_form_field( 'cstm_full_name'.$i, array(
'type' => 'text',
'class' => array('my-field-class form-row-first'),
'label' => __('Nome e cognome'),
'placeholder' => __(''),
'required' => true,
), $checkout->get_value( 'cstm_full_name'.$i ));
woocommerce_form_field( 'cstm_email'.$i, array(
'type' => 'email',
'required' => true,
'class' => array( 'my-field-class form-row-last' ),
'label' => __( 'Email' ),
), $checkout->get_value( 'cstm_email'.$i ));
woocommerce_form_field( 'cstm_phone'.$i, array(
'type' => 'text',
'class' => array('my-field-class form-row-first'),
'label' => __('Numero di telefono'),
'placeholder' => __(''),
'required' => true,
), $checkout->get_value( 'cstm_phone'.$i ));
woocommerce_form_field( 'cstm_address'.$i, array(
'type' => 'textarea',
'class' => array('my-field-class form-row-last'),
'label' => __('Indirizzo di residenza'),
'placeholder' => __(''),
'required' => true,
), $checkout->get_value( 'cstm_address'.$i ));
echo '<div class="clear"></div>';
echo '<div class="clearbox"></div>';
}
}
add_action( 'woocommerce_before_order_notes', 'person_details' );
function customise_checkout_field_update_order_meta($order_id)
{
global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
$i = 0;
for($k=1; $k<= $count; $k++) {
$i++;
if (!empty($_POST['cstm_full_name'.$i])) {
update_post_meta($order_id, 'Nome e cognome'.$i, sanitize_text_field($_POST['cstm_full_name'.$i]));
}
if (!empty($_POST['cstm_email'.$i])) {
update_post_meta($order_id, 'email'.$i, sanitize_text_field($_POST['cstm_email'.$i]));
}
if (!empty($_POST['cstm_phone'.$i])) {
update_post_meta($order_id, 'Numero di telefono'.$i, sanitize_text_field($_POST['cstm_phone'.$i]));
}
if (!empty($_POST['cstm_address'.$i])) {
update_post_meta($order_id, 'Indirizzo di residenza'.$i, sanitize_text_field($_POST['cstm_address'.$i]));
}
/*if ( isset( $_POST['cstm_groep'.$i] )){
$value = sanitize_text_field( $_POST['cstm_groep'.$i] );
update_post_meta( $post->ID, 'cstm_groep'.$i, $value );
}*/
}
}
add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');
function add_email_custom_order_meta( $order, $sent_to_admin, $plain_text ){
$quantity = 0;
foreach ( $order->get_items() as $item_id => $item ) {
$quantity = $quantity + $item->get_quantity();
}
$order_id = $order->get_order_number();
echo "<ul>";
$i = 0;
for( $k=1; $k <= $quantity; $k++ ) {
$i++;
echo "<li>Nome e cognome: ".get_post_meta($order_id, 'Nome e cognome'.$i, true )."</li>";
echo "<li>Email: ".get_post_meta($order_id, 'Email'.$i, true )."</li>";
echo "<li>Numero di telefono: ".get_post_meta($order_id, 'Numero di telefono'.$i, true )."</li>";
echo "<li>Indirizzo di residenza: ".get_post_meta($order_id, 'Indirizzo di residenza'.$i, true )."</li>";
}
echo "</ul>";
}
add_action( 'woocommerce_email_order_meta', 'add_email_custom_order_meta', 10, 3 );
// display the extra data in the order admin panel
function display_order_custom_data_in_admin_order_overview_page( $order ){ ?>
<div class="order_data_column" style="width: 100% !important;">
<h4><?php _e( 'Your label' ); ?></h4>
<?php
$quantity = 0;
foreach ( $order->get_items() as $item_id => $item ) {
$quantity = $quantity + $item->get_quantity();
}
$order_id = $order->get_order_number();
echo "<ul>";
$i = 0;
for( $k=1; $k <= $quantity; $k++ ) {
$i++;
echo "<li>Nome e cognome: ".get_post_meta($order_id, 'Nome e cognome'.$i, true )."</li>";
echo "<li>Email: ".get_post_meta($order_id, 'Email'.$i, true )."</li>";
echo "<li>Numero di telefono: ".get_post_meta($order_id, 'Numero di telefono'.$i, true )."</li>";
echo "<li>Indirizzo di residenza: ".get_post_meta($order_id, 'Indirizzo di residenza'.$i, true )."</li>";
}
echo "</ul>";
?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'display_order_custom_data_in_admin_order_overview_page' );
这有效,它根据数量添加字段,但我看不到电子邮件和订单页面中的数据,而且我还想添加一些条件,例如如果您选择我是私人表演我是标准字段,否则如果您选择我是一家公司,它将显示一组仅填写一次的字段,以及更多组要填写的相同字段,数量与所选数量相同。
代码很旧,也许 WooCommerce 的钩子随着时间的推移而发生了变化,我试图查看,但我无法让它工作。你知道是否有更简单的方法来做到这一点,就像使用某些插件一样,或者你是否可以混合上面提到的一些插件和代码?
例如,使用插件的字段和条件,但通过代码选择根据数量显示哪组字段?
更新 这是 function.php 文件中的完整代码
<?php
function my_theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
/**
* Add the product's short description (excerpt) to the WooCommerce shop/category pages.
*/
function woocommerce_after_shop_loop_item_title_short_description() {
global $product;
if ( ! $product->get_short_description() ) return; ?>
<div itemprop="description">
<?php echo apply_filters( 'woocommerce_short_description', $product->get_short_description() ) ?>
</div>
<?php
}
add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_after_shop_loop_item_title_short_description', 5);
// Add "Add to Cart" buttons in Divi shop pages
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 20 );
// To change add to cart text on single product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
function woocommerce_custom_single_add_to_cart_text() {
return __( 'Iscriviti', 'woocommerce' );
}
// To change add to cart text on product archives(Collection) page
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );
function woocommerce_custom_product_add_to_cart_text() {
return __( 'Vedi', 'woocommerce' );
}
function category_single_product(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<p itemprop="name" class="card product_category_title"><span><?php echo $single_cat->name; ?></span></p>
<?php }
}
add_action( 'woocommerce_before_shop_loop_item_title', 'category_single_product', 25 );
// campi checkout ordine in base alla quantità
/**
* Display field value on the order edit page DA TOGLIERE
*/
/*add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('My Field').':</strong> ' . get_post_meta( $order->id, 'Full name', true ) . '</p>';
}*/
/* nuovo test custom field*/
add_action( 'woocommerce_before_order_notes', 'persons_details' );
function persons_details( $checkout ) {
$count = WC()->cart->get_cart_contents_count();
$i = 0;
for( $k=1; $k<= $count; $k++ ) {
$i++;
echo '<div><strong>'. __('Dati iscritto n. ') . $i . '</strong></div>';
woocommerce_form_field( 'cstm_full_name' . $i, array(
'type' => 'text',
'class' => array('my-field-class form-row-first'),
'label' => __("Nome e cognome"),
'placeholder' => __(""),
'required' => true,
), $checkout->get_value( 'cstm_full_name' . $i ));
woocommerce_form_field( 'cstm_email' . $i, array(
'type' => 'email',
'class' => array( 'my-field-class form-row-last' ),
'label' => __( "Email" ),
'placeholder' => __(""),
'required' => true,
), $checkout->get_value( 'cstm_email' . $i ));
woocommerce_form_field( 'cstm_phone' . $i, array(
'type' => 'text',
'class' => array('my-field-class form-row-first'),
'label' => __("Numero di telefono"),
'placeholder' => __(""),
'required' => true,
), $checkout->get_value( 'cstm_phone' . $i ));
woocommerce_form_field( 'cstm_address' . $i, array(
'type' => 'textarea',
'class' => array('my-field-class form-row-last'),
'label' => __("Indirizzo di residenza"),
'placeholder' => __(""),
'required' => true,
), $checkout->get_value( 'cstm_address' . $i ));
echo '<div class="clear"></div>
<div class="clearbox"></div>';
}
}
add_action( 'woocommerce_checkout_create_order', 'save_custom_checkout_field_order_meta' );
function save_custom_checkout_field_order_meta( $order )
{
$count = WC()->cart->get_cart_contents_count();
$order->update_meta_data( 'cstm_items_count', intval($count) ); // Save the cart contents count as meta data
$i = 0;
for($k=1; $k<= $count; $k++) {
$i++;
if ( isset($_POST['cstm_full_name'.$i]) && ! empty($_POST['cstm_full_name'.$i]) ) {
$order->update_meta_data( 'cstm_full_name'.$i, sanitize_text_field($_POST['cstm_full_name'.$i]) );
}
if ( isset($_POST['cstm_email'.$i]) && ! empty($_POST['cstm_email'.$i]) ) {
$order->update_meta_data( 'cstm_email'.$i, sanitize_text_field($_POST['cstm_email'.$i]) );
}
if ( isset($_POST['cstm_phone'.$i]) && ! empty($_POST['cstm_phone'.$i])) {
$order->update_meta_data( 'cstm_phone'.$i, sanitize_text_field($_POST['cstm_phone'.$i]) );
}
if ( isset($_POST['cstm_address'.$i]) && ! empty($_POST['cstm_address'.$i])) {
$order->update_meta_data( 'cstm_address'.$i, sanitize_text_field($_POST['cstm_address'.$i]) );
}
}
}
add_action( 'woocommerce_email_order_meta', 'add_email_custom_order_meta', 10, 3 );
function add_email_custom_order_meta( $order, $sent_to_admin, $plain_text ){
$quantity = $order->get_meta('cstm_items_count'); // Get items quantity count from meta data
echo '<ul>';
$i = 0;
for( $k=1; $k <= $quantity; $k++ ) {
$i++;
echo '<li><strong>'. __("Dati iscritto n. ") . $i . '<strong></li>
<li>' . __("Nome e cognome: ") . $order->get_meta('cstm_full_name'.$i) . '</li>
<li>' . __("Email: ") . $order->get_meta('cstm_email'.$i) . '</li>
<li>' . __("Numero di telefono: ") . $order->get_meta('cstm_phone'.$i) . '</li>
<li>' . __("Indirizzo di residenza: ") . $order->get_meta('cstm_address'.$i) . '</li>';
}
echo '</ul>';
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'display_custom_fields_in_admin_order_pages' );
function display_custom_fields_in_admin_order_pages( $order ){
$quantity = $order->get_meta('cstm_items_count'); // Get items quantity count from meta data
echo '<div class="order_data_column" style="width: 100% !important;">
<h4>' . __( 'Your label' ) . '</h4>
<ul>';
$i = 0;
for( $k=1; $k <= $quantity; $k++ ) {
$i++;
echo '<li><strong>'. __("Dati iscritto n. ") . $i . '<strong></li>
<li>' . __("Nome e cognome: ") . $order->get_meta('cstm_full_name'.$i) . '</li>
<li>' . __("Email: ") . $order->get_meta('cstm_email'.$i) . '</li>
<li>' . __("Numero di telefono: ") . $order->get_meta('cstm_phone'.$i) . '</li>
<li>' . __("Indirizzo di residenza: ") .$order->get_meta('cstm_address'.$i) . '</li>';
}
echo '</ul>
</div>';
}
这是您重新审视的工作代码,使用正确的语法和最后的 WooCommerce Hooks:
add_action( 'woocommerce_before_order_notes', 'persons_details' );
function persons_details( $checkout ) {
$count = WC()->cart->get_cart_contents_count();
$i = 0;
for( $k=1; $k<= $count; $k++ ) {
$i++;
echo '<div><strong>'. __('Dati iscritto n. ') . $i . '</strong></div>';
woocommerce_form_field( 'cstm_full_name' . $i, array(
'type' => 'text',
'class' => array('my-field-class form-row-first'),
'label' => __("Nome e cognome"),
'placeholder' => __(""),
'required' => true,
), $checkout->get_value( 'cstm_full_name' . $i ));
woocommerce_form_field( 'cstm_email' . $i, array(
'type' => 'email',
'class' => array( 'my-field-class form-row-last' ),
'label' => __( "Email" ),
'placeholder' => __(""),
'required' => true,
), $checkout->get_value( 'cstm_email' . $i ));
woocommerce_form_field( 'cstm_phone' . $i, array(
'type' => 'text',
'class' => array('my-field-class form-row-first'),
'label' => __("Numero di telefono"),
'placeholder' => __(""),
'required' => true,
), $checkout->get_value( 'cstm_phone' . $i ));
woocommerce_form_field( 'cstm_address' . $i, array(
'type' => 'textarea',
'class' => array('my-field-class form-row-last'),
'label' => __("Indirizzo di residenza"),
'placeholder' => __(""),
'required' => true,
), $checkout->get_value( 'cstm_address' . $i ));
echo '<div class="clear"></div>
<div class="clearbox"></div>';
}
}
add_action( 'woocommerce_checkout_create_order', 'save_custom_checkout_field_order_meta' );
function save_custom_checkout_field_order_meta( $order )
{
$count = WC()->cart->get_cart_contents_count();
$order->update_meta_data( 'cstm_items_count', intval($count) ); // Save the cart contents count as meta data
$i = 0;
for($k=1; $k<= $count; $k++) {
$i++;
if ( isset($_POST['cstm_full_name'.$i]) && ! empty($_POST['cstm_full_name'.$i]) ) {
$order->update_meta_data( 'cstm_full_name'.$i, sanitize_text_field($_POST['cstm_full_name'.$i]) );
}
if ( isset($_POST['cstm_email'.$i]) && ! empty($_POST['cstm_email'.$i]) ) {
$order->update_meta_data( 'cstm_email'.$i, sanitize_text_field($_POST['cstm_email'.$i]) );
}
if ( isset($_POST['cstm_phone'.$i]) && ! empty($_POST['cstm_phone'.$i])) {
$order->update_meta_data( 'cstm_phone'.$i, sanitize_text_field($_POST['cstm_phone'.$i]) );
}
if ( isset($_POST['cstm_address'.$i]) && ! empty($_POST['cstm_address'.$i])) {
$order->update_meta_data( 'cstm_address'.$i, sanitize_text_field($_POST['cstm_address'.$i]) );
}
}
}
add_action( 'woocommerce_email_order_meta', 'add_email_custom_order_meta', 10, 3 );
function add_email_custom_order_meta( $order, $sent_to_admin, $plain_text ){
$quantity = $order->get_meta('cstm_items_count'); // Get items quantity count from meta data
echo '<ul>';
$i = 0;
for( $k=1; $k <= $quantity; $k++ ) {
$i++;
echo '<li><strong>'. __("Dati iscritto n. ") . $i . '<strong></li>
<li>' . __("Nome e cognome: ") . $order->get_meta('cstm_full_name'.$i) . '</li>
<li>' . __("Email: ") . $order->get_meta('cstm_email'.$i) . '</li>
<li>' . __("Numero di telefono: ") . $order->get_meta('cstm_phone'.$i) . '</li>
<li>' . __("Indirizzo di residenza: ") . $order->get_meta('cstm_address'.$i) . '</li>';
}
echo '</ul>';
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'display_custom_fields_in_admin_order_pages' );
function display_custom_fields_in_admin_order_pages( $order ){
$quantity = $order->get_meta('cstm_items_count'); // Get items quantity count from meta data
echo '<div class="order_data_column" style="width: 100% !important;">
<h4>' . __( 'Your label' ) . '</h4>
<ul>';
$i = 0;
for( $k=1; $k <= $quantity; $k++ ) {
$i++;
echo '<li><strong>'. __("Dati iscritto n. ") . $i . '<strong></li>
<li>' . __("Nome e cognome: ") . $order->get_meta('cstm_full_name'.$i) . '</li>
<li>' . __("Email: ") . $order->get_meta('cstm_email'.$i) . '</li>
<li>' . __("Numero di telefono: ") . $order->get_meta('cstm_phone'.$i) . '</li>
<li>' . __("Indirizzo di residenza: ") .$order->get_meta('cstm_address'.$i) . '</li>';
}
echo '</ul>
</div>';
}
代码位于活动子主题(或活动主题)的 function.php 文件中。已测试并有效。
在管理单一订单页面上:
关于电子邮件通知:
在数据库中,wp_postmeta 表下的订单 ID(帖子 ID):
如果您使用 Divi 主题并使用 Divi 主题生成器创建结帐页面,则此代码将不会保存您的自定义字段数据。所以我建议您不要将其用于结账页面,而使用标准 woocomerce 页面中已有的短代码
这段代码对我帮助很大,所以我真的要感谢你!!
我还想问是否可以将表单字段设为必填,所以如果您没有填写这些字段,您基本上无法结束订单
谢谢!!