php - Set image thumbnail for order a "Free Sample" on cart page in WooCommerce

I have found "WooCommerce: Order a “Free Sample” @ Single Product Page" on Business Bloomer to create a sample option in the shop for people to order.
Question is: how can I make sure also to copy the thumbnail of item like it does with the name. Something with$thumbnail
orget_image
etc so its visible in the on the cart page?
/**
* @snippet Add Free Sample to Cart @ Single Product
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 4.0
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
//
I've tried adding this rule to step 3
$thumbnail = $_product->get_image();
and
add_filter( 'woocommerce_cart_item_thumbnail', 'hostingrumors_voeg_thumbnail_toe', 9999, 2 );
function hostingrumors_voeg_thumbnail_toe( $_product->get_image(), $cart_item, $cart_item_key ) {
if ( $product_name == "Gratis staal" ) {
$product = wc_get_product( $cart_item["gratis_staal"] );
$_product->get_image() .= " (" . $_product->get_image() . ")";
}
return $_product->get_image();
}
But it doesn't add the visited product image page. So I am doing something wrong.
Answer
Answer
Answer
Answer
Answer
Solution:
In the template file on line
we find
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
So to replace the product image from the "Free Sample" product on cart page use
// 3.1 Replace "Free Sample" with product image (CART & CHECKOUT)
function filter_woocommerce_cart_item_thumbnail( $product_image, $cart_item, $cart_item_key ) {
// Get name
$item_name = $cart_item['data']->get_name();
if ( $item_name == "Free Sample" ) {
// Get product
$product = wc_get_product( $cart_item["free_sample"] );
// Get image
$new_product_image = $product->get_image();
// Add new image
$product_image = $new_product_image;
}
return $product_image;
}
add_filter( 'woocommerce_cart_item_thumbnail', 'filter_woocommerce_cart_item_thumbnail', 10, 3 );
Note:woocommerce_add_order_item_meta
hook is since WooCommerce 3. Use
instead
So replace
// 4. Add "Free Sample" product name to order meta
// Note: this will show on thank you page, emails and orders
add_action( 'woocommerce_add_order_item_meta', 'bbloomer_save_posted_field_into_order', 9999, 2 );
function bbloomer_save_posted_field_into_order( $itemID, $values ) {
if ( ! empty( $values['free_sample'] ) ) {
$product = wc_get_product( $values['free_sample'] );
$product_name = $product->get_name();
wc_add_order_item_meta( $itemID, 'Free sample for', $product_name );
}
}
With
// 4. Add "Free Sample" product name to order meta
// Note: this will show on thank you page, emails and orders
function action_woocommerce_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
if ( ! empty( $values['free_sample'] ) ) {
$product = wc_get_product( $values['free_sample'] );
$product_name = $product->get_name();
$item->update_meta_data( __( 'Free sample for', 'woocommerce' ), $product_name );
}
}
add_action('woocommerce_checkout_create_order_line_item', 'action_woocommerce_checkout_create_order_line_item', 10, 4 );
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non-numeric value encountered
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.