Last active
April 4, 2024 13:47
-
-
Save marcusig/0f337fcc2f91f9990b9937908a8b460e to your computer and use it in GitHub Desktop.
Override product image when loading a preset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'wp_get_attachment_image', function( $html, $attachment_id, $size, $icon, $attr ) { | |
global $product; | |
static $doing_filter = false; | |
if ( $doing_filter ) return $html; | |
if ( ! isset( $_REQUEST[ 'load-preset' ] ) ) return $html; | |
$doing_filter = true; | |
$post = get_post( (int) $_REQUEST[ 'load-preset' ] ); | |
if ( ! $post || 'preset' !== $post->post_status || ! class_exists( 'Mkl_PC_Preset_Configuration' ) ) return $html; | |
$preset = new Mkl_PC_Preset_Configuration( (int) $_REQUEST[ 'load-preset' ] ); | |
if ( is_a( $product, 'WC_Product' ) && (int)$product->get_image_id() === (int)$attachment_id ) { | |
$preset_post = $preset->get_the_post(); | |
if ( $preset_post->post_parent != $product->get_id() ) { | |
$doing_filter = false; | |
return $html; | |
} | |
if ( isset( $attr['srcset'] ) ) unset( $attr['srcset'] ); | |
if ( $image = $preset->get_image( 'woocommerce_single', $attr, false ) ) { | |
$doing_filter = false; | |
return $image; | |
} | |
} | |
$doing_filter = false; | |
return $html; | |
}, 2000, 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment