Woocommerce变体产品图片未在电子邮件中显示

时间:2016-09-06 11:48:06

标签: php wordpress image woocommerce

我正在使用Woocommerce与动态图库PRO 。产品页面上的图像工作正常,我还设法在发送给管理员和客户的电子邮件中显示产品图像。但是,它是显示的主要产品图像,而不是变化图像。据我所知,Gallery插件使用结帐页面的第一个图像来显示。

我已经在Woocommerce的email-order-details.php中将'$show_image'变为true。

我应编辑的代码文件(我认为)如下:

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

foreach ( $items as $item_id => $item ) :
    $_product     = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
    $item_meta    = new WC_Order_Item_Meta( $item, $_product );

    if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
        ?>
        <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
            <td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php

                // Show title/image etc
                **if ( $show_image ) {
                    echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . esc_attr__( 'Product Image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-right: 10px;" /></div>', $item );
                }**

                // Product name
                echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false );

                // SKU
                if ( $show_sku && is_object( $_product ) && $_product->get_sku() ) {
                    echo ' (#' . $_product->get_sku() . ')';
                }

                // allow other plugins to add additional product information here
                do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );

                // Variation
                if ( ! empty( $item_meta->meta ) ) {
                    echo '<br/><small>' . nl2br( $item_meta->display( true, true, '_', "\n" ) ) . '</small>';
                }

                // File URLs
                if ( $show_download_links ) {
                    $order->display_item_downloads( $item );
                }

                // allow other plugins to add additional product information here
                do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );

            ?></td>
            <td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item ); ?></td>
            <td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
        </tr>
        <?php
    }

    if ( $show_purchase_note && is_object( $_product ) && ( $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) ) : ?>
        <tr>
            <td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
        </tr>
    <?php endif; ?>

<?php endforeach; ?>

Gallery PRO定义图像的文件如下:

class WC_Dynamic_Gallery_Variations{

public static function wc_dgallery_change_variation() {
    $product_id   = (int) $_REQUEST['product_id'];
    $variations = wp_unslash( $_REQUEST['variations'] );
    ob_start();
    if ( $variations == '' ) {
        WC_Gallery_Display_Class::wc_dynamic_gallery_display($product_id);
    } else {
        WC_Gallery_Display_Class::get_gallery_variations($product_id, $variations);
    }
    $result = ob_get_clean();
    echo json_encode($result);
    die();
}

public static function change_image_in_cart_page( $product_image, $values, $cart_item_key ) {
    if ( is_array( $values ) && isset( $values['variation_id'] ) && $values['variation_id'] > 0 ) {
        $variation_id = $values['variation_id'];

        $dgallery_ids = WC_Dynamic_Gallery_Functions::get_gallery_ids( $variation_id );
        if ( is_array( $dgallery_ids ) && count( $dgallery_ids ) > 0 ) {
            // Use first image from variation gallery
            $img_id = (int) array_shift( $dgallery_ids );
            $product_image = wp_get_attachment_image( $img_id, 'shop_thumbnail' );
        }

    } elseif ( isset( $values['product_id'] ) && $values['product_id'] > 0 ) {
        $product_id = $values['product_id'];
        // Don't change the image if product has featured image
        if ( has_post_thumbnail( $product_id ) ) return $product_image;

        $dgallery_ids = WC_Dynamic_Gallery_Functions::get_gallery_ids( $product_id );
        if ( is_array( $dgallery_ids ) && count( $dgallery_ids ) > 0 ) {
            // Use first image from variation gallery
            $img_id = (int) array_shift( $dgallery_ids );
            $product_image = wp_get_attachment_image( $img_id, 'shop_thumbnail' );
        }
    }

    return $product_image;
}

public static function wc_dgallery_variation_save_gallery_ids() {
    if ( isset( $_POST['variation_id'] ) && $_POST['variation_id'] > 0 && isset( $_POST['dgallery_ids'] ) ) {
        $variation_id = trim( $_POST['variation_id'] );
        $dgallery_ids = array_filter( explode( ',', trim( $_POST['dgallery_ids'] ) ) );
        update_post_meta( $variation_id, '_product_image_gallery', implode( ',', $dgallery_ids ) );
    }
    die();
}

public static function add_gallery_variation( $loop, $variation_data, $variation ) {
    $global_wc_dgallery_activate  = get_option( WOO_DYNAMIC_GALLERY_PREFIX.'activate' );
    $actived_d_gallery            = get_post_meta( $variation->post_parent, '_actived_d_gallery',true );

    if ( $actived_d_gallery == '' && $global_wc_dgallery_activate != 'no' ) {
        $actived_d_gallery = 1;
    }

    $default_show_variation = get_option( WOO_DYNAMIC_GALLERY_PREFIX.'show_variation' );
    $show_variation         = get_post_meta($variation->post_parent, '_wc_dgallery_show_variation',true);
    if ( $show_variation == '' ) {
        $show_variation = $default_show_variation;
    }
    if ( $show_variation == 1 || $show_variation == 'yes' ) {
        $show_variation = 1 ;
    }
?>
    <div class="variations_dgallery_activated_panel_container a3-metabox-panel-wrap a3-dynamic-metabox-panel-wrap" style="<?php if ( 1 != $actived_d_gallery ) { echo 'display: none;'; } ?> padding-left: 0px;">

        <div class="a3-metabox-panel a3-metabox-wrapper">

            <div id="variations_dgallery_panel" class="a3-metabox-items" style="<?php if ( 1 != $show_variation ) { echo 'display: none;'; } ?>">

                <div class="dgallery_images_container dgallery_variation_images_container a3-metabox-options-panel">
                    <h4 style="margin:0;">
                        <?php echo __( 'Variation Gallery', 'woo_dgallery' ); ?>
                        <span class="a3_dg_variation_ajax_loader" style="display: none;"><img class="" src="<?php echo WOO_DYNAMIC_GALLERY_IMAGES_URL; ?>/ajax-loader.gif" /></span>
                    </h4>

                    <ul class="dgallery_images">
                        <?php
                            $variation_id = $variation->ID;
                            $dgallery_ids = WC_Dynamic_Gallery_Functions::get_gallery_ids( $variation_id );
                            if ( is_array( $dgallery_ids ) && count( $dgallery_ids ) > 0 ) {
                                foreach ( $dgallery_ids as $img_id ) {
                                    $img_data = wp_get_attachment_image_src( $img_id, 'thumbnail' );
                        ?>
                        <li class="image" data-attachment_id="<?php echo $img_id ; ?>">
                            <img class="image_item" src="<?php echo $img_data['0']; ?>" />
                            <ul class="actions">
                                <li><a href="#" class="delete dg_tips" data-tip="<?php echo __( 'Delete image', 'woo_dgallery' ); ?>"><?php echo __( 'Delete image', 'woo_dgallery' ); ?></a></li>
                            </ul>
                        </li>
                        <?php
                                }
                            }
                        ?>
                    </ul>

                    <input type="hidden" class="dgallery_ids" data-variation-id="<?php echo $variation_id; ?>" value="<?php if ( $dgallery_ids ) echo esc_attr( implode( ',', $dgallery_ids ) ); ?>" />

                    <p class="add_dgallery_images hide-if-no-js" style="margin: 10px 0px;">
                        <a href="#" data-choose="<?php _e( 'Add Images to Dynamic Gallery', 'woo_dgallery' ); ?>" data-update="<?php _e( 'Add to gallery', 'woo_dgallery' ); ?>" data-delete="<?php _e( 'Delete image', 'woo_dgallery' ); ?>" data-text="<?php _e( 'Delete', 'woo_dgallery' ); ?>"><?php _e( 'Add variation gallery images', 'woo_dgallery' ); ?></a>
                    </p>
                </div>

            </div>
        </div>

        <?php
        // Add an nonce field so we can check for it later.
        //wp_nonce_field( 'a3_dynamic_variation_metabox_action', 'a3_dynamic_variation_metabox_nonce_field' );
        ?>
        <div style="clear: both;"></div>

    </div>
<?php
} }

有人熟悉这个问题吗?我不是一个真正的程序员,但我一路上学到了很多东西。

1 个答案:

答案 0 :(得分:2)

@ $ show_image 附近试试这个,它为我工作。

选项1:

echo $order->email_order_items_table( true, false, true, true, array(80,48) );

选项2:

echo $_product->get_image();