I have stumbled upon a pretty cool woocommerce snipped that I would like to incorporate into my site by entering it into my function.php page. Which I have done by now, but the problem is the fact that woocommerce now pulls the variable slug from the variable product and displays that on the frontend of the site.
Is there a way that I can change the code below to use the actual name that was given to the variable instead of the slug?
Here is the code bellow
/*product variations list table*/
function find_valid_variations() {
global $product;
$variations = $product->get_available_variations();
$attributes = $product->get_attributes();
$new_variants = array();
// Loop through all variations
foreach( $variations as $variation ) {
// Peruse the attributes.
// 1. If both are explicitly set, this is a valid variation
// 2. If one is not set, that means any, and we must 'create' the rest.
$valid = true; // so far
foreach( $attributes as $slug => $args ) {
if( array_key_exists("attribute_$slug", $variation['attributes']) && !empty($variation['attributes']["attribute_$slug"]) ) {
// Exists
} else {
// Not exists, create
$valid = false; // it contains 'anys'
// loop through all options for the 'ANY' attribute, and add each
foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) {
$attribute = trim( $attribute );
$new_variant = $variation;
$new_variant['attributes']["attribute_$slug"] = $attribute;
$new_variants[] = $new_variant;
}
}
}
// This contains ALL set attributes, and is itself a 'valid' variation.
if( $valid )
$new_variants[] = $variation;
}
return $new_variants;}
Thank you guys for sharing your knowladge.
As we get, product variations slug, we can get its name as follows,
Refernece Link : https://gist.github.com/renegadesk/3967956
Please, check code with line no., 80 to 91.
Here, we can directly extract variation name. (Line no. 86) through,
$woocommerce->attribute_label