Just wanted to share this in case it comes in useful for other folk… there may well be a better way of doing it, but this worked a charm for me!
Recently had a case where I needed to display additional product attributes on the WC archive pages. E.g. a site sells ladders, and you want a label on the archive page telling customers if it’s aluminium, plastic, wood etc.
Had it running on an old child theme override, but wanted more control over the archive page so rebuilt it using Layout Builder and loopers. Might have been possible to do it ‘native’ in a looper with conditions, but prefer to do the heavy lifting in php as the real site is somewhat more complicated than ladders.
The Looper was working fine with standard product fields, so I just needed a block of different attributes displaying below the title. Initial thought was to include te old php file, and hey presto, it… almost worked Problem was that it was displaying the same information for the first product in the loop on every product, so I figured sending the product id to the include should fix it, and it did. Stood on the shoulders of giants to get the ‘basic’ include code, then adapted to send variables to get_template_part. Huzzah!
Hope this helps someone, and happy to learn a more elegant way of doing it.
functions.php:
function super_include_file($args) {
$a = shortcode_atts( array(
'slug' => 'NULL',
'pid' => 'NULL'
), $args );
if($a['slug'] != 'NULL'){
ob_start();
get_template_part(
$a['slug'],
null,
array(
'pid' => $a['pid'],
)
);
return ob_get_clean();
}
}
add_shortcode('superinclude', 'super_include_file');
Shortcode:
[superinclude slug="woocommerce/loop/custom-material" pid="{{dc:woocommerce:product_id}}"]
Included file:
global $product;
$productID = esc_html( $args['pid'] );
$product = wc_get_product( $productID );
$material = $product->get_attribute( 'material' );
echo blah blah blah…