Add Custom field to item metadata
-
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)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Add Custom field to item metadata’ is closed to new replies.