在 Woocommerce 3 中更改缩放放大镜产品图像大小

Change zoom magnifier product image size in Woocommerce 3

我花了很长时间才将放大图片或灯箱图片设置为不同的(自定义)尺寸,而不是上传的完整尺寸图片。

我发现是wc-template-functions.php中1399行的这行代码控制的:

$full_size         = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' ) );

我试图创建一个函数来将 'full' 更改为“预览”或 'large',但我的代码无法正常工作。请参阅下面的代码:

function change_magnifier_lightbox_image_size(){
  echo "De post" , $post;

  $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
  echo "De ID ", $post_thumbnail_id;
  $filemeta = wp_get_attachment_metadata( $post_thumbnail_id, FALSE );
  echo "De filemeta ", $filemeta;

    if ($filemeta['width']>3071 || $filemeta['height']>3071){
      $full_size         = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'preview' ) );
    }else{
      $full_size         = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'large' ) );
    };
};
apply_filters( 'woocommerce_product_thumbnails_large_size', 'change_magnifier_lightbox_image_size' );

$post 设置为全局。

我根本没有得到任何回声,也没有从字符串中得到任何回声,代码在我的子主题中 functions.php。我错过了什么?

我相信您可能需要解除默认的 WooCommerce 操作

remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20);

然后输入代码。

应该是add_filter不是apply_filters

add_filter( 'woocommerce_product_thumbnails_large_size', 'change_magnifier_lightbox_image_size' );

并且您必须 return 过滤器中的值,检查 add_filter() https://developer.wordpress.org/reference/functions/add_filter/

您的代码中存在一些错误。请尝试以下操作:

add_filter( 'woocommerce_gallery_full_size', 'change_magnifier_lightbox_image_size', 20, 1 );
function change_magnifier_lightbox_image_size( $size ){
    $thumbnail_id = get_post_thumbnail_id( get_the_id() );
    $attachment   = wp_get_attachment_metadata( $thumbnail_id, FALSE );

    // Always return a value in a filter hook
    return ( $attachment['width'] > 3071 || $attachment['height'] > 3071 ) ? 'preview' : 'large';
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。