Snippets

Barn2 Plugins Replace Related Products with WooCommerce Product Table

Created by Paul Houser last modified
<?php
/**
 * Related Products
 *
 * This template is a modification of the woocommerce/single-product/related.php template, it reimplements the related products 
 * to show a list of related products inside the WooCommerce Product Table plugin.
 *
 * @version     3.9.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
if ( ! $related_products ) {
    return;
} 

$ids = [];
foreach ( $related_products as $related_product ) {
    $ids[] = $related_product->get_id();
}

if ( empty( $ids ) ) {
    return;
}

// You can customize the shortcode according your site's needs. Replace
// everything between the single quote marks with your product_table shortcode of
// choice. Just make sure to leave the `include="%s"` part in tact.
$shortcode = '[product_table include="%s"]';

?>

<section class="related products">

    <?php

    $heading = apply_filters( 'woocommerce_product_related_products_heading', __( 'Related products', 'woocommerce' ) );

    if ( $heading ) :
        ?>
        <h2><?php echo esc_html( $heading ); ?></h2>
    <?php endif; ?>

    <?php


    echo do_shortcode( sprintf( $shortcode, implode( ',', $ids ) ) ); 

    ?>

</section>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.