-
swissky
Hey, actually, I try to display product_attributes like “brand” with Dynamic Content from the Header Block. The problem is, that the option to use one of these product_attributes is not available.
Any tips?
-
Hi there,
The product_attributes is to create custom woocommerce product taxonomies, GB can not pull the data directly as its REST API is not true.
However, you can try this method:
1. Add an additional CSS class to the headline block, eg.
product-brand
, do NOT enable dynamic data options.
https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/2. Add this PHP snippet:
add_filter('render_block', function($block_content, $block) { global $post; $terms = wp_get_post_terms($post->ID, 'pa_brand'); if ($terms) { $html = '<div class="product-brand">'; foreach ($terms as $term) { $html .= '<a href="' . esc_attr(get_term_link($term)) . '">' . esc_html($term->name) . '</a>'; } $html .= '</div>'; if (!empty($block['attrs']['className']) && 'product-brand' === $block['attrs']['className']) { $block_content = $html; } } return $block_content; }, 10, 2);
Adding PHP: https://docs.generatepress.com/article/adding-php/
It will output the product brand terms for the headline block.
Let me know if this helps!
-
swissky
okey thank you. Then i think i have to test something with ACF, so i don`t need to do this for each information i would like to show and the extra -> link to this page then.
-
Yes, creating post meta in ACF for product post type should be easier in this case 🙂
-
Nicolas
It seems that this solution now creates warnings: “PHP Warning: Attempt to read property “ID” on null”. I have these warning when editing some posts (events from the events calendar apparently).
Is adding a condition
if ( !is_admin())
a solution?add_filter('render_block', function($block_content, $block) { if ( !is_admin()) { global $post;
-
Yes, sorry I missed adding this condition, glad you figured it out!
- You must be logged in to reply to this topic.