Skip to content

Instantly share code, notes, and snippets.

@Garconis
Last active April 3, 2018 13:10
Show Gist options
  • Save Garconis/172361b74e37dd7b91273253372d5997 to your computer and use it in GitHub Desktop.
Save Garconis/172361b74e37dd7b91273253372d5997 to your computer and use it in GitHub Desktop.
WooCommerce | Product pricing table for simple and variable products
<?php
// Creates a block/card for each product (including variations) with Price, Stock, Size, etc. under each single product display
// https://stackoverflow.com/a/49565625/1887218
// Utility funtion: getting and formtting product data
function format_product_data_output( $the_id ){
$empty = __( '<em>(empty)</em>', 'woocommerce' );
// Get an instance of the WC_Product_Variation object
$product = wc_get_product( $the_id );
// Only wc_get_price_to_display() respect if product is to be displayed with or without including taxes
$price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
$sale_price = ! empty( $sale_price ) ? wc_price($sale_price) : $empty;
$size = $product->get_attribute( 'pa_size' );
$size = ! empty( $size ) ? get_term_by( 'slug', $size, 'pa_size' )->name : $empty;
$stock_qty = $product->get_stock_quantity();
$stock_qty = ! empty( $stock_qty ) ? $stock_qty : '0';
$output = '
<ul>
<li class="fs-data-price">'.$price.' ea.</li>
<li class="fs-data-size">Size: '.$size.'</li>
<li class="fs-data-sale">'.$sale_price.' ea. (Members only)</li>
<li class="fs-data-stock">Quantity in Stock: '.$stock_qty.'</li>
</ul>';
return $output;
}
// Display the data before the product meta area
add_action( 'woocommerce_product_meta_start', 'custom_table_after_single_product' );
function custom_table_after_single_product(){
global $product;
$output = '<div class="fs-product-data-wrapper">';
// Variable products
if( $product->is_type('variable'))
{
// Get available variations in the variable product
$available_variations = $product->get_available_variations();
if( count($available_variations) > 0 ){
foreach( $available_variations as $variation )
$output .= format_product_data_output( $variation['variation_id'] );
}
}
// Simple products
elseif( $product->is_type('simple'))
{
$output .= format_product_data_output( $product->get_id() );
}
else return; // Exit
echo $output .= '</div>'; // Display
}
/**
* -- OR ALTERNATIVELY USE A SHORTCODE VERSION
**/
// https://stackoverflow.com/a/49613980/1887218
// e.g., [variation_table] (without any need of define an ID) or [variation_table id='27']
add_shortcode("price_variation_table", "fs_custom_available_variations_table");
function fs_custom_available_variations_table( $atts ) {
global $post;
// Attributes
$atts = shortcode_atts(
array(
'id' => $post->ID
),
$atts, 'price_variation_table'
);
if( is_admin() ) return; // Only on front end
$product = wc_get_product($atts['id']); // Get the WC_Product Object
$output = '<div class="fs-product-data-wrapper">';
// Variable products
if( $product->is_type('variable'))
{
// Get available variations in the variable product
$available_variations = $product->get_available_variations();
if( count($available_variations) > 0 ){
foreach( $available_variations as $variation )
$output .= fs_format_product_data_output( $variation['variation_id'] );
}
}
// Simple products
elseif( $product->is_type('simple'))
{
$output .= fs_format_product_data_output( $product->get_id() );
}
else return; // Exit
return $output .= '</div>'; // return always for a shortcode
}
// Utility funtion: getting and formtting product data
function fs_format_product_data_output( $the_id ){
$empty = __( '<em>(empty)</em>', 'woocommerce' );
// Get an instance of the WC_Product_Variation object
$product = wc_get_product( $the_id );
// Only wc_get_price_to_display() respect if product is to be displayed with or without including taxes
$price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
$sale_price = ! empty( $sale_price ) ? wc_price($sale_price) : $empty;
$size = $product->get_attribute( 'pa_size' );
$size = ! empty( $size ) ? get_term_by( 'slug', $size, 'pa_size' )->name : $empty;
$stock_qty = $product->get_stock_quantity();
$stock_qty = ! empty( $stock_qty ) ? $stock_qty : '0';
$output = '
<ul>
<li class="fs-data-price">'.$price.' ea.</li>
<li class="fs-data-size">Size: '.$size.'</li>
<li class="fs-data-sale">'.$sale_price.' ea. (Members only)</li>
<li class="fs-data-stock">Quantity in Stock: '.$stock_qty.'</li>
</ul>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment