Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #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?

    • This topic was modified 7 months ago by RENZCOM.
    • This topic was modified 6 months, 4 weeks ago by RENZCOM.
    #1441216

    you 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?

    #1441246

    Sorry. 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!

    #1441253

    Hi,

    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,
    Ismael

    #1441408

    We found the mistake! It was not the theme!
    Tanks anyway for the fast support!

    #1441439

    Hi,

    Thanks for the update. Please let us know if you should need any further help on the topic, or if we can close it.

    Best regards,
    Rikard

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.