Friday 15 May 2015

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



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

i'm going utilize google books embedded viewer in website runs using wordpress cms , woocommerce plugin.

my thought 1 woocommerce attribute each product (here book) assigned relevant book google books id (something 0738531367), using method, string inserted viewer script simplest form of follows,

<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 extract string (attribute) woocommerce , set in viewer script?

more specific explanation

this book page (a woocommerce product page).

the book page offers, has been given attribute value of google books id. can found @ lowest row of "مشخصات کتاب" tab.

after inserting id above code , putting whole modified code in book page html code, famous google books embedded viewer corresponding book, appeared in "توضیح ناشر" tab.

but process time-consuming , practically impossible when i'm faced huge number of books added.

it quite simple embed viewer code in woocommerce (or theme) template page once. so, each books effort defining so-called book id in predefined attribute.

after all, 1 task remains: transferring id viewer code. think can handled using wordpress hook, php function or like. unfortunately searching net yielded no results (including google books back upwards forum , wordpress back upwards directory).

it's possible integrate wc product meta box (also, see docs), i'll show how 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 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 template, retrieve 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>

php wordpress api woocommerce

No comments:

Post a Comment