Add a gallery image to Woocommerce checkout - php

I'm trying to add an image from the product gallery to the Woocommerce checkout. I've managed it on the cart using the following code:
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id )
{
// Display Image instead of URL
echo wp_get_attachment_image($attachment_id, 'full');
}
But this won't work in the checkout. I've currently got the main product image showing using this code:
$thumbnail = apply_filters( 'woocommerce_in_cart_product_thumbnail', $_product->get_image(), $values, $cart_item_key );
echo $thumbnail;
But I can't figure out how to adapt it to allow the gallery image through.
Any help would be appreciated!
Cheers,
Ash

Related

woocommerce thumbnail not working on cart page

I have several thousand products on the store that do not have a thumbnail. So I came up with a code to get them from the "featured-images" attribute, which has a link in it. On the store page, the category page works fine, but the product in the cart does not display the image. What am I doing wrong?
function alterImageSRC($image, $attachment_id, $size, $icon){
global $product;
$url = "https://website.com/wp-content/uploads/thumbs/placeholder.png";
if (!is_null($product)) {
$optimex_featured = $product->get_attribute( 'pa_featured-images' );
$url = "https://website.com/wp-content/uploads/thumbs/$optimex_featured";
}
$image[0] = $url;
return $image;
}
add_filter('wp_get_attachment_image_src', 'alterImageSRC', 10, 4);

Get the URL of the first image from the product gallery in Woocommerce

How can I get the URL of the first image that is uploaded to the product gallery?
I am not talking about the featured image.
I would appreciate any suggestions.
The following will give you the first image Url from the product gallery:
global $post, $product;
if( is_a($product, 'WC_Product'))
$product = wc_get_product($post->ID);
$product = wc_get_product(40);
// Get the array of the gallery attachement IDs
$attachment_ids = $product->get_gallery_image_ids();
if( sizeof($attachment_ids) > 0 ){
$first_attachment_id = reset($attachment_ids);
$link = wp_get_attachment_image_src( $first_attachment_id, 'full' )[0];
// Output
echo $link;
}

Get Woocommerce Cart Product Image Url So I can use it as background image instead

In cart.php we have:
<td class="product-thumbnail" style='background:url(<?php ?>)'>
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
if ( ! $product_permalink ) {
echo $thumbnail;
} else {
printf( '%s', esc_url( $product_permalink ), $thumbnail );
}
?>
</td>
Is there a way to get the url from that so I can use a background image on the td element instead of the image tag inside?
Was able to get it to work using
<?php echo get_the_post_thumbnail_url($product_id);?>
If for whatever reason, this isn't the best way to solve this problem, please share.
Thanks.

How can I get a woocommerce brand thumbnail attribute?

I want to display brand logos on all product pages.
I've uploaded the logos in the theme editor by going to products->attributes->brands...clicking on the sprocket allows one to upload a thumbnail image.
There are several plugins that can achieve the same thing, but there must be a way to retrieve the information of the brand thumbnail.
If you're using the WooCommerce Brands plugin, the following code supports obtaining a brand thumbnail URL:
global $post;
$brands = wp_get_post_terms( $post->ID, 'product_brand' );
if ( $brands )
$brand = $brands[0];
if ( ! empty( $brand ) ) {
$thumbnail = get_brand_thumbnail_url( $brand->term_id );
$url = get_term_link( $brand->slug, 'product_brand' );
echo '<img class="woocommerce-brand-image-single" src="'. $thumbnail . '"/>';
}
.. If you are looking up via a term ID, then the following code will also obtain the thumbnail URL:
$url = get_brand_thumbnail_url( $term->term_id, 'full' );

Hook for customizing product image thumbnail

I need a help on woo-commerce to override the cart product image thumbnail.
I am creating a plugin for customizing the product in the detail page and if we do "add to cart" it will be updated in the cart page with a customized thumbnail.
If any hook is available for overriding the image, please let me know.
I've spent many hours searching for the answer also and even asked a Stackoverflow question (WooCommerce: change product image permalink with filter/action hook) which now happens to be duplicate (could not find this question prior to submitting my own).
The answer:
The hook is woocommerce_cart_item_thumbnail.
So in your functions.php add
function custom_new_product_image($a) {
$class = 'attachment-shop_thumbnail wp-post-image'; // Default cart thumbnail class.
$src = [PATH_TO_YOUR_NEW_IMAGE];
// Construct your img tag.
$a = '<img';
$a .= ' src="' . $src . '"';
$a .= ' class="' . $class . '"';
$a .= ' />';
// Output.
return $a;
}
add_filter( 'woocommerce_cart_item_thumbnail', 'custom_new_product_image' );
and your thumbnails will be replaced (more processing needed if you want to change each thumbnail individually).
To change thumbnail image size of WooCommerce cart page you need next steps:
In function.php create the size you need:
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'custom-thumb', 100, 100, true ); // 100 wide and 100 high
}
In cart.php which should be located in your_theme\woocommerce\cart\cart.php find
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image( 'custom-thumb' ), $cart_item, $cart_item_key );
Please review the WooCommerce cart templates in woocommerce/templates/cart/cart.php.
There is a clear filter woocommerce_cart_item_thumbnail for the product thumbnail in the cart.
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
Change 'medium' for your required image size:
/**
* Update cart product thumbnail
*/
function woocommerce_cart_item_thumbnail_2912067($image, $cartItem, $cartItemKey)
{
$id = ($cartItem['variation_id'] !== 0 ? $cartItem['variation_id'] : $cartItem['product_id']);
return wp_get_attachment_image(get_post_thumbnail_id((int) $id), 'medium');
}
add_filter('woocommerce_cart_item_thumbnail', 'woocommerce_cart_item_thumbnail_2912067', 10, 3);
This results in your not having to update the core WooCommerce template files.

Categories