基于所选产品数量的附加字段

huangapple go评论62阅读模式
英文:

Additional fields based on the quantity of products selected

问题

我必须创建一个网站来销售课程报名,并且为此我正在使用WooCommerce。无论是单个人还是公司,他们都将报名参加课程,我希望如果是单个人,可以填写像标准WooCommerce结账那样的字段。否则,如果是一家公司(我必须为同一课程注册更多的依赖项),则显示公司数据的标准字段和基于所选产品中选择的数量重复的员工数据字段。完成订单后,我需要在订单页面和电子邮件中看到所有数据。

我尝试了这个插件,它可以工作,但我无法根据数量显示一组字段,您认为是否可以以某种方式添加此功能?此外,我找到了这个代码

这段代码可以工作,它根据数量添加字段,但我无法在电子邮件和订单页面中看到数据,并且我希望能够添加一些条件,例如,如果选择“我是个人”,则显示标准字段,否则如果选择“我是公司”,则显示要填写的一组字段,只显示一次,以及其他相同字段的多个组,数量与选择的数量相同。

代码可能有点老,也许WooCommerce的挂钩随着时间的推移已经改变,我尝试过查找,但无法使其工作。您是否知道是否有更简单的方法,比如使用某些插件,或者是否可以将上述插件和代码混合使用?

例如,使用插件的字段和条件,但根据数量选择要显示的字段组?
1: https://it.wordpress.org/plugins/woo-checkout-field-editor-pro/
2: https://wasimsama.com/woocommerce-dynamic-custom-field-on-checkout-based-on-product-quantity-in-cart/

英文:

I have to make a site to sell course enrollments, and to do, so I'm using WooCommerce.
Both single people and companies who will make multiple registrations for their employees will enroll in the courses, I would like to be able to have fields like the standard WooCommerce checkout filled in if it is a single person. Otherwise, if it is a company (which I have to register more depends for the same course) standard fields for company data and fields for employee data that are repeated based on the quantity selected in the product. Once the order is completed, I need to see all the data on the order page and in the emails.
I tried this plug-in, it works, but I can't show a group of fields based on quantity, do you think this feature can be added somehow? Otherwise, I found this code

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' );

This works, it adds the fields based on the quantity, but I can't see the data in the email and in the order page, and also I would like to put some conditions like if you select I'm a private person show i standard fields otherwise if you select I am a company it will show a group of fields to be filled in only once and more other groups of equal fields to be filled in, as many as the selected quantity.

The code is old maybe the hooks of WooCommerce have changed over time, I tried to look, but I could not make it work. do you know if there is an easier way to do it like with some plugin or if you can mix some of the plugin mentioned above and the code?

For example, using the fields and conditions of the plugin but choosing with the code which group of fields to show based on the quantity?

update
this is the complete code present in the function.php file

<?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>';
}

答案1

得分: 2

这是您修订后的工作代码,使用了正确的语法和最新的WooCommerce挂钩:

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)); // 保存购物车内容计数为元数据

    $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'); // 从元数据中获取商品数量计数

    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'); // 从元数据中获取商品数量计数

    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>';
}

代码放入您活动的子主题(或活动主题)的functions.php文件中。已测试并可正常工作。

在管理员单个订单页面上:

基于所选产品数量的附加字段

在电子邮件通知中:

基于所选产品数量的附加字段

在数据库中,在订单ID(帖子ID)的wp_postmeta表下:

基于所选产品数量的附加字段

英文:

Here is your revisited working code, using the correct syntax and last WooCommerce Hooks:

