I am trying to get all small medium large and full thumbnail sizes in the SHOP page of woocommerce. I am using the following code in woocommerce.php (duplicate of page.php) to display all products
<?php woocommerce_content(); ?>
Next I have put the following code in functions.php but all in vain
<?php
/**
* Hook in on activation
*/
/**
* Define image sizes
*/
function yourtheme_woocommerce_image_dimensions() {
global $pagenow;
if ( ! isset( $_GET['activated'] ) || $pagenow != 'themes.php' ) {
return;
}
$thumbnail = array(
'thumbnail' , 'medium' , 'large' , 'full'
);
$index = array_rand($sizes);
// Image sizes
// Single product image
update_option( 'shop_thumbnail_image_size', $sizes[$index] ); // Image gallery thumbs
}
add_action( 'after_switch_theme', 'yourtheme_woocommerce_image_dimensions', 1 );
?>
I am new to woocommerce and I wish to achieve a masonry layout with these different thumbnail sizes. Any help would be greatly appreciated.
Thank You in advance.
Try following code:
<?php
/**
* Hook in on activation
*/
/**
* Define image sizes
*/
function yourtheme_woocommerce_image_dimensions() {
global $pagenow;
if ( ! isset( $_GET['activated'] ) || $pagenow != 'themes.php' ) {
return;
}
$thumbnail = array(
'thumbnail' , 'medium' , 'large' , 'full'
);
$index = array_rand($thumbnail);
// Image sizes
// Single product image
update_option( 'shop_thumbnail_image_size', $thumbnail[$index] ); // Image gallery thumbs
}
add_action( 'after_switch_theme', 'yourtheme_woocommerce_image_dimensions', 1 );
?>
Related
How do I set a default product thumbnail on WooCommerce even if there is an existing product image on a product, but will not replace or change the existing one on the database? Hope someone can help, thank you in advance.
All right so let me explain the solution for you. You have to remove product loop original image thumbnail with the remove action. Then you have to insert new action which is calling the custom thumbnail. Don't forget to replace URL OF YOUR IMAGE HERE with your image URL. For a single product you have to do the same. This code goes into functions.php file of your theme. Tested and works.
//Product loop
function custom_image_woo() {
// Remove original product image from product loop
remove_action( 'woocommerce_before_shop_loop_item_title', 'custom_image_woo', 10 );
// Your custom image thumbnail function
function here_is_the_magic() {
echo '<img src="URL OF YOUR IMAGE HERE">';
}
add_action( 'woocommerce_before_shop_loop_item_title', 'here_is_the_magic', 10 );
}
add_action( 'woocommerce_init', 'custom_image_woo');
// Single product page
//this removes product featured image
add_filter('woocommerce_single_product_image_thumbnail_html', 'main_image_away', 10, 2);
function main_image_away($html, $attachment_id ) {
global $post, $product;
$featured_image = get_post_thumbnail_id( $post->ID );
if ( $attachment_id == $featured_image )
$html = '';
return $html;
}
//this will add your image before gallery
function gallery(){
echo '<img src="URL OF YOUR IMAGE HERE">';
}
add_action( 'woocommerce_product_thumbnails', 'gallery', 10 );
This one worked for me:
function alterImageSRC($image, $attachment_id, $size, $icon) {
$image[0] = 'http://newimagesrc.com/myimage.jpg';
return $image;
}
add_filter('wp_get_attachment_image_src', 'alterImageSRC', 10, 4);
I work on slider for woocommerce product gallery and I don't use/need the generated thumbnails, because I use "dots" for navigation and I want to hide/unload the generated thumbnails by woocommerce gallery.
Firstly, I put this in my theme functions.php file :
remove_theme_support( 'wc-product-gallery-slider' );
And actually, I just put "display:none" in my css file and I make this for the product-thumbnails.php :
// Note: `wc_get_gallery_image_html` was added in WC 3.3.2 and did not exist prior. This check protects against theme overrides being used on older versions of WC.
if ( ! function_exists( 'wc_get_gallery_image_html' ) ) {
return;
}
global $post, $product;
$attachment_ids = $product->get_gallery_image_ids();
if ( $attachment_ids && $product->get_image_id() ) { ?>
<div class="slider product-responsive-thumbnail" id="product_thumbnail_<?php echo esc_attr( $post->ID ); ?>">
<?php foreach ( $attachment_ids as $attachment_id ) { ?>
<div class="thumbnail-wrapper">
<?php echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', wc_get_gallery_image_html( $attachment_id ), $attachment_id ); // phpcs:disable WordPress.XSS.EscapeOutput.OutputNotEscaped ?>
</div>
<?php
} ?>
</div>
<?php
}
?>
I would like to disable the generation of thumbnails and its display completely to optimize the loading of my page!
I know that I can totally delete the contents of the file "produtc-thumbnails.php" but it is a bit raw method and I would like to know if this is possible with another less raw method
As #7uc1f3r suggested to me,
First simple solution. Mask in the woocommerce-general.css file like this:
.woocommerce div.product div.images .flex-control-thumbs {
overflow: hidden;
zoom: 1;
margin: 0;
padding:0;
display:none; /* this hide the thumbnails */
}
Second solution, put "false" in the product-thumbnails.php
if ( ! function_exists( 'wc_get_gallery_image_html' ) ) {
return;
}
global $post, $product;
$attachment_ids = false; // This disable the thumbnails
And voilĂ , now it's up to you to choose the method you want to keep.
Good I was wondering if i could remove product thumbnail from single product page is product has only one image (i.e the product image only).. So that when user are viewing the product with only one image, they dont need to see the thumbnail but products with product image and Product gallery images, the thumbnail can show up.
Is there a way to achieve this?
I have tried the below but didn't work for me (though the code is to remove the thumbnail entirely);
function remove_gallery_thumbnail_images() {
if ( is_product() ) {
remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
}
}
add_action('loop_start', 'remove_gallery_thumbnail_images');
How can i achieve this? disable the thumbnail if product has only one image but display thumbnails if product has multiple images.
Any help is very welcome.
Normally woocommerce doesn't show the gallery when there is no thumbnails in it.
In your case, you can try to use the following:
add_action( 'woocommerce_product_thumbnails', 'enable_gallery_for_multiple_thumbnails_only', 5 );
function enable_gallery_for_multiple_thumbnails_only() {
global $product;
if( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( get_the_id() );
}
if( empty( $product->get_gallery_image_ids() ) ) {
remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
}
}
Or if the image is included as a thumbnail in the gallery you can replace in the function:
if( empty( $product->get_gallery_image_ids() ) ) {
by the following line:
if( sizeof( $product->get_gallery_image_ids() ) == 1 ) {
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
You can also hide the gallery with an inline CSS:
add_action( 'woocommerce_before_single_product_summary', 'enable_gallery_for_multiple_thumbnails_only', 5 );
function enable_gallery_for_multiple_thumbnails_only() {
global $product;
if( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( get_the_id() );
}
if( empty( $product->get_gallery_image_ids() ) ) {
echo '<style> ol.flex-control-thumbs { display:none; } </style>';
}
}
Or if the image is included as a thumbnail in the gallery you can replace in the function:
if( empty( $product->get_gallery_image_ids() ) ) {
by the following line:
if( sizeof( $product->get_gallery_image_ids() ) == 1 ) {
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
All this works on themes that doesn't make related customizations.
I am using the Wordpress plugin "Woocommerce Product Image Flipper" to flip images on hover.
It works perfectly on a product category page, but the not on the homepage.
How do I make the images flip on the homepage?
I am displaying the Homepage Template and it shows the following sections:
"New In" , "On Sale", "Bestsellers"
When I hover over them, I would like the images to flip like they do on the product category pages.
The code of the plugin is as follows:
<?php
/*
Plugin Name: WooCommerce Product Image Flipper
Plugin URI:
Version: 0.4.0
Description: Adds a secondary image on product archives that is revealed on hover. Perfect for displaying front/back shots of clothing and other products.
Author: jameskoster
Author URI:
Text Domain: woocommerce-product-image-flipper
Domain Path: /languages/
License: GNU General Public License v3.0
License URI:
*/
/**
* Check if WooCommerce is active
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
/**
* Localisation (with WPML support)
*/
add_action( 'init', 'plugin_init' );
function plugin_init() {
load_plugin_textdomain( 'woocommerce-product-image-flipper', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Image Flipper class
*/
if ( ! class_exists( 'WC_pif' ) ) {
class WC_pif {
public function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'pif_scripts' ) );
add_action( 'woocommerce_before_shop_loop_item_title', array( $this, 'woocommerce_template_loop_second_product_thumbnail' ), 11 );
add_filter( 'post_class', array( $this, 'product_has_gallery' ) );
}
/**
* Class functions
*/
public function pif_scripts() {
if ( apply_filters( 'woocommerce_product_image_flipper_styles', true ) ) {
wp_enqueue_style( 'pif-styles', plugins_url( '/assets/css/style.css', __FILE__ ) );
}
}
public function product_has_gallery( $classes ) {
global $product;
$post_type = get_post_type( get_the_ID() );
if ( ! is_admin() ) {
if ( $post_type == 'product' ) {
$attachment_ids = $this->get_gallery_image_ids( $product );
if ( $attachment_ids ) {
$classes[] = 'pif-has-gallery';
}
}
}
return $classes;
}
/**
* Frontend functions
*/
public function woocommerce_template_loop_second_product_thumbnail() {
global $product, $woocommerce;
$attachment_ids = $this->get_gallery_image_ids( $product );
if ( $attachment_ids ) {
$attachment_ids = array_values( $attachment_ids );
$secondary_image_id = $attachment_ids['1'];
$secondary_image_alt = get_post_meta( $secondary_image_id, '_wp_attachment_image_alt', true );
$secondary_image_title = get_the_title($secondary_image_id);
echo wp_get_attachment_image(
$secondary_image_id,
'shop_catalog',
'',
array(
'class' => 'secondary-image attachment-shop-catalog wp-post-image wp-post-image--secondary',
'alt' => $secondary_image_alt,
'title' => $secondary_image_title
)
);
}
}
/**
* WooCommerce Compatibility Functions
*/
public function get_gallery_image_ids( $product ) {
if ( ! is_a( $product, 'WC_Product' ) ) {
return;
}
if ( is_callable( 'WC_Product::get_gallery_image_ids' ) ) {
return $product->get_gallery_image_ids();
} else {
return $product->get_gallery_attachment_ids();
}
}
}
$WC_pif = new WC_pif();
}
}
I'm trying to add images sizes on my wordpress site. I searched alot and tried too many solutions but couldn't work for me can anyone help me on this, it will be appreciatable. Thanks in advance. I have added this code in my functions.php file
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'slider_image_desktop' => __( 'slider_image_desktop' ),
) );
}
add_action( 'after_setup_theme', 'wpse_setup_theme' );
function wpse_setup_theme() {
add_theme_support( 'post-thumbnails' );
add_image_size( 'slider_image_desktop2', 60, 60, true );
}
Thanks everyone i solved my problem. I changed my some lines of code to
add_action( 'after_setup_theme', 'wpse_setup_theme' );
function wpse_setup_theme() {
add_theme_support( 'post-thumbnails' );
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'slider_image_desktop2', 60, 60, true );
}
}
It made the thumbnail then for retrieving i didn't use function for get image thumb because i am using Advanced Custom Fields so in wp admin in my acf fields group i just checked my image uploader's return value as image array and i just got full array of the uploaded image with my defined size. there was no need to use wp_get_attachment_image_src or wp_get_attachment_image function to fetch the image..
Thanks everyone for your help may this answer will help someone who will have been the same problem...
Try This:
$attachment_id = get_field('field_name');
$size = "slider_image_desktop2"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img class="image-class" alt="" src="<?php echo $image[0]; ?>" />
<?php
just copy and paste on your theme function.php file
add_image_size( 'img-cropped', 198, 198, true );// you can change the size
Your selected self answer was an absolute life saver, and got me to the point of what I was trying to do - but I had to make one small addition to get the custom image size in the backend page editor "add media" options:
function nmg_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'social-icons' => __('Social Icon'),
) );
}
add_action( 'after_setup_theme', 'nmg_setup_theme' );
function nmg_setup_theme() {
add_theme_support( 'post-thumbnails' );
if ( function_exists( 'add_image_size' ) ) {
add_image_size('social-icons', 9999, 60, false);
}
add_filter( 'image_size_names_choose', 'nmg_custom_sizes' );
}
It's your function, but with the function to add the custom size to image_size_names_choose - but after theme setup (what was stopping it from working for me before).
Might be useful to someone...