Forum Replies Created

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

    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 );
    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

    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)

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