I develope a woo-commerce website all product image is shown but these images are not shown full size in Cart page, and I want that product images are shown full size, please help me?
$thumbnail = apply_filters( 'get_the_post_thumbnail_url', $_product->get_image(), $cart_item, $cart_item_key, 'full' );
if ( ! $product_permalink ) {
echo $thumbnail;
} else {
printf( '%s', esc_url( $product_permalink ), $thumbnail );
}
Try this code.
You need to add full in $_product->get_image('full')
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image('full'), $cart_item, $cart_item_key );
if ( ! $product_permalink ) {
echo $thumbnail; // PHPCS: XSS ok.
} else {
printf( '%s', esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
}
?>
Related
I'm using the filter woocommerce_add_to_cart_redirect to redirect users to a custom page when they click the "Add to Cart" button in WooCommerce.
This function works to redirect to '/opie'
function xc_add_to_cart_redirect_to_opie() {
global $woocommerce;
// Remove the default `Added to cart` message
wc_clear_notices();
$url = get_site_url() . '/opie/';
return $url;
}
The problem I am having is that I want to get properties of the currently being added/modified cart item. I will use this information in an http request I send to another service.
I thought I would be able to do this using the Cart object and getting the last item in the cart array. Something like:
$items = $woocommerce->cart->get_cart();
$lastItemAdded = end($items);
And then I'd pull the values I need from $lastItemAdded.
However, this doesn't work, because if the user adds a product that already exists in their cart, WooCommerce augments the product quantity, but does not shuffle this cart item to the end position in the array.
In other words, the end($items) will only work if the item being added is a new item and not one being updated with additional quantity.
I've also tried using the filter hook woocommerce_add_cart_item – this does return the last item added, and may be a better approach than the end($items) method, but again, when the item is already in the cart and quantities are only being modified on add-to-cart, this hook is not triggered, so I can't reference the properties of the most recently added/modified cart item.
My question therefore is the following: when hooking with the filter woocommerce_add_to_cart_redirect is there any way to reliably get the cart item that has just been added or modified to the cart?
do_action( 'woocommerce_before_cart' );
do_action( 'woocommerce_before_cart_contents' );
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<tr class="woocommerce-cart-form__cart-item <?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-remove">
<?php
// #codingStandardsIgnoreLine
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'×',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
</td>
<td class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
if ( ! $product_permalink ) {
echo $thumbnail; // PHPCS: XSS ok.
} else {
printf( '%s', esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
}
?>
</td>
<td class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">
<?php
if ( ! $product_permalink ) {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . ' ' );
} else {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
}
do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key );
// Meta data.
echo wc_get_formatted_cart_item_data( $cart_item ); // PHPCS: XSS ok.
// Backorder notification.
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>', $product_id ) );
}
?>
</td>
<td class="product-price" data-title="<?php esc_attr_e( 'Price', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</td>
<td class="product-quantity" data-title="<?php esc_attr_e( 'Quantity', 'woocommerce' ); ?>">
<?php
if ( $_product->is_sold_individually() ) {
$product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key );
} else {
$product_quantity = woocommerce_quantity_input( array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'max_value' => $_product->get_max_purchase_quantity(),
'min_value' => '0',
'product_name' => $_product->get_name(),
), $_product, false );
}
echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item ); // PHPCS: XSS ok.
?>
</td>
<td class="product-subtotal" data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</td>
</tr>
<?php
}
}
I want to display woocommerce product gallery to post page with galleries effect on my custom theme (based twentysixteen).
Status WooCommerce Version : 3.5.1
For display the product gallery, i using the below code:
<?php
if ( ! function_exists( 'wc_get_gallery_image_html' ) ) {
return;
}
global $product;
$columns = apply_filters( 'woocommerce_product_thumbnails_columns', 4 );
$post_thumbnail_id = $product->get_image_id();
$wrapper_classes = apply_filters( 'woocommerce_single_product_image_gallery_classes', array(
'woocommerce-product-gallery',
'woocommerce-product-gallery--' . ( $product->get_image_id() ? 'with-images' : 'without-images' ),
'woocommerce-product-gallery--columns-' . absint( $columns ),
'images',
'flex-control-thumbs0',
'woocommerce-product-gallery__trigger',
) );
?>
<div class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', $wrapper_classes ) ) ); ?>" data-columns="<?php echo esc_attr( $columns ); ?>" style="opacity: 1; transition: opacity .25s ease-in-out;">
<figure class="woocommerce-product-gallery__wrapper">
<?php
if ( $product->get_image_id() ) {
$html = wc_get_gallery_image_html( $post_thumbnail_id, true );
} else {
$html = '<div class="woocommerce-product-gallery__image--placeholder">';
$html .= sprintf( '<img src="%s" alt="%s" class="wp-post-image" />', esc_url( wc_placeholder_img_src( 'woocommerce_single' ) ), esc_html__( 'Awaiting product image', 'woocommerce' ) );
$html .= '</div>';
}
echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', $html, $post_thumbnail_id ); // phpcs:disable WordPress.XSS.EscapeOutput.OutputNotEscaped
do_action( 'woocommerce_product_thumbnails' );
?>
</figure>
</div>
The product gallery now already displayed, but just image. Not has effect slider or zoom.
You can see the screenshot of my page on this link:
https://imgur.com/a/FnBJhtf
How i must do for show the effect of slider and zoom?
After WooCommerce 3.0, there is a significant frontend change that can be broken down into three separate new features;
Image zoom / magnification
Lightbox
Slider
To enable each of these features in your theme you must declare support using add_theme_support() like so;
add_action( 'after_setup_theme', 'yourtheme_setup' );
function yourtheme_setup() {
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
Code goes in functions.php
i've been trying to implement the modification discussed on the page: Woocommerce: Featured image different than product image
But it doesn't seem to work with the latest release of woocommerce v2.4.7
does anyone have any ideas on how to fix it?
Here is the current code from v2.4.7 i'm trying to replace. when the fix is applied it breaks the page.
<div class="images">
<?php
if ( has_post_thumbnail() ) {
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_caption = get_post( get_post_thumbnail_id() )->post_excerpt;
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
'title' => $image_title,
'alt' => $image_title
) );
$attachment_count = count( $product->get_gallery_attachment_ids() );
if ( $attachment_count > 0 ) {
$gallery = '[product-gallery]';
} else {
$gallery = '';
}
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_caption, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="%s" />', wc_placeholder_img_src(), __( 'Placeholder', 'woocommerce' ) ), $post->ID );
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
</div>
And here the original modified code from the page mentioned above:
<div class="images">
<?php
$attachment_ids = $product->get_gallery_attachment_ids();
isset ($placeholder_width)? : $placeholder_width=0;
isset ($placeholder_height)? : $placeholder_height=0;
if ( $attachment_ids ) {
$attachment_id = $attachment_ids[0];
if ( ! $placeholder_width )
$placeholder_width = $woocommerce->get_image_size( 'shop_catalog_image_width' );
if ( ! $placeholder_height )
$placeholder_height = $woocommerce->get_image_size( 'shop_catalog_image_height' );
$output = '<div class="imagewrapper">';
//$classes = array( 'imagewrapper' );
$classes = array();
$image_link = wp_get_attachment_url( $attachment_id );
if ( $image_link ) {
$image = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_thumbnail_size', 'shop_thumbnail' ) );
$image_class = esc_attr( implode( ' ', $classes ) );
$image_title = esc_attr( get_the_title( $attachment_id ) );
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
}
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
</div>
Any help would be much appreciated.
Thanks
Darrell
I've been struggling with the same here, but this seems to have done the trick for me:
<div class="images">
<?php
$attachment_ids = $product->get_gallery_attachment_ids();
isset ($placeholder_width)? : $placeholder_width=0;
isset ($placeholder_height)? : $placeholder_height=0;
if ( $attachment_ids ) {
$attachment_id = $attachment_ids[0];
if ( ! $placeholder_width )
$size = wc_get_image_size( 'shop_catalog' );
$placeholder_width = $size['width'];
if ( ! $placeholder_height )
$size = wc_get_image_size( 'shop_catalog' );
$placeholder_width = $size['height'];
$output = '<div class="imagewrapper">';
//$classes = array( 'imagewrapper' );
$classes = array();
$image_link = wp_get_attachment_url( $attachment_id );
if ( $image_link ) {
$image = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image_class = esc_attr( implode( ' ', $classes ) );
$image_title = esc_attr( get_the_title( $attachment_id ) );
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_caption, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="%s" />', wc_placeholder_img_src(), __( 'Placeholder', 'woocommerce' ) ), $post->ID );
}
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
Just recently figured it out, so it possibly needs some finishing touches, but might point you in the right direction.
However, I'm not able to remove the product image from the gallery. The code that's supposed to make that happen is this (added to the product-thumbnails.php):
unset($attachment_ids);
All this does for me is removing ALL of the gallery thumbnails. Maybe you have figured this one out?
Put this:
unset($attachment_ids); // Don't display first image in product gallery, only as product image
right before the foreach loop in product-thumbnails.php. It's working fine for me.
Hege got it right with the replacement code for page product-image.php
To remove just the first thumbnail, and not all of them, use
unset($attachment_ids[0]);
on product-thumbnails.php
I am currently working on a Wordpress website that supports WooCommerce. I've created a product, and I've uploaded a featured image and other photos for the gallery. But there is a problem i can't figure out. It doesn't matter how big the photo is, if I put the cursor on it, it zooms almost 200%, and that's too much. Does anyone know how to reduce (or remove) the zoom feature? I've been to "sinlge-product/product-image.php" but it seems I can't find any zoom option in the code:
<?php
/**
* Single Product Image
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.0.14
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $post, $woocommerce, $product;
?>
<div class="images">
<?php
if ( has_post_thumbnail() ) {
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_caption = get_post( get_post_thumbnail_id() )->post_excerpt;
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
'title' => $image_title,
'alt' => $image_title
) );
$attachment_count = count( $product->get_gallery_attachment_ids() );
if ( $attachment_count > 0 ) {
$gallery = '[product-gallery]';
} else {
$gallery = '';
}
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="%s" />', wc_placeholder_img_src(), __( 'Placeholder', 'woocommerce' ) ), $post->ID );
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
</div>
Thank you!
Copy / Paste this code to functions.php on your child theme. Tested and approved ;)
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
add_filter( 'woocommerce_gallery_full_size', 'change_magnifier_lightbox_image_size', 20, 1 );
function change_magnifier_lightbox_image_size( $size ){
$thumbnail_id = get_post_thumbnail_id( get_the_id() );
$attachment = wp_get_attachment_metadata( $thumbnail_id, FALSE );
// Always return a value in a filter hook
return ( $attachment['width'] > 3071 || $attachment['height'] > 3071 ) ? 'preview' : 'large';
}
I'm using Woocommerce for a 1800+ product e-shop. Woocommerce comes with a built-in PrettyPhoto lightbox plugin. By default it loads the original Wordpress image size when you click to zoom the image in the lightbox.
Woocommerce calls for this original image in the template 'product-image.php'. I can modify that one. The variable $image_link calls the original size. I would like to call a custom size that I already made. I just don't know how to call it, I've looked for over a day in docs, forums and sites but can't find it.
I need to modify $image_link so that it calls my custom image size (that I added via add_image_size). But how? I know this is Wordpress basics but I'm unable to get it done...
Example of my site (click to zoom, it shows a reduced image of 1600px wide image, but I want a smaller one): http://www.affichesmarci.com/shop/the-railys-cycling-act/
<?php
if ( has_post_thumbnail() ) {
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
$attachment_count = count( $product->get_gallery_attachment_ids() );
if ( $attachment_count > 0 ) {
$gallery = '[product-gallery]';
} else {
$gallery = '';
}
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" class="bigbox" />', woocommerce_placeholder_img_src() ), $post->ID );
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
I found the solution myself.
Original variable that calls the full size image.
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
New variable that calls a custom size 'zoom'.
$image_link = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'zoom' );
If you already set "add_image_size" in functions, I think you could easily edit
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
to
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'your_thumbnail_name', 'shop_single' ) );
you could also use WP's default sizes which is "thumbnail", "medium", "large" and "full"
ok, adding large size instead of full
$image_link = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
Now change $image_link to $image_link[0]
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link[0], $image_title, $image ), $post->ID );
Thanks for ideas :)
for get src url use this code:
$image_link = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail');
and for use to tag image by alt and size:
echo '<a href="' . get_permalink() . '" class="thumb"><span class="face">
<img src="' . $image_link[0]. '" width="'.$image_link[1].'"
height="'.$image_link[2].'" loading="lazy" alt="'.get_the_title().'" /></span></a>';