A previous question of mine was answered and included two separate functions, one using a WooCommerce add_action
hook. How can I take this answer and convert it to a shortcode that I could add to a product page?
I am using a page builder (Divi Builder) to create a custom layout/template of WooCommerce product pages. Having a shortcode will allow me to paste the shortcode into the builder, and have it output the result anywhere within that template structure.
This is the code I need turned into a shortcode:
// 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 : $empty;
$output = '
<ul>
<li class="fs-data-price">'.$price.'</li>
<li class="fs-data-size">Size: '.$size.'</li>
<li class="fs-data-sale">'.$sale_price.' Preferred Customer Price</li>
<li class="fs-data-stock">Quantity in Stock: '.$stock_qty.'</li>
</ul>';
return $output;
}
//
add_action( 'woocommerce_after_single_product', '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
}
You will need to use Wordpress Shortcode Api
Implementation would be something like this:
I have added the add_shortcode line,
The explanation is that the shortcode 'format' does the 'format_product_data_output' function.
Update 2: Try the following:
Code goes in function.php file of the active child theme (or active theme). Tested and works.
SHORTCODE USAGE:
[variation_table]
(without any need of define an ID)[variation_table id='27']