Skip to content
Website design companyWebsite design company
Artofdata Website Designers
  • Web
    • Website Design
      • Portfolio
      • Win new clients with brochure website design
      • Estate Agent Website Designers
      • Elementor Pro
      • WooCommerce
    • Ecommerce
      • Retail clothing websites
    • Graphic Design Reading
    • WordPress Plugin Developers
      • Sequential Invoice Numbers Plugin
  • Marketing
    • SEO Content Writing
    • Social Media
  • Internet
    • Domain Names
    • Hosting
    • Fast Woocommerce Hosting
    • Fast Websites – Greener Hosting
  • Pricing
  • Etc.
    • About
    • News
    • Website Design Questionnaire
  • Contact
  • 0118 942 0101
  • Web
    • Website Design
      • Portfolio
      • Win new clients with brochure website design
      • Estate Agent Website Designers
      • Elementor Pro
      • WooCommerce
    • Ecommerce
      • Retail clothing websites
    • Graphic Design Reading
    • WordPress Plugin Developers
      • Sequential Invoice Numbers Plugin
  • Marketing
    • SEO Content Writing
    • Social Media
  • Internet
    • Domain Names
    • Hosting
    • Fast Woocommerce Hosting
    • Fast Websites – Greener Hosting
  • Pricing
  • Etc.
    • About
    • News
    • Website Design Questionnaire
  • Contact
  • 0118 942 0101
Website design companyWebsite design company
Artofdata Website Designers
  • Web
    • Website Design
      • Portfolio
      • Win new clients with brochure website design
      • Estate Agent Website Designers
      • Elementor Pro
      • WooCommerce
    • Ecommerce
      • Retail clothing websites
    • Graphic Design Reading
    • WordPress Plugin Developers
      • Sequential Invoice Numbers Plugin
  • Marketing
    • SEO Content Writing
    • Social Media
  • Internet
    • Domain Names
    • Hosting
    • Fast Woocommerce Hosting
    • Fast Websites – Greener Hosting
  • Pricing
  • Etc.
    • About
    • News
    • Website Design Questionnaire
  • Contact
  • 0118 942 0101
  • Web
    • Website Design
      • Portfolio
      • Win new clients with brochure website design
      • Estate Agent Website Designers
      • Elementor Pro
      • WooCommerce
    • Ecommerce
      • Retail clothing websites
    • Graphic Design Reading
    • WordPress Plugin Developers
      • Sequential Invoice Numbers Plugin
  • Marketing
    • SEO Content Writing
    • Social Media
  • Internet
    • Domain Names
    • Hosting
    • Fast Woocommerce Hosting
    • Fast Websites – Greener Hosting
  • Pricing
  • Etc.
    • About
    • News
    • Website Design Questionnaire
  • Contact
  • 0118 942 0101
  • Request a call back

Woocommerce get variation meta data PHP

Woocommerce get variation meta data PHP

Woocommerce get variation metadata PHP

This code goes in your WordPress child theme’s functions.php or your plugin script.

Assuming you or a plugin has added a custom field to your product variations in WooCommerce; you will probably want this information later e.g., to tell the customer what they bought exactly or to work out the profit on your product so the commission can be paid to your sales force. In our case, we added a “product cost price” as a variation custom field.

Our variation prices vary a gold widget costs more than a plastic widget.

In order to calculate the total cost price of the sale, we have had to multiply the value by the quantity bought. Once we have the value we can add a new meta record to the post order item meta.

PHP to get variation meta and add it to the order item meta

To summarise, when an item is added to the cart, we loop through cart items find the variation id and look up its associated meta data. This data is then stored for later using $item->update_meta_data

MYSQL Query for retrieving custom meta added to the WooCommerce order

add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );

