phpwordpressimagewoocommerceproduct

Disable original image crop in WooCommerce


Then there is a portrait image of all the products, and according to the design of my themes I have to show all the images in a square box.So all the images are cropped and displayed.

How to disable original image crop in woocommerce?

Because I have a problem with cropping WooCommerce process:

Original portrait image

enter image description here

Woocommerce Cropped Image such as:

enter image description here

So how can I fix this problem, like the original image, stop cropping for thumbnails resized by WooCommerce?


Solution

  • Go to woocommerce > settings > products (tab) > Display (sub-tab)

    Path to products display

    Then at the bottom of the page, in "Product Images, disable Hard Crop options and save changes:

    products images settings

    Then you will need to regenerate your products images with Regenerate Thumbnails plugin:


    WITH PHP ALTERNATIVE:

    Sometimes in some themes, this is settings are hard coded. So you can change them with this code snippet, pasting it in the function.php file of your active child theme or theme:

    function yourtheme_woocommerce_image_dimensions() {
        global $pagenow;
    
        if ( ! isset( $_GET['activated'] ) || $pagenow != 'themes.php' ) {
            return;
        }
        $catalog = array(
            'width'     => '300',   // px
            'height'    => '300',   // px
            'crop'      => 0 // Disabling Hard crop option.
        );
        $single = array(
            'width'     => '150',   // px
            'height'    => '150',   // px
            'crop'      => 0 // Disabling Hard crop option.
        );
        $thumbnail = array(
            'width'     => '90',   // px
            'height'    => '90',   // px
            'crop'      => 0 // Disabling Hard crop option.
        );
        // Image sizes
        update_option( 'shop_catalog_image_size', $catalog );       // Product category thumbs
        update_option( 'shop_single_image_size', $single );      // Single product image
        update_option( 'shop_thumbnail_image_size', $thumbnail );   // Image gallery thumbs
    }
    add_action( 'after_switch_theme', 'yourtheme_woocommerce_image_dimensions', 1 );
    

    You can comment/uncomment the code (or remove some portions) to feet your needs. This code will overwrite define options in WooCommerce settings > Products > Display (Product Images).

    ACTIVATION: You will need to switch your active theme to another and then switch back to activate it.

    You also might need to regenerate product images with Regenerate Thumbnails plugin...