Home / Woo-Commerce / WooCommerce Product Variations Grid or Table form with add to cart button

WooCommerce Product Variations Grid or Table form with add to cart button

WooCommerce Product Variations Grid or Table form with add to cart button.


Copy and Paste the code in your theme function.php

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;
}
?>
 <table class="variations variations-grid" cellspacing="0">
 <tbody>
 <?php
 foreach ($variations as $key => $value) {
 if( !$value['variation_is_visible'] ) continue;
 ?>
 <tr>
 <td>
 <?php foreach($value['attributes'] as $key => $val ) {
 $val = str_replace(array('-','_'), ' ', $val);
 printf( '<span class="attr attr-%s">%s</span>', $key, ucwords($val) );
 } ?>
 </td>
 <td>
 <?php echo $value['price_html'];?>
 </td>
 <td>
 <?php if( $value['is_in_stock'] ) { ?>
 <form class="cart" action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" method="post" enctype='multipart/form-data'>
 <?php woocommerce_quantity_input(); ?>
 <?php
 if(!empty($value['attributes'])){
 foreach ($value['attributes'] as $attr_key => $attr_value) {
 ?>
 <input type="hidden" name="<?php echo $attr_key?>" value="<?php echo $attr_value?>">
 <?php
 }
 }
 ?>
 <button type="submit" class="single_add_to_cart_button btn btn-primary"><span class="glyphicon glyphicon-tag"></span> Add to cart</button>
 <input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?>" />
 <input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
 <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $post->ID ); ?>" />
 </form>
 <?php } else { ?>
 <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
 <?php } ?>
 </td>
 </tr>
 <?php } ?>
 </tbody>
 </table>
 <?php
}
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;
}
[tblock anim=”bounceInUp” tcuscolor=”#00ff09″ title=”Example” tag=”h2″/] woocommerce-product-variation-grid-table

About Sushil_kumar

Check Also

Jevelin v2.4.12 – Responsive Multi-Purpose WordPress Theme

Download Jevelin v2.4.12 | Themeforest Responsive Multi-Purpose WordPress Theme Jevelin v2.4.12 is one of the most popular multi-purpose WordPress themes available today, users have consistently given Jevelin positive ratings. With a growing library of high-quality, professional website designs ready to be imported into your WordPress site in just a few clicks, Jevelin can help you […] The post Jevelin v2.4.12 – Responsive Multi-Purpose WordPress Theme appeared first on Themes24x7 - Free Premium Blogger and Wordpress Templates. http://www.sushilkumar.ind.in...

Leave a Reply

Your email address will not be published. Required fields are marked *

Engineer Sushil Kumar is Stephen Fry proof thanks to caching by WP Super Cache