2

In Woocommerce, I would like to create a function that outputs a simple HTML table with height, width, regular price, and sale price for each variation of a variable product.

For example, let's say that the variable product comes with three variations with different dimensions and I need to make my function output this HTML:

<table>
<thead>
    <tr>
        <th>Height</th>
        <th>Width</th>
        <th>Regular price</th>
        <th>Sale price</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>180cm</td>
        <td>100cm</td>
        <td>224€</td>
        <td>176€</td>
    </tr>
    <tr>
        <td>210cm</td>
        <td>125cm</td>
        <td>248€</td>
        <td>200€</td>
    </tr>
    <tr>
        <td>240cm</td>
        <td>145cm</td>
        <td>288€</td>
        <td>226€</td>
    </tr>
</tbody>

I am not sure how to build a function for this so I can add it into woocommerce_after_single_product action inside content-single-product.php.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
Tom F.
  • 133
  • 1
  • 11

1 Answers1

3

Update (on 2018-03-27 - Restricted to variable products only, avoiding an error)

Here is the correct way to achieve hooking it in woocommerce_after_single_product action hook:

add_action( 'woocommerce_after_single_product', 'custom_table_after_single_product' );
function custom_table_after_single_product(){
    global $product;

   // Only for variable products
   if( ! $product->is_type('variable')) return; 

    $available_variations = $product->get_available_variations();

    if( count($available_variations) > 0 ){

        $output = '<table>
            <thead>
                <tr>
                    <th>'. __( 'Height', 'woocommerce' ) .'</th>
                    <th>'. __( 'Width', 'woocommerce' ) .'</th>
                    <th>'. __( 'Regular price', 'woocommerce' ) .'</th>
                    <th>'. __( 'Sale price', 'woocommerce' ) .'</th>
                </tr>
            </thead>
            <tbody>';

        foreach( $available_variations as $variation ){
            // Get an instance of the WC_Product_Variation object
            $product_variation = wc_get_product($variation['variation_id']);

            $sale_price = $product_variation->get_sale_price();
            if( empty( $sale_price ) ) $sale_price = __( '<em>(empty)</em>', 'woocommerce' );

            $output .= '
            <tr>
                <td>'. $product_variation->get_height() .'</td>
                <td>'. $product_variation->get_width() .'</td>
                <td>'. $product_variation->get_regular_price() .'</td>
                <td>'. $sale_price .'</td>
            </tr>';
        }
        $output .= '
            </tbody>
        </table>';

        echo $output;
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

All code is tested on Woocommerce 3+ and works. You can view additional hooks here

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • FYI, this is causing a white screen when viewing a non-variable product. I haven't had time to debug why yet, but figured I'd let you know (not sure if you're able to replicate). – Garconis Mar 27 '18 at 19:25
  • 1
    The latest one. ;) – Garconis Mar 27 '18 at 19:30
  • Thoughts on getting this to work with BOTH variable and simple products? E.g., if it's a simple one, it'd just show 1 row? – Garconis Mar 29 '18 at 19:53
  • Well, I don't have a use for this exact request above. But instead for a different type of data: https://gyazo.com/08cc02cd7d13b2de2df368bc83386138 Was thinking I could display this info for each variation. But if the product was a simple product, it would still display this info in the same structure. So, a block for each set of: Price, Size, Sale Price, Stock – Garconis Mar 29 '18 at 20:38
  • @LiocTheAztec, sure thing: https://stackoverflow.com/questions/49564967/woocommerce-variable-and-simple-products-display-price-stock-and-variation-of – Garconis Mar 29 '18 at 21:02