add_action( &#39;woocommerce_before_order_notes&#39;, &#39;persons_details&#39; );
function persons_details( $checkout ) {
$count = WC()-&gt;cart-&gt;get_cart_contents_count();
$i = 0;
for( $k=1; $k&lt;= $count; $k++ ) {
$i++;
echo &#39;&lt;div&gt;&lt;strong&gt;&#39;. __(&#39;Dati iscritto n. &#39;) . $i . &#39;&lt;/strong&gt;&lt;/div&gt;&#39;;
woocommerce_form_field( &#39;cstm_full_name&#39; . $i, array(
&#39;type&#39;          =&gt; &#39;text&#39;,
&#39;class&#39;         =&gt; array(&#39;my-field-class form-row-first&#39;),
&#39;label&#39;         =&gt; __(&quot;Nome e cognome&quot;),
&#39;placeholder&#39;   =&gt; __(&quot;&quot;),
&#39;required&#39;      =&gt; true,
), $checkout-&gt;get_value( &#39;cstm_full_name&#39; . $i ));
woocommerce_form_field( &#39;cstm_email&#39; . $i, array(
&#39;type&#39;          =&gt; &#39;email&#39;,
&#39;class&#39;         =&gt; array( &#39;my-field-class form-row-last&#39; ),
&#39;label&#39;         =&gt; __( &quot;Email&quot; ),
&#39;placeholder&#39;   =&gt; __(&quot;&quot;),
&#39;required&#39;      =&gt; true,
), $checkout-&gt;get_value( &#39;cstm_email&#39; . $i ));
woocommerce_form_field( &#39;cstm_phone&#39; . $i, array(
&#39;type&#39;          =&gt; &#39;text&#39;,
&#39;class&#39;         =&gt; array(&#39;my-field-class form-row-first&#39;),
&#39;label&#39;         =&gt; __(&quot;Numero di telefono&quot;),
&#39;placeholder&#39;   =&gt; __(&quot;&quot;),
&#39;required&#39;      =&gt; true,
), $checkout-&gt;get_value( &#39;cstm_phone&#39; . $i ));
woocommerce_form_field( &#39;cstm_address&#39; . $i, array(
&#39;type&#39;          =&gt; &#39;textarea&#39;,
&#39;class&#39;         =&gt; array(&#39;my-field-class form-row-last&#39;),
&#39;label&#39;         =&gt; __(&quot;Indirizzo di residenza&quot;),
&#39;placeholder&#39;   =&gt; __(&quot;&quot;),
&#39;required&#39;      =&gt; true,
), $checkout-&gt;get_value( &#39;cstm_address&#39; . $i ));
echo &#39;&lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;clearbox&quot;&gt;&lt;/div&gt;&#39;;
}
}
add_action( &#39;woocommerce_checkout_create_order&#39;, &#39;save_custom_checkout_field_order_meta&#39; );
function save_custom_checkout_field_order_meta( $order )
{
$count = WC()-&gt;cart-&gt;get_cart_contents_count();
$order-&gt;update_meta_data( &#39;cstm_items_count&#39;, intval($count) ); // Save the cart contents count as meta data
$i = 0;
for($k=1; $k&lt;= $count; $k++) {
$i++;
if ( isset($_POST[&#39;cstm_full_name&#39;.$i]) &amp;&amp; ! empty($_POST[&#39;cstm_full_name&#39;.$i]) ) {
$order-&gt;update_meta_data( &#39;cstm_full_name&#39;.$i, sanitize_text_field($_POST[&#39;cstm_full_name&#39;.$i]) );
}
if ( isset($_POST[&#39;cstm_email&#39;.$i]) &amp;&amp; ! empty($_POST[&#39;cstm_email&#39;.$i]) ) {
$order-&gt;update_meta_data( &#39;cstm_email&#39;.$i, sanitize_text_field($_POST[&#39;cstm_email&#39;.$i]) );
}
if ( isset($_POST[&#39;cstm_phone&#39;.$i]) &amp;&amp; ! empty($_POST[&#39;cstm_phone&#39;.$i])) {
$order-&gt;update_meta_data( &#39;cstm_phone&#39;.$i, sanitize_text_field($_POST[&#39;cstm_phone&#39;.$i]) );
}
if ( isset($_POST[&#39;cstm_address&#39;.$i]) &amp;&amp; ! empty($_POST[&#39;cstm_address&#39;.$i])) {
$order-&gt;update_meta_data( &#39;cstm_address&#39;.$i, sanitize_text_field($_POST[&#39;cstm_address&#39;.$i]) );
}
}
}
add_action( &#39;woocommerce_email_order_meta&#39;, &#39;add_email_custom_order_meta&#39;, 10, 3 );
function add_email_custom_order_meta( $order, $sent_to_admin, $plain_text ){
$quantity = $order-&gt;get_meta(&#39;cstm_items_count&#39;); // Get items quantity count from meta data
echo &#39;&lt;ul&gt;&#39;;
$i = 0;
for( $k=1; $k &lt;= $quantity; $k++ ) {
$i++;
echo &#39;&lt;li&gt;&lt;strong&gt;&#39;. __(&quot;Dati iscritto n. &quot;) . $i . &#39;&lt;strong&gt;&lt;/li&gt;
&lt;li&gt;&#39; . __(&quot;Nome e cognome: &quot;) . $order-&gt;get_meta(&#39;cstm_full_name&#39;.$i) . &#39;&lt;/li&gt;
&lt;li&gt;&#39; . __(&quot;Email: &quot;) . $order-&gt;get_meta(&#39;cstm_email&#39;.$i) . &#39;&lt;/li&gt;
&lt;li&gt;&#39; . __(&quot;Numero di telefono: &quot;) . $order-&gt;get_meta(&#39;cstm_phone&#39;.$i) . &#39;&lt;/li&gt;
&lt;li&gt;&#39; . __(&quot;Indirizzo di residenza: &quot;) . $order-&gt;get_meta(&#39;cstm_address&#39;.$i) . &#39;&lt;/li&gt;&#39;;
}
echo &#39;&lt;/ul&gt;&#39;;
}
add_action( &#39;woocommerce_admin_order_data_after_order_details&#39;, &#39;display_custom_fields_in_admin_order_pages&#39; );
function display_custom_fields_in_admin_order_pages( $order ){ 
$quantity = $order-&gt;get_meta(&#39;cstm_items_count&#39;); // Get items quantity count from meta data  
echo &#39;&lt;div class=&quot;order_data_column&quot; style=&quot;width: 100% !important;&quot;&gt;
&lt;h4&gt;&#39; . __( &#39;Your label&#39; ) . &#39;&lt;/h4&gt;
&lt;ul&gt;&#39;;
$i = 0;
for( $k=1; $k &lt;= $quantity; $k++ ) {
$i++;
echo &#39;&lt;li&gt;&lt;strong&gt;&#39;. __(&quot;Dati iscritto n. &quot;) . $i . &#39;&lt;strong&gt;&lt;/li&gt;
&lt;li&gt;&#39; . __(&quot;Nome e cognome: &quot;) . $order-&gt;get_meta(&#39;cstm_full_name&#39;.$i) . &#39;&lt;/li&gt;
&lt;li&gt;&#39; . __(&quot;Email: &quot;) . $order-&gt;get_meta(&#39;cstm_email&#39;.$i) . &#39;&lt;/li&gt;
&lt;li&gt;&#39; . __(&quot;Numero di telefono: &quot;) . $order-&gt;get_meta(&#39;cstm_phone&#39;.$i) . &#39;&lt;/li&gt;
&lt;li&gt;&#39; . __(&quot;Indirizzo di residenza: &quot;) .$order-&gt;get_meta(&#39;cstm_address&#39;.$i) . &#39;&lt;/li&gt;&#39;;
}
echo &#39;&lt;/ul&gt;
&lt;/div&gt;&#39;;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

On Admin single Order Pages:

基于所选产品数量的附加字段

On email notifications:

基于所选产品数量的附加字段

In the database, under wp_postmeta table for the order ID (post ID):

基于所选产品数量的附加字段

答案2

得分: 0

如果您正在使用Divi主题并使用Divi主题构建器创建了结账页面,此代码将不会保存您的自定义字段数据。因此,我建议您不要在结账页面使用它,而是使用标准WooCommerce页面中已经存在的短代码。

英文:

If you are using the Divi theme and have created the checkout page with the Divi theme builder this code will not save your custom field data. so I advise you not to use it for the checkout page and use the shortcode already present in the standard woocomerce page

huangapple
  • 本文由 发表于 2023年6月8日 13:51:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定