add order item meta woocommerce

How to Easily Add Order Item Meta in Woocommerce

by Expert of Tech

If you add the order item meta in woocommerce, it will add a small input field for each ordered product. This can be used to add things like shipping information and any other data that the customer may need for this specific purchase. It also makes it easier to fill out return forms if necessary. You can add the order item meta in woocommerce by adding custom code into your theme files.

What is WooCommerce?

The Woocommerce is a popular plugin to create a ecommerce website using WordPress. It’s totally to free to use and anyone can contribute to improve the plugin.

How to add order item meta in woocommerce: see code example.

Add order item meta

Use below code if you are using class.

add_action( 'woocommerce_add_order_item_meta', array( $this,'add_order_item_meta'), 10, 2 );

public static function add_order_item_meta($item_id, $values) {
    $key = 'statusitem'; // Define your key here
    $value = 'AAA'; // Get your value here

    wc_update_order_item_meta($item_id, $key, $value);
}

Use below code if you are not using class.

add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta', 10, 2 );

function add_order_item_meta($item_id, $values) {
    $key = 'statusitem'; // Define your key here
    $value = 'AAA'; // Get your value here

    wc_update_order_item_meta($item_id, $key, $value);
}

Add order item meta using plugin

If you want to add order item metadata, then there are several ways to do so, depending on which process works best for your website. For example, if you run a Magento store or an OpenCart store, you can add order item meta in woocommerce by using add-ons. If you run a WordPress website, it is possible to add order item metadata in woocommerce with plugins.




Suppose you are running WooCommerce on an eCommerce platform other than Magento or OpenCart. In that case, adding order item meta may not be available since those two platforms feature. However, if you add order item metadata for woocommerce in Magento or OpenCart on your WooCommerce store website, it will be displayed on all products and product details pages.


When adding the metadata with add-ons, firstly download the add-on from the site where it is available. Once you add the order item meta in woocommerce, it will add a small input field for each ordered product. This can be used to add things like shipping information and any other data that the customer may need for this specific purchase. It also makes it easier to fill out return forms if necessary. You can add metadata using add-ons.

<?php

// WC_Checkout

/**
 * Add shipping lines to the order.
 *
 * @param WC_Order $order                   Order Instance.
 * @param array    $chosen_shipping_methods Chosen shipping methods.
 * @param array    $packages                Packages.
 */
public function create_order_shipping_lines( &$order, $chosen_shipping_methods, $packages ) {
  foreach ( $packages as $package_key => $package ) {
    if ( isset( $chosen_shipping_methods[ $package_key ], $package['rates'][ $chosen_shipping_methods[ $package_key ] ] ) ) {
      $shipping_rate            = $package['rates'][ $chosen_shipping_methods[ $package_key ] ];
      $item                     = new WC_Order_Item_Shipping();
      // ... code removed for reading purposes.

      /**
       * Action hook to adjust item before save.
       *
       * @since 3.0.0
       */
      do_action( 'woocommerce_checkout_create_order_shipping_item', $item, $package_key, $package, $order );

      // Add item to order and save.
      $order->add_item( $item );
    }
  }
}


After adding order item metadata in woocommerce, go to the product data section and add some meta information for your products. You can add the custom code into your theme files by adding a new text box on all product pages displayed right above or below the regular details fields like title, description, etc. This will add order item metadata for woo-commerce to your theme files.

Additional things to know when adding order meta

You can add order item metadata for woocommerce by using add-ons in the admin panel. Firstly download the add-on from where it is available, then add code to your theme files, or you can add custom code into your theme files by adding a new text box on all of the product pages that are displayed right above or below the regular details fields like title, description, etc.


This added order item metadata for woocommerce will add a small input field for each ordered product. This can be used to add things like shipping information and any other data that the customer may need for this specific purchase. It also makes it easier to fill out return forms if necessary.

<?php
add_action( 'woocommerce_before_add_to_cart_button', 'add_fields_before_add_to_cart' );

function add_fields_before_add_to_cart( ) {
	?>
	<table>
		<tr>
			<td>
				<?php _e( "Name:", "aoim"); ?>
			</td>
			<td>
				<input type = "text" name = "customer_name" id = "customer_name" placeholder = "Name on Gift Card">
			</td>
		</tr>
		<tr>
			<td>
				<?php _e( "Message:", "aoim"); ?>
			</td>
			<td>
				<input type = "text" name = "customer_message" id = "customer_message" placeholder = "Your Message on Gift Card">
			</td>
		</tr>
	</table>
	<?php
}

In conclusion, that’s all to fix the issue but in any case, not so you can hire one of our expertise. We will respond and solve it ASAP.

Share it