I'm trying to modify the SKUs displayed on the product pages of variable products. The SKUs are in the format "text1.text2.text3". The texts can contain all letters and numbers and are of variable length. On the product page, I only want to display text1.text2 as SKU.

I came up with the following code so far that I included in the functions.php of my theme (Avada). However, the SKU output doesn't change. Are there any pointers you could give me? I'm not sure if I'm using the correct hook.

add_action( 'avada_woocommerce_before_product_summary', 'modifySKU' );

function modifySKU( $atts )
    global $product;

    $atts = shortcode_atts( array( 'id' => '', ), $atts );

    // If there is no ID, we are already on a product page
    if (empty( $atts['id'] ) )
        $sku = $product->get_sku();
        // new array $output_array();
        preg_match("/[^\.]*\.[^\.]*/", $sku, $output_array);
        $sku = reset($output_array);
        return $sku;

Try using the core WooCommerce filter woocommerce_get_sku:

function wpse_188691_woocommerce_get_sku( $sku, $product ) {
    if ( ! is_admin() ) {
        $parts = explode( '.', $sku );

        // Only first two parts
        $parts  = array_slice( $parts, 0, 2 );

        // OR all parts except last (remove above)
        // array_pop( $parts );

        $sku = implode( '.', $parts );

    return $sku;

add_filter( 'woocommerce_get_sku', 'wpse_188691_woocommerce_get_sku', 10, 2 );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.