Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// Going from 'pa_size' to 'Size' the hard way.
if ( 0 === strpos( $args[ 'attribute' ], 'pa_' ) ) {
$args['show_option_none'] = 'Choose ' . ucwords( str_replace( '-', ' ', substr( $args[ 'attribute' ], 3 ) ) );
}
<?php
/*
Plugin Name: Choose Attribute Name
Plugin URI: https://www.damiencarbery.com/2018/06/customise-choose-an-option-dropdown-item/
Description: Change the default "Choose an option" to be based on the attribute name.
Author: Damien Carbery
Version: 0.1
*/
// Change 'Choose an option' to use attribute name to be more user friendly.
// Inspired by: https://stackoverflow.com/a/34713246/8605943
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'am_change_option_none_text' );
function am_change_option_none_text( $args ) {
$args['show_option_none'] = 'Choose ' . wc_attribute_label( $args[ 'attribute' ] );
return $args;
}
<?php
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'am_change_option_none_text' );
function am_change_option_none_text( $args ) {
// Only change text for these two product attributes.
if ( 'pa_size' == $args[ 'attribute' ] ) {
$args['show_option_none'] = 'Choose size';
}
if ( 'pa_colour' == $args[ 'attribute' ] ) {
$args['show_option_none'] = 'Choose colour';
}
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.