Product title below the thumbnail in Woocommerce email notifications - php

How to make the text with the name of the product in the woocommerce email be below the product image?
I want the letters to look good.

Updated: If the product image are displayed in your email notifications, you can try the following to display the product title under this image:
add_filter( 'woocommerce_order_item_name', 'product_title_under_thumbnail_emails', 10, 3 );
function product_title_under_thumbnail_emails( $item_name, $item, $is_visible ) {
// Targeting view order pages only
if( is_wc_endpoint_url() )
return $item_name;
// Get the WC_Product object (from order item)
$product = $item->get_product();
if( $product->get_image_id() > 0 && $is_visible )
$item_name = '<br>' . $item_name;
return $item_name;
}
Code goes in function.php file of your active child theme (or active theme). It should works.

Related

Set order item permalink from the parent variable product in WooCommerce

On the thank you page and in order emails, the permalink for variable products always links directly to the product variation instead of the parent product itself, e.g., https://mystore.com/some-product/?attribute_color=red. I need the permalink to reflect the product and not the variation, e.g. https://mystore.com/some-product/.
I've tried the following:
$parent_id = $product->get_parent_id();
$slug = $product->get_permalink($parent_id);
The variable
$parent_id
returns correctly, but
$slug
is always the variation permalink. What am I missing? Alternatively, I've tried to retrieve the post name of the parent like so
$parent_id = $product->get_parent_id();
$slug = $product->get_post_name($parent_id);
but this throws an error and the thank you page only partially renders.
You don't need to override any template file, just use the following hooked function, to replace the product variation permalink, by the parent variable product permalink on all orders:
add_filter( 'woocommerce_order_item_permalink', 'filter_order_item_permalink_callback', 10, 3 );
function filter_order_item_permalink_callback( $product_permalink, $item, $order ) {
// For product variations
if( $item->get_variation_id() > 0 ){
$product = $item->get_product();
$is_visible = $product && $product->is_visible();
// Get the instance of the parent variable product Object
$parent_product = wc_get_product( $item->get_product_id() );
// Return the parent product permalink (if product is visible)
return $is_visible ? $parent_product->get_permalink() : '';
}
return $product_permalink;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
For email notifications
WooCommerce doesn't display, by default, the product permalink in the email notifications…
To display the product permalink on email notifications, use the following:
add_filter( 'woocommerce_order_item_name', 'filter_order_item_name_callback', 10, 3 );
function filter_order_item_name_callback( $item_name, $item, $is_visible ) {
// On emails notifications only
if( ! is_wc_endpoint_url() > 0 ) {
$product = $item->get_product();
// For product variation type
if( $item->get_variation_id() > 0 ){
// Get the instance of the parent variable product Object
$parent_product = wc_get_product( $item->get_product_id() );
// The parent product permalink (if product is visible)
$product_permalink = $parent_product->get_permalink();
}
// For other item (product) type
else {
$product_permalink = $product->get_permalink();
}
return sprintf( '%s', $product_permalink, $item_name );
}
return $item_name;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and work.

Disable item name link for specific product in Woocommerce cart checkout and orders

I'm looking to disable the product link to the product page of a specific product in the cart. This product is a gift product automatically added to the cart when the cart subtotal amount equals a particular value.
I know it's possible to do this with all the cart items. But I'm not quite sure on how to target a specific item.
New answer that works for all product types for an array of defined products Ids, here:
Disable item link for specific products in WooCommerce cart checkout and orders
Updated: Added a hooked function to handle minicart
To remove the item name link from cart, checkout and orders, use the following:
// Cart item link
add_filter( 'woocommerce_cart_item_name', 'conditionally_remove_link_from_cart_item_name', 10, 3 );
function conditionally_remove_link_from_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
// HERE set your Free product ID
$gift_product_id = 37;
if( $gift_product_id == $cart_item['data']->get_id() ) {
$item_name = $cart_item['data']->get_name();
}
return $item_name;
}
// Mini-cart item link
add_filter( 'woocommerce_cart_item_permalink', 'conditionally_remove_cart_item_permalink', 10, 3 );
function conditionally_remove_cart_item_permalink( $permalink, $cart_item, $cart_item_key ) {
// HERE set your Free product ID
$gift_product_id = 37;
if( $gift_product_id == $cart_item['data']->get_id() ) {
$permalink = '';
}
return $permalink;
}
// Order item link
add_filter( 'woocommerce_order_item_name', 'conditionally_remove_link_from_order_item_name', 10, 2 );
function conditionally_remove_link_from_order_item_name( $item_name, $item ) {
// HERE set your Free product ID
$gift_product_id = 37;
if( $gift_product_id == $item->get_product_id() ) {
$item_name = $item->get_name();
}
return $item_name;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Add an icon after product price on Woocommerce archive pages

I am trying to add a custom icon after the product price in Woocommerce for a specific product category on shop page. So I would like to add an icon of a "fast delivery truck" after the price on all products from "FAST SHIPPING" product category.
I would like it to display that like in wish.com web site, like in this screenshot:
This is what I've tried:
add_filter( 'woocommerce_price_html', 'prepend_append_icon_to_price', 10, 2 );
function prepend_append_icon_to_price( $price, $instance ) {
if(is_product_category( 'fast-shipping')){
$icon = ' <i class="fas fa-shipping-fast"></i> ';
$price = $icon . $price . $icon;
}
return $price;
}
But It doesn't display anything after the price.
Any help would be much appreciated.
You are using the wrong hook since Woocommerce 3 and there are some errors in your code.
To display an icon after the price on the right for "fast-shipping" product category, two cases:
1) On all Woocommerce archive pages:
add_filter( 'woocommerce_get_price_html', 'prepend_append_icon_to_price', 10, 2 );
function prepend_append_icon_to_price( $price, $product ) {
if( has_term( 'fast-shipping', 'product_cat', $product->get_id() ) && ! is_product() ){
$price .= '<span style="float:right"><i class="fas fa-shipping-fast"></i></span> ';
}
return $price;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) On a specific Woocommerce product category archive pages:
add_filter( 'woocommerce_get_price_html', 'append_icon_after_product_price', 10, 2 );
function append_icon_after_product_price( $price, $product ) {
if( is_product_category( 'fast-shipping' ) ){
$price .= '<span style="float:right"><i class="fas fa-shipping-fast"></i></span> ';
}
return $price;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Use css before or after belong to the price tag :
fa-shipping-fast:before{
content: url(.....);
width : ....;
height : ....;
....
}

Add the product thumbnail on Woocommerce Thankyou page

In woocommerce, on Order received page (Thankyou page) the product image is not displayed in the order items.
How can I display the product image in order items on Order received page?
Is there a usable hook for that?
Or do I have to override the template order/order-details-item.php file?
Any help is appreciated.
To display the thumbnail image in the order items on Order received page (thankyou), you will use:
// Display the product thumbnail in order received page
add_filter( 'woocommerce_order_item_name', 'order_received_item_thumbnail_image', 10, 3 );
function order_received_item_thumbnail_image( $item_name, $item, $is_visible ) {
// Targeting order received page only
if( ! is_wc_endpoint_url('order-received') ) return $item_name;
// Get the WC_Product object (from order item)
$product = $item->get_product();
if( $product->get_image_id() > 0 ){
$product_image = '<span style="float:left;display:block;width:56px;">' . $product->get_image(array(48, 48)) . '</span>';
$item_name = $product_image . $item_name;
}
return $item_name;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Add the sku to order items on my account order view pages in Woocommerce

In Woocommerce, I am customizing my view order in MyAccount. I already added the Product images with this answer code: Add the product image to Woocommerce my account order view
Now I would like to add the Product SKU to The View order pages but, I don't know how to get it.
Anyone have an Idea?
Replacing your code with the following to display the product SKU in order items:
// Display the product thumbnail in order view pages
add_filter( 'woocommerce_order_item_name', 'display_product_image_in_order_item', 20, 3 );
function display_product_image_in_order_item( $item_name, $item, $is_visible ) {
// Targeting view order pages only
if( is_wc_endpoint_url( 'view-order' ) ) {
$product = $item->get_product(); // Get the WC_Product object (from order item)
$thumbnail = $product->get_image(array( 36, 36)); // Get the product thumbnail (from product object)
// The thumbnail
if( $product->get_image_id() > 0 )
$item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>' . $item_name;
// The SKU
if( $sku = $product->get_sku() )
$item_name .= '<br><div class="product-sku">' . $sku . '</div>';
}
return $item_name;
}
Code goes in function.php file of your active child theme (active theme). It should works.

Categories