-
AuthorPosts
-
April 26, 2024 at 2:26 pm #1440957
Hi there
We use the following code snippet to send an info to a special shop manager at checkout// Add checkout fields function custom_checkout_field( $checkout ) { // echo '<fieldset><legend>Zusätzliche Angaben</legend>'; $options = array(); $nicknames = array(); $args = array( 'role' => 'shop_manager' ); $users = get_users( $args ); foreach ( $users AS $user ) : $user_id = $user->ID; $user_info = get_userdata( $user_id ); $options[$user_id] = $user_info->first_name.' '.$user_info->last_name.' ('.$user_info->nickname.')'; foreach ( explode( ', ', $user_info->nickname ) as $nickname ) $nicknames[$nickname] = $nickname; endforeach; woocommerce_form_field( 'user_id', array( 'type' => 'select', 'class' => array( 'user_id form-row-first' ), 'label' => __( 'Cost center group' ), 'required' => true, 'options' => $options ), $checkout->get_value( 'user_id' ) ); woocommerce_form_field( 'cost_center', array( 'type' => 'select', 'class' => array( 'cost_center form-row-last' ), 'label' => __( 'Cost center' ), 'required' => true, 'options' => $nicknames ), $checkout->get_value( 'cost_center' ) ); // echo '</fieldset>'; } add_action( 'woocommerce_after_checkout_billing_form', 'custom_checkout_field' ); function custom_checkout_field_process() { /* if ( !str_contains( $_POST['first_name_1'], 'wf-ib.de' ) ) // Checkout: Email validation wc_add_notice( __( '<strong>E-Mail-Adresse:</strong> Es sind nur E-Mail-Adressen mit der Domainendung wf-ib.de zulässig.' ), 'error' ); */ if ( !$_POST['user_id'] ) wc_add_notice( sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . __( 'Cost center group' ) . '</strong>' ), 'error' ); if ( !$_POST['cost_center'] ) wc_add_notice( sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . __( 'Cost center' ) . '</strong>' ), 'error' ); } add_action( 'woocommerce_checkout_process', 'custom_checkout_field_process' ); function custom_checkout_field_update_order_meta( $order_id ) { if ( !empty( $_POST['user_id'] ) ) update_post_meta( $order_id, 'user_id', intval( $_POST['user_id'] ) ); if ( !empty( $_POST['cost_center'] ) ) update_post_meta( $order_id, 'cost_center', sanitize_text_field( $_POST['cost_center'] ) ); } add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta' ); function custom_checkout_field_display_admin_order_meta( $order ) { $user_id = get_post_meta( $order->id, 'user_id', true ); $cost_center = get_post_meta( $order->id, 'cost_center', true ); $user_info = get_userdata( $user_id ); echo '<p><strong>'.__( 'Cost center' ).'</strong>: '.$user_info->first_name.' '.$user_info->last_name.' ('.$cost_center.') <'.$user_info->user_email.'></p>'; } add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
The order meta for user_id and cost_center are not saved by the hook woocommerce_checkout_update_order_meta. The big question now is why? We use a very similar code snippet in other projects and there it works without problems. Does Enfold prevent something?
April 28, 2024 at 7:45 pm #1441216you can edit your post by pasting the code snippet into code tags! (please copy and paste the code from your template again).
It’s hard to check the snippet – when many letters are converted by Board Soft.One thing might be:
Since WooCommerce 3, the hook woocommerce_checkout_update_order_meta is outdated and replaced by woocommerce_checkout_create_order
PS: Isn’t that more a question for the Woocommerce support team?
April 29, 2024 at 7:13 am #1441246Sorry. I have edited the post.
We did something like this once before with a different theme and it went through. Hence the question of whether we have to pay special attention to something with Enfold.Thank you very much!
April 29, 2024 at 8:18 am #1441253Hi,
Thank you for the inquiry.
The theme doesn’t alter the checkout process or add anything to the checkout template, which might mean that the issue is likely due to another extension or plugin. Have you tried to test this with other plugins disabled?
You can check the theme’s modifications for the WooCommerce plugin in the enfold/config-woocommerce folder.
Best regards,
IsmaelApril 30, 2024 at 9:39 am #1441408We found the mistake! It was not the theme!
Tanks anyway for the fast support!April 30, 2024 at 12:29 pm #1441439 -
AuthorPosts
- You must be logged in to reply to this topic.