Posted on 6 Comments

How to Add MPN to WooCommerce

In the current article, we will review how to add a unique number issued by manufacturers to identify individual products – Manufacturer Part Number (MPN) – to your products in WooCommerce. We will describe how to add and save it for your products and variations in the backend; how to display MPNs for products and variations in the frontend; how to add MPNs to your products schema.

Adding MPN to WooCommerce

MPN field

First of all, we need a field in our backend for the MPN. The snippet below will add the MPN field right after the standard SKU field, and it will save the field’s value when we update the product. You can change the position of our MPN field by replacing the woocommerce_product_options_sku with some other hook, for example, if you’ll use woocommerce_product_options_pricing, then the field will be added right after the product pricing fields.

add_action( 'woocommerce_product_options_sku', 'wpfactory_add_mpn' );
add_action( 'save_post_product', 'wpfactory_save_mpn', 10, 2 );

if ( ! function_exists( 'wpfactory_add_mpn' ) ) {
    /**
     * Adds MPN field to product admin edit page.
     */
    function wpfactory_add_mpn() {
        woocommerce_wp_text_input( array(
            'id'    => 'mpn',
            'value' => get_post_meta( get_the_ID(), 'mpn', true ),
            'label' => esc_html__( 'MPN', 'text-domain' ),
        ) );
    }
}

if ( ! function_exists( 'wpfactory_save_mpn' ) ) {
    /**
     * Saves product MPN field.
     */
    function wpfactory_save_mpn( $post_id, $__post ) {
        if ( isset( $_POST['mpn'] ) && empty( $_REQUEST['woocommerce_quick_edit'] ) ) {
            update_post_meta( $post_id, 'mpn', wc_clean( $_POST['mpn'] ) );
        }
    }
}
Adding MPN to WooCommerce - Backend - Simple Product
Backend – Simple Product

Variations

You probably noticed that MPN is not added for variable product variations. To add it to each variation, we need to add an extra snippet. Like the first snippet, it will add the field and save it on product updates. You can change the field’s position by replacing the woocommerce_variation_options_pricing hook. For example, if you use the woocommerce_variation_options_dimensions hook, the field will be added right after the variation’s dimensions fields.

add_action( 'woocommerce_variation_options_pricing', 'wpfactory_add_mpn_variation', 10, 3 );
add_action( 'woocommerce_save_product_variation', 'wpfactory_save_mpn_variation', 10, 2 );

if ( ! function_exists( 'wpfactory_add_mpn_variation' ) ) {
    /**
     * Adds MPN field to variations.
     */
    function wpfactory_add_mpn_variation( $loop, $variation_data, $variation ) {
        woocommerce_wp_text_input( array(
            'id'            => "variable_mpn_{$loop}",
            'name'          => "variable_mpn[{$loop}]",
            'value'         => ( isset( $variation_data['mpn'][0] ) ? $variation_data['mpn'][0] : '' ),
            'label'         => esc_html__( 'MPN', 'text-domain' ),
            'wrapper_class' => 'form-row form-row-full',
        ) );
    }
}

if ( ! function_exists( 'wpfactory_save_mpn_variation' ) ) {
    /**
     * Saves variation MPN field.
     */
    function wpfactory_save_mpn_variation( $variation_id, $i ) {
        if ( isset( $_POST['variable_mpn'][ $i ] ) ) {
            update_post_meta( $variation_id, 'mpn', wc_clean( $_POST['variable_mpn'][ $i ] ) );
        }
    }
}
Adding MPN to WooCommerce - Backend - Variation
Backend – Variation

Display

Currently, the MPN field is only visible in the backend. If you want to display it to your customers on the frontend, use the snippet below. It will add the MPN to the product meta, right above the SKU. If you want to display it at the end of the product meta, replace the woocommerce_product_meta_start hook with woocommerce_product_meta_end.

add_action( 'woocommerce_product_meta_start', 'wpfactory_display_mpn' );

if ( ! function_exists( 'wpfactory_display_mpn' ) ) {
    /**
     * Display MPN.
     */
    function wpfactory_display_mpn() {
        if ( '' !== ( $mpn = get_post_meta( get_the_ID(), 'mpn', true ) ) ) {
            printf( '<span class="mpn_wrapper">%s: <span class="mpn">%s</span></span>',
                esc_html__( 'MPN', 'text-domain' ), $mpn );
        }
    }
}
Adding MPN to WooCommerce - Frontend - Simple Product
Frontend – Simple Product

Display for variations

Like the backend, we’ll need a separate snippet to handle variations on the frontend. The easiest way is to add the MPN to each variation’s description – this way, it will be automatically updated when a user selects a different variation.

add_filter( 'woocommerce_available_variation', 'wpfactory_display_mpn_variation', 10, 3 );

if ( ! function_exists( 'wpfactory_display_mpn_variation' ) ) {
    /**
     * Display MPN in variation description.
     */
    function wpfactory_display_mpn_variation( $args, $product, $variation ) {
        if ( '' !== ( $mpn = $variation->get_meta( 'mpn' ) ) ) {
            $args['variation_description'] .= sprintf( '<span class="mpn_wrapper">%s: <span class="mpn">%s</span></span>',
                esc_html__( 'MPN', 'text-domain' ), $mpn );
        }
        return $args;
    }
}
Adding MPN to WooCommerce - Frontend - Variation
Frontend – Variation

Product structured data

Finally, you may want to add the MPN to the product schema.

add_filter( 'woocommerce_structured_data_product', 'wpfactory_add_mpn_structured_data_product', 10, 2 );

if ( ! function_exists( 'wpfactory_add_mpn_structured_data_product' ) ) {
    /**
     * Adds MPN field to product structured data.
     */
    function wpfactory_add_mpn_structured_data_product( $markup, $product ) {
        if ( '' !== ( $mpn = get_post_meta( $product->get_id(), 'mpn', true ) ) ) {
            $markup['mpn'] = $mpn;
        }
        return $markup;
    }
}

6 thoughts on “How to Add MPN to WooCommerce

  1. Very useful guide!
    what is the meta field name that can be used for bulk upload ???

    1. Hi Avraham,

      Happy to help 🙂

      We used the mpn meta key in our snippets.

      I hope this helps. Please let me know if you have any questions.

  2. Thank you, now if you could just a cost field for when the cost price is also a sale price in the cost plugin.

    1. Hi,

      I’m not sure if I got it correctly – could you please elaborate?

  3. Where do these snippets go?

    1. Hi Frances,

      You can add it to your (child) theme’s functions.php file, or use a plugin, e.g., Code Snippets.

      P.S. You can also check our How to Add Custom PHP Code in WordPress article.

Leave a Reply

Your email address will not be published.