Skip to content

Commit 31a85ab

Browse files
Ensure filters are loaded only on the is_cart page
1 parent d8a38b1 commit 31a85ab

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

inc/compatibilities/woocommerce.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function should_load() {
2424
public function register() {
2525
if ( Optml_Main::instance()->admin->settings->use_lazyload() ) {
2626
add_filter( 'optml_lazyload_early_flags', [ $this, 'add_lazyload_early_flag' ], PHP_INT_MAX, 1 );
27-
if ( class_exists( '\Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils' ) && \Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils::is_cart_block_default() ) {
28-
add_filter( 'image_downsize', [ Optml_Tag_Replacer::instance(), 'filter_image_downsize' ], PHP_INT_MAX, 3 );
27+
if ( class_exists( '\Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils' ) && \Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils::is_cart_block_default() && function_exists( 'wc_cart_totals_subtotal_html' ) ) {
28+
add_filter( 'image_downsize', [ $this, 'filter_cart_item_image' ], PHP_INT_MAX, 3 );
2929
}
3030
}
3131
}
@@ -80,4 +80,20 @@ public function should_load_early() {
8080
}
8181
return false;
8282
}
83+
84+
/**
85+
* This filter will replace all the images retrieved via "wp_get_image" type of functions.
86+
*
87+
* @param array $image The filtered value.
88+
* @param int $attachment_id The related attachment id.
89+
* @param array|string $size This could be the name of the thumbnail size or an array of custom dimensions.
90+
*
91+
* @return array
92+
*/
93+
public function filter_cart_item_image( $image, $attachment_id, $size ) {
94+
if ( ! is_cart() ) {
95+
return $image;
96+
}
97+
return Optml_Tag_Replacer::instance()->filter_image_downsize( $image, $attachment_id, $size );
98+
}
8399
}

0 commit comments

Comments
 (0)