A key factor in optimising an e-commerce website’s performance is ensuring the correct use of GTIN (Global Trade Item Number) and MPN (Manufacturer Part Number) fields for integration with Google Merchant Center. By doing so, you can significantly improve your website’s organic search visibility and unlock the potential for running high-performing PMAX/Shopping campaigns in Google Ads.

To maximise the opportunity, it’s essential to store additional product information like the GTIN and MPN. These fields help ensure products meet Google Merchant Center’s approval standards, allowing for greater exposure and enhanced ad campaign performance.

In many cases, an MPN may align with the SKU field already present in WooCommerce. However, we recommend adding MPN as a separate field since internal ERP systems often have different SKUs that don’t necessarily match the manufacturer’s part number. Separating these fields avoids potential conflicts and ensures accurate data is used for Google integration.

The code for these custom fields can either be added to your child theme’s functions.php file or managed via a separate plugin, depending on your preference.

The Code

Exit if accessed directly

Copy to Clipboard

This is a security check. It prevents direct access to the file outside of WordPress, which could be a potential security risk. If the ABSPATH constant (the path to the WordPress installation) is not defined, the script will stop executing.

Add custom fields for simple products in WooCommerce

Copy to Clipboard

This adds custom fields (RRP, MPN, GTIN, and a dropdown for Include in PPC?) to the WooCommerce product editing screen. These fields are displayed under the “General” tab when editing simple products. The woocommerce_wp_text_input function is used to add text input fields, and woocommerce_wp_select is used to add a dropdown selection field.

  • _as_rrp: Recommended Retail Price (RRP).
  • _as_mpn: Manufacturer Part Number (MPN).
  • _as_gtin: Global Trade Item Number (GTIN).
  • _as_ppc: Option to include the product in PPC campaigns.

Add custom fields for product variations

Copy to Clipboard

This adds the same custom fields (RRP, MPN, GTIN, and Include in PPC?) to individual product variations. The $loop parameter helps distinguish each variation when managing multiple ones in a product. The custom fields are displayed in the “Pricing” section of each variation.

Save custom fields for product variations

Copy to Clipboard

This saves the custom field values for each product variation using update_post_meta. Each value is sanitized using esc_attr() to ensure data safety before being saved to the database.

Display custom fields on the front-end for product variations

Copy to Clipboard

This filter hook adds the custom fields (RRP, MPN, GTIN, Include in PPC?) to the product variation data, making them available on the front-end when a user selects a variation. These fields are displayed in the product’s details section.

Full code

For ease, here is the completed code and remember if you need to use this in a plugin, you will have to add the appropriate plugin headers and activate the plugin in your WordPress installation.

Copy to Clipboard

Whats next? Progress on to my next article on using the new fields and other data available alongside WP-CLI to generate a Google Merchant Feed nightly.

Please note: Full SSH access is required to use WP-CLI.

Leave A Comment

Share this Story