Woocommerce 产品图片替换为图库插件

Woocommerce product images replaced with a gallery plugin

首先我想分析一下我的问题。使用 Wordpress/Woocommerce 我需要在产品图库中的图片旁边添加视频。 Woocommerce 根本不支持视频。

所以,我想安装一个额外的支持图像和视频的图库插件。

现在,我想将特定 image/video 图库 collection 映射到特定产品。我还想在不属于描述或简短描述等标准文本字段的新区域中查看此图库 collection。让我们说上面的主要产品图片。代表画廊 collection id=1 的 php 代码如下所示:

<?php echo do_shortcode('[wonderplugin_gallery id="1"]'); ?>

问题是我需要画廊 collection id 是可变的,像这样:

<?php echo do_shortcode('[wonderplugin_gallery id="X"]'); ?>

其中 X 代表特定画廊 collection。如何将图库 collection ID XXXX 连接到我的产品页面 XXXXX?

我有编程技能,但我对 wordpress 代码逻辑不熟悉。

关于我的问题的任何其他建议,例如可以用更好的产品库替换默认产品库的插件?

此致,

我要么按照 Anand 的建议使用产品自定义字段,要么创建一个包含必要输入字段(或下拉列表,具体取决于您如何使用图库插件)的元数据框。

首先我会创建一个 metabox,然后在那个元数据框中我会从插件中提取信息(画廊 ID 和名称)。从中你可以创建一个下拉菜单。您应该能够 select 像您建议的那样从该元数据框中获取每个产品的 ID。例如这样的事情可以工作:

<?php

if ( ! function_exists( 'product_add_meta' ) ){
    function product_add_meta(){
        add_meta_box("gallery_dropdown", "Select Gallery", "product_gallery_meta_box", "product");
    }
}
add_action("admin_init", "product_add_meta");

if ( ! function_exists( 'product_gallery_meta_box' ) ){
    function product_gallery_meta_box( $post ){
        $post_types = array('product');     //limit meta box to certain post types
        global $post;
        $product = get_product( $post->ID );
        $values = get_post_custom( $post->ID );
        $gallery = (isset($values['gallery'][0])) ? $values['gallery'][0] : '';
        wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
        ?>
        <p>
            <select name="gallery" id="gallery">
            //example of how the option should look
            <option value="<?php echo $gallery_id; ?>" <?php selected( $gallery, $gallery_id ); ?>><?php echo $gallery_name; ?></option>
                <?php
                //pull options from plugin here and create an option dropdown with foreach
                ?>
            </select>
        </p>
        <?php
    }
}

if ( ! function_exists( 'product_gallery_save_meta_box' ) ){
    function product_gallery_save_meta_box( $post_id ){
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
            return;
        }
        if( !isset( $_POST['gallery'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) {
            return;
        }
        if( !current_user_can( 'edit_pages' ) ) {
            return;
        }
        if( isset( $_POST['gallery'] ) ){
            update_post_meta( $post_id, 'gallery', wp_kses( $_POST['gallery'] ,'') );
        }
    }
}
add_action( 'save_post', 'product_gallery_save_meta_box' );

如果你把它放在 functions.php 中,它应该会在你的 woocommerce 产品页面上显示一个名为 'Select Gallery' 的元数据框,其中有一个空的下拉列表。

我没有填写您从用于创建画廊的插件中获得的选项,但应该不会太难。

一种方法是绑定产品页面 ID 和图库 ID。如果您可以更改图库的 ID,则将其更改为与产品页面的 ID 相匹配。现在您可以使用这两个示例中的任何一个创建短代码。

// outside the loop use global ( uncomment appropriate statement )
// global $product; 
// global $post;

do_shortcode( sprintf( '[wonderplugin_gallery id="%d"]', $product->id ) );
do_shortcode( sprintf( '[wonderplugin_gallery id="%d"]', $post->ID ) );

HERE 是一个 link 的插件,可以显示管理页面上的大部分 ID。

另一个是创建 Custom Field ( post meta ) in edit product admin page ( named gallery_id for example ), and to save there id of the gallery to use. To create shortcode use get_post_meta() 函数来检索保存的 post 元数据。

do_shortcode( sprintf( '[wonderplugin_gallery id="%d"]', get_post_meta( $post->ID, 'gallery_id', true ) ) );

要获取图库 ID 元数据,请使用 $post->ID$product->idget_the_ID() 函数,后者仅在循环内。