function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {

	foreach( WC()->cart->get_cart() as $cart_item ){
    // Get the WC_Product object (instance) 
	$cartqty 		= $cart_item['quantity'];
	$variation_id 		= $cart_item['variation_id'];
	$custom_field_value 	= get_post_meta( $variation_id, 'YOUR CUSTOM FIELD NAME', true );
	$cost_line_total	= $cartqty * $custom_field_value;
}

    if ( ! empty( $custom_field_value ) ){
		$item->update_meta_data( 'cost_line_total', $cost_line_total );
    }
}
PHP-to-get-variation-meta-woocommerce

SQL For Reporting

We have then queried the post in the myphp database to create a Sales Commission Table in wp-admin. This is just a section of our code.

select DISTINCT
i.order_item_id,
im.meta_key as YOUR CUSTOM FIELD NAME,
im.meta_value as amount

inner join
wp_woocommerce_order_itemmeta as im 
on i.order_item_id = im.order_item_id

where 
p.post_type = 'shop_order'

I hope this post saves developers alot of time

Here are some related articles

https://wordpress.stackexchange.com/questions/354370/woocommerce-display-variation-custom-field-value

https://stackoverflow.com/questions/45256561/woocommerce-3-1-product-variation-meta-data

https://docs.woocommerce.com/document/variable-product/

get-variation-meta-woocommerce
  • 1.2 Ecommerce
  • July 13, 2021
  • 11:43 am
PrevPrevious
NextNext

Vectorization

March 30, 2026

Free Website Design

May 19, 2025

Websites for Tradesmen

October 25, 2024

Website Internal Link Building Explained

July 17, 2024

The Darkside of Eco Website Design

January 17, 2024

News Categories

1.0 Website Design

1.1 Brochure Websites

1.2 Ecommerce

1.3 Wordpress

2.0 SEO

2.1 Logo

2.2 Content Creation

3.0 Locations

3.1 Reading

PrevPreviousResponsive brochure style website. SEO by Artofdata
NextRevalyu Website LaunchNext
vectorization
free-websites

Free Website Design

websites for tradesmen

Websites for Tradesmen

SEO Internal link building explained

Website Internal Link Building Explained

eco-website-designers

The Darkside of Eco Website Design

websites-for-gardeners

Website Design Woodley Reading

Sort by stock for woocommerce

WooCommerce Elementor order by stock quantity

website-for-the-office-chair-man

Saving the environment with used office chairs (Oct 22)

website-for-revalyu

Revalyu Website Launch

get-variation-meta-woocommerce

Woocommerce get variation meta data PHP

website for law firms

Responsive brochure style website. SEO by Artofdata

How to SEO your logo

Logo SEO Search Engine Optimisation

E-Commerce-websites

Retail eCommerce Websites Reading

Website designers Reading News

Professionally written website content

Digital Marketing Agency Reading

Website Design Agency

website-for-clothing-companies

Migration to Woocommerce

custom-website-for-retailers

The Pangbourne Cheese Shop

signalR-website-development

SignalR Development

what-are-citations

SEO: What is Citation and Trust Flow?

SERPs-rankings-explained

What determines your position in the Search Engine Results Pages (SERP)

Budget websites Reading Berkshire

Budget websites Reading Berkshire

websites-for-accountants

Websites for Accountants

websites for law firms

New Site – E J Winter

Gutenberg-WordPress-editor

Gutenberg WordPress Editor

.co .uk domain names

Which domain name?

wordpress websitedesign

WordPress – What is it?

tails of tilehurst dog training web site

Tails of Tilehurst now live!

responsive website design

What’s a responsive site?

Get a free no obligation website design quote.

Click here

Tel: 01189 420101
Company Number: 6092965
Copyright Statement.

Website Design Bracknell | Website Design Wokingham |

Terms & Conditions | Privacy Policy

Artofdata.com Ltd
(Web Design Agency)
47 Brooksby Road,
Reading,
Berkshire,
RG31 6LZ

Facebook X-twitter Youtube Pinterest Instagram
Linkedin Reddit Youtube Map-marker-alt Rss
© 2026 ArtofData.com Ltd.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT