• Resolved radupl

    (@radupl)


    Hello,

    I’m trying to manually add Open Graph Meta info in the functions.php of my child theme, but I’m having trouble adding the product price and sku in there.

    Here’s a snippet of the code:

    
    function facebook_open_graph() {
        global $post;
    
            echo '<meta property="og:title" content="' . get_the_title() . '"/>';
            echo '<meta property="og:type" content="article"/>';
            echo '<meta property="og:url" content="' . get_permalink() . '"/>';
            echo '<meta property="product:condition" content="new"/>';
            echo '<meta property="product:price:currency" content="RON"/>';
            echo '<meta property="product:price:amount" content="' . get_price() . '"/>';
            echo '<meta property="product:retailer_item_id" content="' . get_sku() . '"/>';
    }
    add_action( 'wp_head', 'facebook_open_graph', 5 );
    

    The last two lines don’t work, so I was wondering if anyone could help me with the code that I need to change there to get the price or sku into the meta as well?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi @radupl,

    For the price and SKU, you’ll need to have access to the $product object and not just $post. SkyVerge has a quick article on using SKU in a function like this at:

    https://www.skyverge.com/blog/output-woocommerce-sku/

    For the global object at the top, you would use something like this:

    
    global $post, $product;
    

    and then for the SKU:

    
    $product->get_sku()
    

    and for the price:

    
    $product->get_price()
    

    Hope that gets you pointed in the right direction. Let us know if we can help.

    Thread Starter radupl

    (@radupl)

    Hey @3sonsdevelopment ,

    Thanks for the quick response!

    I just tried doing those changes, even just copy the code from the linked article, but I keep getting this error: Uncaught Error: Call to a member function get_sku() on null in wp-content/themes/shopkeeper-child/functions.php:63

    If I do a var_dump on $product, I only get the product’s name, such as:
    string(21) "stilou-exclusiv-metal"

    Any idea what’s going on? Or is this something that can be caused by the theme?

    • This reply was modified 6 years ago by radupl.

    Hello @radupl,

    Let’s modify this a bit so it uses the wp_query object too. Give this function a try:

    
    function facebook_open_graph() {
        
    	global $post, $wp_query;
    	$postID = $wp_query->post->ID;
    	$product = wc_get_product( $postID );
    
            echo '<meta property="og:title" content="' . get_the_title() . '"/>';
            echo '<meta property="og:type" content="article"/>';
            echo '<meta property="og:url" content="' . get_permalink() . '"/>';
            echo '<meta property="product:condition" content="new"/>';
            echo '<meta property="product:price:currency" content="RON"/>';
            echo '<meta property="product:price:amount" content="' . $product->get_price() . '"/>';
            echo '<meta property="product:retailer_item_id" content="' . $product->get_sku() . '"/>';
    }
    add_action( 'wp_head', 'facebook_open_graph', 5 );
    
    

    When I did that on my test site it returned this:

    meta properties

    Let me know it goes on your site,

    Thread Starter radupl

    (@radupl)

    Hey @3sonsdevelopment

    Again, thanks for the response. I did that change, and now I get: Uncaught Error: Call to a member function get_sku() on boolean in wp-content/themes/shopkeeper-child/functions.php:44
    get_price() doesn’t work either, I’ll just use sku as an example because it’s easier to talk about just one of them ??

    Running a var_dump on $product now does give me a lot of information. It seems the sku is here (I removed additional lines, cause it’s a lot of them):

    object(WC_Product_Simple)#1612 (12) {
      ["data":protected]=>
      array(49) {
        ["sku"]=>
        string(7) "9478290"
      }
    }

    I tried to get the sku from the array with $product[‘sku’]; but I just ended up with a blank page.

    • This reply was modified 6 years ago by radupl. Reason: wrote 9 instead of 'sku'
    Thread Starter radupl

    (@radupl)

    @3sonsdevelopment

    I have also tried to use
    $data = $product->get_data();
    to access the array, but I receive an error again: Uncaught Error: Call to a member function get_data() on boolean in wp-content/themes/shopkeeper-child/functions.php:52

    Hello @radupl,

    Sorry for the trouble there. Could I get you to post the complete function that you’re using here?

    Thanks!

    Thread Starter radupl

    (@radupl)

    Sure @3sonsdevelopment , no worries:

    function facebook_open_graph() {
    	global $post, $wp_query;
    	$postID = $wp_query->post->ID;
    	$product = wc_get_product( $postID );
    
    	
    	if ( !is_singular()) //if it is not a post or a page
    		return;
    		if($excerpt = $post->post_excerpt) 
    	        {
     			$excerpt = strip_tags($post->post_excerpt);
    			$excerpt = str_replace("", "'", $excerpt);
            	} 
            	else 
            	{
                		$excerpt = get_bloginfo('description');
    		}
    
    	echo '<meta property="og:title" content="' . get_the_title() . '"/>';
    	echo '<meta property="og:description" content="' . $excerpt . '"/>';
    	echo '<meta property="og:type" content="article"/>';
    	echo '<meta property="og:url" content="' . get_permalink() . '"/>';
    	echo '<meta property="product:condition" content="new"/>';
    	echo '<meta property="product:price:currency" content="RON"/>';
    	echo '<meta property="product:price:amount" content="' . $product->get_price() . '"/>';
    	echo '<meta property="product:retailer_item_id" content="' . $product->get_sku() . '"/>';
    
    	//Twitter card
    	echo '<meta name="twitter:card" content="summary" />';
    	echo '<meta name="twitter:description" content="' . $excerpt . '" />';
    	echo '<meta name="twitter:title" content="' . get_the_title() . '" />';
       
    	echo '<meta property="og:site_name" content="Uzina de Carte"/>';
    
    	//Create a default image if none exists
    	if(!has_post_thumbnail( $post->ID )) {
    		$default_image="https://www.uzinadecarte.ro/wp-content/uploads/2016/04/logo-udc-septmbriey.png"; 
    		echo '<meta property="og:image" content="' . $default_image . '"/>';
    		echo '<meta name="twitter:image" content="' . $default_image . '" />';
    	}
        else{
    		$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
    		echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
    		echo '<meta name="twitter:image" content="' . esc_attr( $thumbnail_src[0] ) . '" />';
    	}
    
    }
    add_action( 'wp_head', 'facebook_open_graph', 5 );

    I tried running that function locally on my site and it worked on a single product, but not outside of that. I tried changing the

    
    if ( !is_singular())  //if it is not a post or a page
    

    to

    
    if ( !is_singular( 'product' )) //if it is not a product
    

    That way it would exit the function if this wasn’t a product. That seemed to remove the error I was seeing on pages.

    See if that makes a difference for you.

    Thread Starter radupl

    (@radupl)

    That was it, it works now. I only tested it on product pages and didn’t realise the problem was when the code ran on the others.

    Thank you so much, I wouldn’t have managed without your help! Keep up the amazing work! ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to get product price or sku’ is closed to new replies.