Skip to content

Instantly share code, notes, and snippets.

@gaswirth
Created March 22, 2020 20:09
Show Gist options
  • Save gaswirth/13c80cff0eefe64e5580f4d2d805dd17 to your computer and use it in GitHub Desktop.
Save gaswirth/13c80cff0eefe64e5580f4d2d805dd17 to your computer and use it in GitHub Desktop.
<?php
/**
* Render the latest product in the specified Product Category.
*
* @param array $atts The shortcode attributes
* @return string The formatted HTML
*/
function rhdwp_woo_yayday_latest_bundle_shortcode( $atts ) {
$product_args = shortcode_atts( array(
'category' => null,
'title' => '',
), $atts );
$product_cat = array( esc_attr( $product_args['category'] ) );
$products = wc_get_products( array(
'limit' => 1,
'category' => $product_cat,
) );
if ( $products ) {
$html = '<div class="single-product rhdwp-single-featured-product">';
foreach( $products as $product ) {
$product_template = do_shortcode( "[product_page id={$product->get_id()}]" );
$html .= $product_template;
}
$html .= '</div>';
}
return $html;
}
add_shortcode( 'yayday_latest_bundle', 'rhdwp_woo_yayday_latest_bundle_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment