Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active September 13, 2021 06:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save damiencarbery/243821761aceb11f85d82e97a4253d69 to your computer and use it in GitHub Desktop.
Save damiencarbery/243821761aceb11f85d82e97a4253d69 to your computer and use it in GitHub Desktop.
<?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