Created
January 25, 2025 20:02
-
-
Save hamidrezayazdani/e5165e54d99fae668fa3a89c30cac415 to your computer and use it in GitHub Desktop.
Add the slug to attribute selectbox in the WooCommerce product editing page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* When you have a lot of values in an attribute and some of them have similar names, | |
* choosing the right value in the attribute field in the product information box is a pain and there is a high chance of mistakes. | |
* One solution is to consider a different slug for each attribute. | |
* But the problem is that when searching for values in attributes, | |
* only the attribute name is displayed to you. | |
* The code that follows helps you choose the right values by displaying the slug of each value (after its name). | |
*/ | |
function ywp_modify_taxonomy_term_names( $terms, $taxonomy ) { | |
foreach ( $terms as &$term ) { | |
$decoded_slug = urldecode( $term->slug ); | |
$term->name = $term->name . ' - ' . $decoded_slug; | |
} | |
return $terms; | |
} | |
add_filter( 'woocommerce_json_search_found_product_attribute_terms', 'ywp_modify_taxonomy_term_names', 10, 2 ); | |
// The code goes to your active theme/child theme functions.php file. | |
// TESTED AND WORKING. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment