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

标签 php wordpress image woocommerce

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

我已经在 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
} }

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

最佳答案

试试@$show_image附近的这个,它对我有用。

选项 1:

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

选项 2:

echo $_product->get_image();

关于php - Woocommerce 变体产品图片未显示在订单电子邮件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39348324/

相关文章:

C#:从资源加载图像时出现 FileNotFoundException

python - 将多个 EPS 图像加载到 matplotlib 子图

php - 你如何在 wordpress 插件中使用 curl?

php - WordPress更改特色图片默认大小

php - 如何在 Wordpress 数据库的 mysql 中将行值转换为列标题

php - 为什么我的自定义密码重置链接被视为无效?

html - 使用 html 和 css 组合两个半图像

php - 使用 While 循环 php 更新 mysql 数据库

php - 控制台中出现大量 500 个内部服务器错误(Chrome)

php - SQL - 返回用户拥有的书籍