{"id":444,"date":"2016-02-12T07:15:41","date_gmt":"2016-02-12T07:15:41","guid":{"rendered":"https:\/\/www.sushilkumar.ind.in\/blog\/?p=444"},"modified":"2016-02-12T09:46:40","modified_gmt":"2016-02-12T09:46:40","slug":"woocommerce-product-variations-grid-or-table-form-with-add-to-cart-button","status":"publish","type":"post","link":"https:\/\/www.sushilkumar.ind.in\/blog\/wordpress\/woocommerce-product-variations-grid-or-table-form-with-add-to-cart-button\/","title":{"rendered":"WooCommerce Product Variations Grid or Table form with add to cart button"},"content":{"rendered":"[tblock anim=&#8221;rubberBand&#8221; tcuscolor=&#8221;#9900ff&#8221; title=&#8221;WooCommerce Product Variations Grid or Table form with add to cart button&#8221; tag=&#8221;h3&#8243;\/]\n<p><strong>Copy and Paste the code in your theme function.php <\/strong><\/p>\n<pre>function find_valid_variations() {\r\n    global $product;\r\n \r\n    $variations = $product-&gt;get_available_variations();\r\n    $attributes = $product-&gt;get_attributes();\r\n    $new_variants = array();\r\n \r\n    \/\/ Loop through all variations\r\n    foreach( $variations as $variation ) {\r\n \r\n        \/\/ Peruse the attributes.\r\n \r\n        \/\/ 1. If both are explicitly set, this is a valid variation\r\n        \/\/ 2. If one is not set, that means any, and we must 'create' the rest.\r\n \r\n        $valid = true; \/\/ so far\r\n        foreach( $attributes as $slug =&gt; $args ) {\r\n            if( array_key_exists(\"attribute_$slug\", $variation['attributes']) &amp;&amp; !empty($variation['attributes'][\"attribute_$slug\"]) ) {\r\n                \/\/ Exists\r\n \r\n            } else {\r\n                \/\/ Not exists, create\r\n                $valid = false; \/\/ it contains 'anys'\r\n                \/\/ loop through all options for the 'ANY' attribute, and add each\r\n                foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) {\r\n                    $attribute = trim( $attribute );\r\n                    $new_variant = $variation;\r\n                    $new_variant['attributes'][\"attribute_$slug\"] = $attribute;\r\n                    $new_variants[] = $new_variant;\r\n                }\r\n \r\n            }\r\n        }\r\n \r\n        \/\/ This contains ALL set attributes, and is itself a 'valid' variation.\r\n        if( $valid )\r\n            $new_variants[] = $variation;\r\n \r\n    }\r\n \r\n    return $new_variants;\r\n}\r\n?&gt;\r\n &lt;table class=\"variations variations-grid\" cellspacing=\"0\"&gt;\r\n &lt;tbody&gt;\r\n &lt;?php\r\n foreach ($variations as $key =&gt; $value) {\r\n if( !$value['variation_is_visible'] ) continue;\r\n ?&gt;\r\n &lt;tr&gt;\r\n &lt;td&gt;\r\n &lt;?php foreach($value['attributes'] as $key =&gt; $val ) {\r\n $val = str_replace(array('-','_'), ' ', $val);\r\n printf( '&lt;span class=\"attr attr-%s\"&gt;%s&lt;\/span&gt;', $key, ucwords($val) );\r\n } ?&gt;\r\n &lt;\/td&gt;\r\n &lt;td&gt;\r\n &lt;?php echo $value['price_html'];?&gt;\r\n &lt;\/td&gt;\r\n &lt;td&gt;\r\n &lt;?php if( $value['is_in_stock'] ) { ?&gt;\r\n &lt;form class=\"cart\" action=\"&lt;?php echo esc_url( $product-&gt;add_to_cart_url() ); ?&gt;\" method=\"post\" enctype='multipart\/form-data'&gt;\r\n &lt;?php woocommerce_quantity_input(); ?&gt;\r\n &lt;?php\r\n if(!empty($value['attributes'])){\r\n foreach ($value['attributes'] as $attr_key =&gt; $attr_value) {\r\n ?&gt;\r\n &lt;input type=\"hidden\" name=\"&lt;?php echo $attr_key?&gt;\" value=\"&lt;?php echo $attr_value?&gt;\"&gt;\r\n &lt;?php\r\n }\r\n }\r\n ?&gt;\r\n &lt;button type=\"submit\" class=\"single_add_to_cart_button btn btn-primary\"&gt;&lt;span class=\"glyphicon glyphicon-tag\"&gt;&lt;\/span&gt; Add to cart&lt;\/button&gt;\r\n &lt;input type=\"hidden\" name=\"variation_id\" value=\"&lt;?php echo $value['variation_id']?&gt;\" \/&gt;\r\n &lt;input type=\"hidden\" name=\"product_id\" value=\"&lt;?php echo esc_attr( $post-&gt;ID ); ?&gt;\" \/&gt;\r\n &lt;input type=\"hidden\" name=\"add-to-cart\" value=\"&lt;?php echo esc_attr( $post-&gt;ID ); ?&gt;\" \/&gt;\r\n &lt;\/form&gt;\r\n &lt;?php } else { ?&gt;\r\n &lt;p class=\"stock out-of-stock\"&gt;&lt;?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?&gt;&lt;\/p&gt;\r\n &lt;?php } ?&gt;\r\n &lt;\/td&gt;\r\n &lt;\/tr&gt;\r\n &lt;?php } ?&gt;\r\n &lt;\/tbody&gt;\r\n &lt;\/table&gt;\r\n &lt;?php\r\n}\r\n<\/pre>\n<pre>function find_valid_variations() {\r\n    global $product;\r\n \r\n    $variations = $product-&gt;get_available_variations();\r\n    $attributes = $product-&gt;get_attributes();\r\n    $new_variants = array();\r\n \r\n    \/\/ Loop through all variations\r\n    foreach( $variations as $variation ) {\r\n \r\n        \/\/ Peruse the attributes.\r\n \r\n        \/\/ 1. If both are explicitly set, this is a valid variation\r\n        \/\/ 2. If one is not set, that means any, and we must 'create' the rest.\r\n \r\n        $valid = true; \/\/ so far\r\n        foreach( $attributes as $slug =&gt; $args ) {\r\n            if( array_key_exists(\"attribute_$slug\", $variation['attributes']) &amp;&amp; !empty($variation['attributes'][\"attribute_$slug\"]) ) {\r\n                \/\/ Exists\r\n \r\n            } else {\r\n                \/\/ Not exists, create\r\n                $valid = false; \/\/ it contains 'anys'\r\n                \/\/ loop through all options for the 'ANY' attribute, and add each\r\n                foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) {\r\n                    $attribute = trim( $attribute );\r\n                    $new_variant = $variation;\r\n                    $new_variant['attributes'][\"attribute_$slug\"] = $attribute;\r\n                    $new_variants[] = $new_variant;\r\n                }\r\n \r\n            }\r\n        }\r\n \r\n        \/\/ This contains ALL set attributes, and is itself a 'valid' variation.\r\n        if( $valid )\r\n            $new_variants[] = $variation;\r\n \r\n    }\r\n \r\n    return $new_variants;\r\n}\r\n<\/pre>\n[tblock anim=&#8221;bounceInUp&#8221; tcuscolor=&#8221;#00ff09&#8243; title=&#8221;Example&#8221; tag=&#8221;h2&#8243;\/]\n<img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"461\" data-permalink=\"https:\/\/www.sushilkumar.ind.in\/blog\/wordpress\/woocommerce-product-variations-grid-or-table-form-with-add-to-cart-button\/attachment\/woocommerce-product-variation-grid-table\/\" data-orig-file=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2016\/02\/woocommerce-product-variation-grid-table.png\" data-orig-size=\"1326,580\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"woocommerce-product-variation-grid-table\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2016\/02\/woocommerce-product-variation-grid-table-300x131.png\" data-large-file=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2016\/02\/woocommerce-product-variation-grid-table-1024x448.png\" tabindex=\"0\" role=\"button\" src=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2016\/02\/woocommerce-product-variation-grid-table.png\" alt=\"woocommerce-product-variation-grid-table\" width=\"1326\" height=\"580\" class=\"alignnone size-full wp-image-461\" srcset=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2016\/02\/woocommerce-product-variation-grid-table.png 1326w, https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2016\/02\/woocommerce-product-variation-grid-table-300x131.png 300w, https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2016\/02\/woocommerce-product-variation-grid-table-768x336.png 768w, https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2016\/02\/woocommerce-product-variation-grid-table-1024x448.png 1024w\" sizes=\"(max-width: 1326px) 100vw, 1326px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[tblock anim=&#8221;rubberBand&#8221; tcuscolor=&#8221;#9900ff&#8221; title=&#8221;WooCommerce Product Variations Grid or Table form with add to cart button&#8221; tag=&#8221;h3&#8243;\/] Copy and Paste the code in your theme function.php function find_valid_variations() { global $product; $variations = $product-&gt;get_available_variations(); $attributes = $product-&gt;get_attributes(); $new_variants = array(); \/\/ Loop through all variations foreach( $variations as $variation ) { \/\/ Peruse the attributes. \/\/ &hellip;<\/p>\n","protected":false},"author":1,"featured_media":461,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[54,13],"tags":[47,51,50,52,46,49,48],"class_list":["post-444","post","type-post","status-publish","format-image","has-post-thumbnail","","category-woo-commerce","category-wordpress","tag-product-variation","tag-product-variation-in-grid","tag-product-variation-in-table","tag-product-variation-in-tablegrid-with-add-to-cat-button","tag-woocommerce","tag-woocommerce-product-variations","tag-wordpress-product-variations","post_format-post-format-image"],"jetpack_publicize_connections":[],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2016\/02\/woocommerce-product-variation-grid-table.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p99pkJ-7a","_links":{"self":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/444"}],"collection":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/comments?post=444"}],"version-history":[{"count":0,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/444\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media\/461"}],"wp:attachment":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media?parent=444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/categories?post=444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/tags?post=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}