提取WooCommerce属性并将其放在Google Book Viewer脚本中 [英] Extracting a WooCommerce Attribute and Put it in Google Book Viewer Script

查看:77
本文介绍了提取WooCommerce属性并将其放在Google Book Viewer脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在我的网站中使用Google图书嵌入式查看器,该查看器通过 Wordpress CMS和 Woocommerce 插件运行.

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

我的想法是,将每个产品(这里是一本书)的Woocommerce属性分配给相关的图书Google图书ID(类似于0738531367),然后使用一种方法,将此字符串插入查看器脚本,该脚本的最简单形式如下,

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,

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

现在,如何从Woocommerce中提取此字符串(属性)并将其放入查看器脚本中?

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

更具体的解释

这是一本书页面(WooCommerce产品页面).

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

此页面提供的图书已被赋予一个属性,其值为Google图书ID.可以在مشخصاتکتاب"选项卡的最低行中找到.

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.

将此ID插入上述代码并将整个修改后的代码放入书页HTML代码后,与此书相对应的著名Google Books Embedded Viewer将出现在توضیحناشر"标签中.

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.

将查看器代码嵌入WooCommerce(或主题)模板页面一次中非常简单.因此,对于每本书,唯一的工作就是在预定义的属性中定义所谓的书ID.

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.

毕竟,还有一项任务:将这个ID传输到Viewer代码.我认为可以使用Wordpress挂钩,php函数等来处理.不幸的是,在网上搜索没有任何结果(包括Google图书支持论坛和Wordpress支持目录).

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

推荐答案

它是

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>

这篇关于提取WooCommerce属性并将其放在Google Book Viewer脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