How to use Sku as ISBN meta

#74609
  • Resolved Steve Haines
    Rank Math free

    The products in my Woocommerce store are almost all books and the ISBN is stored as the SKU for each product. Is there a way to use the Sku value to populate the isbn meta for the product pages?

    I thought I had solved this using the following filter, but it doesn’t seem to work once RankMath is installed:
    add_filter( ‘woocommerce_structured_data_product’, ‘my_structured_data_product_filter’, 20, 2);
    function my_structured_data_product_filter ( $markup, $product) {
    $sku = $product->get_sku();
    if (strlen ($sku) == 13 & strpos ($sku, “978”)===0) { // Only use sku as isbn if it has 13 chars starting 978
    $markup[‘isbn’] = $sku; // Set isbn to product sku.
    }
    return $markup;
    }

    Is there an equivalent filter I can use with RankMath?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Alberto
    Rank Math business

    Hello,

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    Yes, there is a Rank Math filter you could use for that:

    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
        if(is_product()){
            global $product;
    	$sku = $product->get_sku();
            if (strlen ($sku) == 13 & strpos ($sku, “978”)===0) {
                  $entity['isbn'] = $sku;
            }
        }
        return $entity;
    });

    Looking forward to helping you. Thank you.

    ​​​​​​​

    Excellent! That works perfectly. I was also able to add other book schema entities from product attributes by adding the code

    	$attribute_map = array (
    		"bookFormat"	=> 'format',
    		'numberOfPages'	=> 'pages',
    		'author'	=> 'author-2',
    		'publisher'	=> 'publisher',
    		'series'	=> 'series',
    	);
    			
    	foreach ($attribute_map as $entname=>$slug) {
    		$attribute = $product->get_attribute ($slug);
    		if (!empty ($attribute) ) {
    			$entity [$entname] = $attribute;
    		}
    	}
    

    Many thanks for your help.

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

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

The ticket ‘How to use Sku as ISBN meta’ is closed to new replies.