Karel
Karel

Reputation: 13

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 or get_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/
 */
 
// -------------------------
// 1. Display Free Sample Add to Cart 
// Note: change "111" with Free Sample ID
  
add_action( 'woocommerce_single_product_summary', 'bbloomer_add_free_sample_add_cart', 35 );
  
function bbloomer_add_free_sample_add_cart() {
   ?>
      <form class="cart" method="post" enctype='multipart/form-data'>
      <button type="submit" name="add-to-cart" value="111" class="single_add_to_cart_button button alt">Order a Free Sample</button>
      <input type="hidden" name="free_sample" value="<?php the_ID(); ?>">
      </form>
   <?php
}
  
// -------------------------
// 2. Add the custom field to $cart_item
  
add_filter( 'woocommerce_add_cart_item_data', 'bbloomer_store_free_sample_id', 9999, 2 );
  
function bbloomer_store_free_sample_id( $cart_item, $product_id ) {
   if ( isset( $_POST['free_sample'] ) ) {
         $cart_item['free_sample'] = $_POST['free_sample'];
   }
   return $cart_item; 
}
  
// -------------------------
// 3. Concatenate "Free Sample" with product name (CART & CHECKOUT)
// Note: rename "Free Sample" to your free sample product name
  
add_filter( 'woocommerce_cart_item_name', 'bbloomer_alter_cart_item_name', 9999, 3 );
  
function bbloomer_alter_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
   if ( $product_name == "Free Sample" ) {
      $product = wc_get_product( $cart_item["free_sample"] );
      $product_name .=  " (" . $product->get_name() . ")";
   }
   return $product_name;
}
  
// -------------------------
// 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 );
    }
}

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.

Upvotes: 1

Views: 567

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29614

In the cart/cart.php template file on line 67 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 deprecated since WooCommerce 3. Use woocommerce_checkout_create_order_line_item 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 );

Upvotes: 1

Related Questions