Hello,
Thanks for your query.
In the above code you shared, the value needs to be static. It does not accept a variable.
If you want to use pa_brand
dynamically as your product brand, here’s the code you should use in your theme’s functions.php file:
/**
* Filter to show brand value in snippet preview.
*/
add_filter( 'rank_math/vars/replacements', function( $vars ) {
global $product;
if ( ! $product ) {
return $vars;
}
if ( isset( $vars['wc_brand'] ) ) {
$brand = $product->get_attribute('pa_brand');
// Use this code to get 1st element from brand attribute if multiple brands are assigned to a Product.
$brands = explode( ',', $brand );
if ( ! empty( $brands ) ) {
$brand = $brands[0];
}
$vars['wc_brand']['example'] = $brand;
}
return $vars;
});
/**
* Filter to replace wc_brand with custom value.
*/
add_filter( 'rank_math/replacements', function( $replacements ) {
if ( is_singular( 'product' ) && isset( $replacements['%wc_brand%'] ) ) {
$product = wc_get_product();
$brand = $product->get_attribute('pa_brand');
// Use this code to get 1st element from brand attribute if multiple brands are assigned to a Product.
$brands = explode( ',', $brand );
if ( ! empty( $brands ) ) {
$brand = $brands[0];
}
$replacements['%wc_brand%'] = $brand; // Pass $brand to set meta value for brand.
}
return $replacements;
});
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Thanks for getting back to me Todd.
As mentioned above, I have tried to add that code to the functions.php file but it doesn’t seem to solve the issue.
Below is a before and after image from the structured data testing tool.
Before:
https://ibb.co/5LVBZbc
After:
https://ibb.co/z2gQRqz
You can see that the code doesn’t associate the pa_brand attribute with the Brand structured data element.
Other then adding the code you supplied, the only other thing I did was make the pa_brand attribute visible on the product page:
https://ibb.co/CJ13WF1
Hope you can help me out.
Hello,
Can please share a temporary login link to your website in the Sensitive Data Section, so we can take a look: https://wordpress.org/plugins/temporary-login-without-password/?
Looking forward to helping you. Thank you.
Hello,
I have updated the sensitive data as requested. Can you please check further?
Thank you.
Hello,
I logged into your site and I added below code in your theme’s functions.php file:
/**
* Filter to replace wc_brand with custom value.
*/
add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
if ( is_singular( 'product' ) ) {
$product = wc_get_product();
$brand = $product->get_attribute('pa_brand');
// Use this code to get 1st element from brand attribute if multiple brands are assigned to a Product.
$brands = explode( ',', $brand );
if ( ! empty( $brands ) ) {
$entity['brand'] = $brands[0];
}
}
return $entity;
});
The product schema now shows the pa_brand attribute value in the brand.
Hope that helps. Thank you.
Hi Pratik,
That seems to have worked, thank you. Do you know why Todd’s version of the custom value filter didn’t work? I’d just like to understand it.
Also, Todd had a filter to show brand value in snippet preview. Are you able to work your magic on that filter to get it to work as well?
Thanks,
Paul
Hi Paul,
Thanks for the question and apologies for the delay.
Todd’s version was making use of a different filter that is fired to replace the variables on the meta descriptions and titles.
You can try the following modified version of Todd’s code that assumes that you only have a single brand for each product:
/**
* Filter to show brand value in snippet preview.
*/
add_filter( 'rank_math/vars/replacements', function( $vars ) {
global $product;
if(!$product){
return $vars;
}
if(isset($vars['wc_brand'])){
$brand = $product->get_attribute('brand');
$vars['%wc_brand%']['example'] = $brand;
}
return $vars;
});
I hope this info helps. Thank you.
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.