After recent Woocommerce update on some website the PHP notice is appeared, like:

PHP Notice:  Undefined index: width in plugins/woocommerce/includes/class-wc-regenerate-images.php on line 276
PHP Notice:  Undefined index: height in plugins/woocommerce/includes/class-wc-regenerate-images.php on line 277

This problem is caused by SVG-files mostly. So when in Woocommerce fucntion such mime type is checked some SVG images can has such structure, which call the php notice:

Array
(
    [filesize] => 4427
)

How to fix

Add this to you functions.php or other included to theme file:


/**
* Disable Woocommerce resize for SVG images.
*/
add_filter( 'wp_get_attachment_image_src', 'fix_wp_get_attachment_image_svg', 10, 4 );

function fix_wp_get_attachment_image_svg($image, $attachment_id, $size, $icon) {
    // if Woocommerce enabled
    if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
        $attachment = get_post($attachment_id);
        $mime_type = $attachment->post_mime_type; // The attachment mime_type
        if($mime_type == 'image/svg+xml') {
            return false;                
        }
    }
    return $image;
}

And probably you need to exclude admin area, because if you set some SVG via ACF fields it would not be visisble in settings.

/**
* Disable Woocommerce resize for SVG images.
*/
add_filter( 'wp_get_attachment_image_src', 'fix_wp_get_attachment_image_svg', 10, 4 );

function fix_wp_get_attachment_image_svg($image, $attachment_id, $size, $icon) {
    // if Woocommerce enabled
    if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {

        if ( ! is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
            $attachment = get_post($attachment_id);
            $mime_type = $attachment->post_mime_type; // The attachment mime_type
            if($mime_type == 'image/svg+xml') {
                return false;                
            }
        }
    }
    return $image;
}

2 Comments

Leave a Reply

Your email address will not be published.