How to change woocommerce snipped to use a variable name instead of a variable slug for a product table list

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?

Read More

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.

Related posts

Leave a Reply

1 comment