• Resolved pannelars

    (@pannelars)


    Hi i try to add a custom field to products. My code work and show my custom filed in frontend. In orders and item meta my custom field do not show. Whats is wrong with my code? Anyone that can help?

    
    // Display Fields
    add_action( 'woocommerce_product_options_inventory_product_data', 'woo_add_custom_inventory_fields' );
    
    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_inventory_fields_save' );
    
    // Add Fields
    function woo_add_custom_inventory_fields() {
    	global $woocommerce, $post;
    	echo '<div class="options_group">';
    
    		// EAN Kode
    		woocommerce_wp_text_input( 
    			array( 
    				'id'                => '_ean_field', 
    				'label'             => __( 'EAN Kode', 'woocommerce' ), 
    				'description'       => 'Strekkode på produkt',
    				'desc_tip'          => 'true',
    				'placeholder'       => 'EAN-Kode',
    				'type'              => 'number', 
    				'custom_attributes' => array(
    					'step'		 	    => 'any',
    					'min'	    		=> '0'
    				)
    			)
    		);
      
    	echo '</div>';
    }
    
    // Save Fields
    function woo_add_custom_inventory_fields_save( $post_id ){
    	$ean_field = $_POST['_ean_field'];
    	if(isset($ean_field)){
    		update_post_meta( $post_id, '_ean_field', esc_attr( $ean_field ));
    	}
    	
       $ean_data = get_post_meta($post_id,'_ean_field', true);
       if (empty($ean_data)){
          delete_post_meta($post_id,'_ean_field', '');
       }
    }
    //Add meta to item order
    add_action( 'woocommerce_checkout_create_order_line_item', 'woo_add_custom_inventory_fields_meta', 10, 4 );
    
    function woo_add_custom_inventory_fields_meta( $item, $cart_item_key, $values, $order) {
    	$ean_field = $_POST['_ean_field'];
    	if(isset($ean_field)){
    		$item->add_meta_data( 'ean_code',$values['_ean_field'], true );
    	} 
    }
    

    The problem is with this lines:

    
    //Add meta to item order
    add_action( 'woocommerce_checkout_create_order_line_item', 'woo_add_custom_inventory_fields_meta', 10, 4 );
    
    function woo_add_custom_inventory_fields_meta( $item, $cart_item_key, $values, $order) {
    	$ean_field = $_POST['_ean_field'];
    	if(isset($ean_field)){
    		$item->add_meta_data( 'ean_code',$values['_ean_field'], true );
    	} 
    }
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • I use:

    add_action( 'woocommerce_new_order_item', 'add_custom_meta_to_order', 99, 3 );
    function add_custom_meta_to_order( $item_id, $item, $order_id ) {
      if( isset( $item->legacy_values['size'] ) ) {
        wc_add_order_item_meta( $item_id, 'Size', $item->legacy_values['size'] );
      }
    Thread Starter pannelars

    (@pannelars)

    Tried with this, same problem. Meta data still empty:

    
    //Add meta to item order
    add_action( 'woocommerce_new_order_item', 'woo_add_custom_inventory_fields_meta', 99, 3 );
    function woo_add_custom_inventory_fields_meta( $item_id, $item, $order_id ) {
      if( isset( $item->legacy_values['_ean_field'] ) ) {
        wc_add_order_item_meta( $item_id, '_ean_field', $item->legacy_values['_ean_field'] );
      }
    }
    

    Maybe i has change your code wrong

    This is the guts of my code to add meta to cart, checkout and orders. Read it somewhere.
    https://pastebin.com/SYGgwUMb

    Thread Starter pannelars

    (@pannelars)

    Still i dont get get this to work with my custom field. This field is prefilled not a field for customer.

    The forum doesn’t lend itself to debugging code, and there are not many in-depth development posts on here.

    I have this function:

    function write_log ( $log )  {
      if ( true === WP_DEBUG ) {
        if ( is_array( $log ) || is_object( $log ) ) {
          error_log( print_r( $log, true ) );
        } else {
        error_log( $log );
        }
      }
    }

    Then in between every line of code in the suspect functions I can put:
    write_log ( $variable );
    $variable can be a simple, object or array. Then run it and look at debug.log to figure out which line is going wrong.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add Custom field to item metadata’ is closed to new replies.