How to add a custom field in the product-category for each product?
I want to display a custom attribute for each product in the loop on the product-category page, I'm using a brizy template and I'm using the following shortcode to get the custom attribute and display it on the page:
add_shortcode( 'tgol_product_rarity', 'get_tgol_product_rarity');
function get_tgol_product_rarity() {
// get shortcode user fields
$atts = shortcode_atts(array('price' => '14.99', 'sandbox' => 'false'), $atts);
global $product;
$local_product = $product;
try {
if( is_a($local_product, 'WC_Product') ) {
return wc_format_content( $local_product->get_attribute("rarity") );
//return $thepostid;
}
return "Product description shortcode run outside of product context";
} catch (Exception $e) {
return "Product description shortcode run outside of product context";
}
The problem is that for some reason all of the products are displaying the same attribute event tho every product has a different value for the given attribute, it looks like the global $product variable is being used as a reference for every product but duplicating one of the products value.
What can I do so that using a shortcode I can display the proper attribute for every product on the product-category page?
Thank you!
-
Hello!
Try to check birzy-pro/content/placeholders/post-loop.php line ~77
Find the
$content .= $replacer->replacePlaceholders($contentPlaceholder->getContent(), $newContext);
And replace it with
$content .= do_shortcode( $replacer->replacePlaceholders( $contentPlaceholder->getContent(), $newContext ) );
Please, before plugin update don't forget to backup your changed files.
Kind regards,
Mihail
0 -
This worked thank you! You should include this fix as an upcoming update for everyone who wants to customize his store further.
Thanks!
0 -
Hello!
Your welcome!
Yes, we will include it for sure.
Thank you!
Kind regards,
Mihail
0
Please sign in to leave a comment.
Comments
3 comments