Extracting a WooCommerce Attribute and Put it in Google Book Viewer Script

I’m going to use Google books embedded viewer in my website which runs using WordPress CMS and Woocommerce plugin.

My idea is that one Woocommerce attribute for each product (here a book) be assigned to relevant book Google books ID (something like 0738531367), then using a method, this string be inserted to viewer script which simplest form of it follows,

Read More
<script type="text/javascript" src="http://books.google.com/books/previewlib.js"></script>
<script type="text/javascript">
GBS_insertEmbeddedViewer('4rghAwAAQBAJ',600,500);
</script>

Now, how can I extract this string (attribute) from Woocommerce and put it in the viewer script?


More Specific Explanation

This is a book page (a WooCommerce product page).

The book this page offers, has been given an attribute with the value of Google Books ID. It can be found at the lowest row of “مشخصات کتاب” tab.

After inserting this ID into the above code and putting the whole modified code in book page HTML code, the famous Google Books Embedded Viewer corresponding to this book, appeared in the “توضیح ناشر” tab.

But this process is time-consuming and practically impossible when I’m faced with huge number of books to be added.

It is quite simple to embed the Viewer code in the WooCommerce (or theme) TEMPLATE page ONCE. So, for each books the only effort is to defining the so-called book ID in an predefined attribute.

After all, one task remains: transferring this ID to the Viewer code. I think it can be handled using a WordPress hook, php function or the like. Unfortunately searching the net yielded no results (including Google Books Support Forum and WordPress support directory).

Related posts

Leave a Reply

1 comment

  1. It’s possible to integrate into WC product meta box (also, see docs), but I’ll show how to do it with a normal meta box.

    <?php
    /**
     * Plugin Name: (SO) Book ID
     */
    
    add_action( 'add_meta_boxes', 'add_box_so_26564103' );
    add_action( 'save_post', 'save_so_26564103', 10, 2 );
    
    function add_box_so_26564103() {
        add_meta_box( 
            'sectionid',
            'Book ID',
            'inner_box_so_26564103',
            'product',
            'side',
            'low'
        );
    }
    
    function inner_box_so_26564103($post)
    {
        wp_nonce_field( plugin_basename( __FILE__ ), 'noncename' );
        $saved = get_post_meta( $post->ID, '_book_id', true);
    
        printf(
            '<label>Enter the ID <input type="text" name="_book_id" value="%s" /></label>',
            $saved ? $saved : ''
        );
    
    }
    
    function save_so_26564103( $post_id, $post_object ) 
    {
          if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
              return;
    
          if ( !wp_verify_nonce( $_POST['noncename'], plugin_basename( __FILE__ ) ) )
              return;
    
        if ( 'revision' == $post_object->post_type )
            return;
    
        if ( isset( $_POST['_book_id'] )  )
            update_post_meta( $post_id, '_book_id', $_POST['_book_id'] );
        else 
            delete_post_meta( $post_id, '_book_id' );
    
    }
    

    Then, in your template, just retrieve the meta data:

    <script type="text/javascript" src="http://books.google.com/books/previewlib.js"></script>
    <script type="text/javascript">
    GBS_insertEmbeddedViewer('<?php echo get_post_meta( $post->ID, "_book_id", true); ?>',600,500);
    </script>