Use product attribute for brand in structured data

  • Hi,

    I need help getting the product brand showing in Google search console via woocommerce.

    I have set a product attribute named ‘brand’, that I have populated with a different value per product. So it looks like ‘pa_brand’ = value.

    I have seen this filter mentioned a number of times in the support forums:

    /**
     * Filter to add Brand Name for Products.
     *
     * @param array $entity Snippet Data
     * @return array
     */
    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
        $entity['brand'] = 'Rank Math;
        return $entity;
    });

    However, I can’t get it to pull the product attribute in. If I replace ‘Rank Math’ in the above filter with ‘pa_brand’, it just shows pa_brand in Google structured data testing tool, without pulling in the actual value for that attribute.

    I have also tried the script mentioned here:
    https://pastebin.com/8m5XU9qP?fbclid=IwAR0CxWnmHCbVkTpoPHhqI6WFnz93ThWhLv1vbsmULq1ZQqRYymEkuDvuDFg

    But that doesn’t seem to work either.

    I don’t want to use the brand plugins you have mentioned in previous support forum posts as they aren’t very well rated and seem to have issues.

    Any help you could provide would be appreciated.

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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.

Viewing 7 replies - 1 through 7 (of 7 total)

The ticket ‘Use product attribute for brand in structured data’ is closed to new replies.