Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Last active September 28, 2025 10:07
Show Gist options
  • Select an option

  • Save Lonsdale201/064948e56bf3594e63fcb393b25a5550 to your computer and use it in GitHub Desktop.

Select an option

Save Lonsdale201/064948e56bf3594e63fcb393b25a5550 to your computer and use it in GitHub Desktop.
Product bundle
// user the [bundle_items] shortcode, or [bundle_items product_id="123" show_qty="yes" show_optional="yes" show_price="no" list_class="bundle-items"]
// place the code in the child theme funcitons.php or custom php snippets plugint
// required plugin https://woocommerce.com/products/product-bundles/
add_action( 'init', function () {
add_shortcode( 'bundle_items', function ( $atts = [] ) {
if ( ! function_exists( 'wc_get_product' ) ) {
return '';
}
$atts = shortcode_atts( [
'product_id' => '',
'show_qty' => 'yes',
'show_optional' => 'yes',
'show_price' => 'no',
'list_class' => 'bundle-items',
], $atts, 'bundle_items' );
$product = null;
if ( ! empty( $atts['product_id'] ) ) {
$product = wc_get_product( absint( $atts['product_id'] ) );
} else {
global $product;
}
if ( ! $product || ! $product instanceof WC_Product ) {
return '';
}
if ( ! $product->is_type( 'bundle' ) || ! method_exists( $product, 'get_bundled_items' ) ) {
return '';
}
$bundled_items = $product->get_bundled_items();
if ( empty( $bundled_items ) ) {
return '';
}
$show_qty = strtolower( $atts['show_qty'] ) === 'yes';
$show_optional = strtolower( $atts['show_optional'] ) === 'yes';
$show_price = strtolower( $atts['show_price'] ) === 'yes';
ob_start();
?>
<ul class="<?php echo esc_attr( $atts['list_class'] ); ?>">
<?php foreach ( $bundled_items as $bundled_item ) :
$child = $bundled_item->get_product();
if ( ! $child ) {
continue;
}
$qty_min = (int) $bundled_item->get_quantity();
$optional = $bundled_item->is_optional();
$price_html = '';
if ( $show_price ) {
$price_html = $child->get_price_html();
if ( $price_html ) {
$price_html = ' <span class="bundle-item__price">' . wp_kses_post( $price_html ) . '</span>';
}
}
?>
<li class="bundle-item">
<a class="bundle-item__link" href="<?php echo esc_url( $child->get_permalink() ); ?>">
<?php echo esc_html( $child->get_name() ); ?>
</a>
<?php
if ( $show_qty && $qty_min > 0 ) {
echo ' <span class="bundle-item__qty">× ' . intval( $qty_min ) . '</span>';
}
if ( $show_optional && $optional ) {
echo ' <em class="bundle-item__optional">(opcionális)</em>';
}
echo $price_html;
?>
</li>
<?php endforeach; ?>
</ul>
<?php
return ob_get_clean();
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment