How to add custom JSON-ld in Woo Commerce product header with WordPress

How to add custom JSON-ld in Woo Commerce product header with WordPress

This tutorial will help you how to enable or add custom JSON-ld in Woo Commerce products. Please follow the below steps to enable or add custom JSON-ld in in a bulk for all Woo Commerce products.

if you don’t have a good budget or wants to save your budget without buying expensive plugin like Yoast SEO or Schema related plugins then its a best method to add add or enable JSON Schema for all Woo Commerce products specially if you have to import products in bulks like 500 or 1000 or more then this solution is for you.

appearance > editor > functions.php

add_action('wp_head', 'osc_product_schema');
function osc_product_schema(){
	if(is_product()){
		global $product;
		$productID = get_the_ID();
		$product = wc_get_product( $productID );
		

		$json['@type'] = 'Product';
		$json['@id']  = 'product-' . get_the_ID();
		
			//get_the_title() 
			if(!empty(get_the_title())){
				$json['name'] = get_the_title();
			}

			//wp_get_attachment_url() 
			if(!empty(wp_get_attachment_url( $product->get_image_id() ))){
				$json['image'] = wp_get_attachment_url( $product->get_image_id() );
			}

 			//get_the_excerpt() 
			if(!empty(get_the_excerpt())){
				$json['description'] = get_the_excerpt();
			}

			//get_the_permalink() 
			if(!empty(get_the_permalink())){
				$json['url'] = get_the_permalink();
			}
			$product_sku = $product->get_sku();
			if(!empty($product_sku)){
				$json['sku'] = $product_sku;
				$json['mpn'] = $product_sku;
			}

			$json['brand'] = array(
				'@type'=> 'cartly',
				'name' => 'cartly'
			);
		
			$json['aggregateRating'] = array(
				'@type'                => 'AggregateRating',
				'ratingValue'          => '5',
				'ratingCount'			=> '50',
				'reviewCount'          => '10',
			);

			$json['review'] = array(
				'@type'        => 'Review',
				'reviewRating' => array(
					'@type'      => 'Rating',
					'ratingValue' => '5',
      				'bestRating'  => '5',
				),
				'author' => array(
					'@type'      => 'Person',
					'name' => 'Cartly',
				),
			);

			if(get_woocommerce_currency()){
				$woo_currency = get_woocommerce_currency();
			} else {
				$woo_currency = 'USD';
			}


			if($product->get_price()){
				$woo_price = $product->get_price();
			} else {
				$woo_price = '0';
			}
			$json['offers'] = array(
				'@type'                  => 'Offer',
				'priceCurrency'          => $woo_currency,
				'price'                  => $woo_price,
				'itemCondition'          => 'http://schema.org/NewCondition',
				'availability'           => 'http://schema.org/' . $stock = ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ),
				'seller'                 => array(
					'@type'                => 'Organization',
					'name'                 => get_bloginfo( 'name' ),
				)
			);
		
		if ( $product->is_type( 'variable' ) ) {
			$product_variations = $product->get_available_variations();
			$variation_product_id = $product_variations [0]['variation_id'];
			$variation_product = new WC_Product_Variation( $variation_product_id );
			$regular_price = $variation_product ->regular_price;
			$sale_price = $variation_product ->sale_price;
		}else{
			$regular_price = $product->regular_price;
			$sale_price = $product->sale_price;
		}
		if( $product->is_on_sale()) {	
			$json['offers'] = array(
				'@type'=> 'AggregateOffer',
				'lowPrice'=> '$'.$sale_price,
				'offerCount'=> number_format($product->stock,0,'',''),
			);
		}else{
			$json['offers'] = array(
				'@type'=> 'RegularOffer',
				'lowPrice'=> '$'.$regular_price,
				'offerCount'=> number_format($product->stock,0,'',''),
				);
			}	
			echo '<script type="application/ld+json" class="product_cartly_jsonld">'.json_encode($json).'</script>';
		}
	}

Ashok kuikel

Hi, I am Ashok Kuikel, WordPress Developer for WordPress Community. While Cloud Computing Associate and Alibaba MVP and ACA for Cloud Professional. You can follow me on Social Media, GitHub, and via my Blog Channels.

Leave a Reply

Your email address will not be published. Required fields are marked *