phpwoocommerceorderscustom-fieldsproduct-variations

Add a custom setting field on WooCommerce product variations to customize the variation name


Is there a way I can add a 'Variation title' custom field to product variations, and replace the default product title with the 'Variation title' when manually creating orders?

This won't have any effect on the front end as I'm not showing single variations, though I have previously and the plugin I used provided this option.

The reason I need this option is for manually created orders.

The vast majority of my orders are subscriptions and I send out manually created orders each month to my customers.

When I was using a plugin to show variations as single products, I was able to add a custom title to my variable invoicing product, but I can longer do so.

Previously when adding a product variation for a subscription item costing $9.99, the item would be added using the custom title:

Subscription item Purchase: NZ$9.99

Now that I'm not using the plugin to show variations as single products and have no custom title option, the product variation is displayed in the Woo default manor like so:

Subscription item - NZ$9.99 Purchase: NZ$9.99

Where 'NZ$9.99' is the name of the attribute applied to the product variation.

It's probably not a big deal of course, but it makes the orders look messy and as the product attribute (NZ$9.99) is also the price it's pretty much redundant.

I've tried to find alternative options that I might be able to adapt, but the only options I can find are related to plugins that give the option of displaying single variants on the frontend.

I found this in the WPC Show Single Variantions plugin, which seems to at least be related to what I'm after but I have no idea how to use it:

function the_title( $post_title, $post_id ) {
            if ( ( $custom_name = get_post_meta( $post_id, 'woosv_name', true ) ) && ! empty( $custom_name ) ) {
                return $custom_name;
            }

            return $post_title;
        }

Any help would be very much appreciated, as I have no idea where to even start.


Solution

  • Based on Add variation setting fields to specific variable products in Woocommerce answer thread, you can use the following, to add a custom input field to variations settings fields:

    // Add custom field(s) to product variations
    add_action( 'woocommerce_variation_options_pricing', 'add_variation_setting_fields', 10, 3 );
    function add_variation_setting_fields( $loop, $variation_data, $variation ) {
        $field_key = 'custom_name';
        
        woocommerce_wp_text_input( array(
            'id'            => $field_key.'['.$loop.']',
            'label'         => __('Variation Name', 'woocommerce'),
            'wrapper_class' => 'form-row',
            'description'   => __('Add a custom Variation Name', 'woocommerce'),
            'desc_tip'      => true,
            'value'         => get_post_meta($variation->ID, $field_key, true)
        ) );
    }
    
    // Save the custom field from product variations
    add_action('woocommerce_admin_process_variation_object', 'save_variation_setting_fields', 10, 2 );
    function save_variation_setting_fields($variation, $i) {
        $field_key = 'custom_name';
    
        if ( isset($_POST[$field_key][$i]) ) {
            $variation->update_meta_data($field_key, sanitize_text_field($_POST[$field_key][$i]));
        }
    }
    

    enter image description here

    Then the following hooked functions will change or filter the variation title or name, based on your custom field value, when using get_title() or get_name() methods (methods used on any WooCommerce order creation):

    add_filter( 'woocommerce_product_title', 'custom_variation_title', 10, 2 );
    function custom_variation_title( $product_title, $variation ) {
        if ( $custom_title = $variation->get_meta('custom_name') ) {
            return $custom_title;
        }
        return $product_title;
    }
    
    add_filter( 'woocommerce_product_variation_get_name', 'custom_variation_name', 10, 2 );
    function custom_variation_name( $product_name, $variation ) {
        if ( $custom_name = $variation->get_meta('custom_name') ) {
            return $custom_name;
        }
        return $product_name;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.