douchui4459 2016-09-06 11:48
浏览 52

Woocommerce变化产品图像未显示在电子邮件中

I'm using Woocommerce with Dynamic Gallery PRO. The images on the product pages work fine, and I also managed to display a product image in the emails sent to admin and customer. However, it's the main product image that's showing, not the variation image. As I understand the Gallery plugin uses the first image for the checkout page to display.

I already turned '$show_image' to true in email-order-details.php in Woocommerce.

The document of code I should edit (I think) is as following:

<?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, '_', "
" ) ) . '</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; ?>

The file of Gallery PRO where it defines images is as following:

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
} }

Someone familiar with this issue? I'm not really a programmer but I learn a lot along the way.

  • 写回答

1条回答 默认 最新

  • douzi9744 2016-09-06 11:55
    关注

    Try this one near @$show_image, Its working for me.

    Option 1:

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

    Option 2:

    echo $_product->get_image();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析