I would like to change the way Woocommerce is displaying the product quantity in the order review table. I would like the quantity to be underneath the product name instead of after it.
I found this post which helped, but the code only changes the quantity layout for variable products.
How can I change it for EVERY product, even simple ones?
This can be done in multiple ways:
1) Overriding template checkout/review-order.php via your child theme.
2) Customizing the product item name:
add_filter( 'woocommerce_cart_item_name', 'customizing_checkout_item_name', 10, 3);
function customizing_checkout_item_name( $item_name, $cart_item, $cart_item_key ) {
if( is_checkout() )
$item_name .= '<br>';
return $item_name;
}
Code goes in function.php file of the active child theme (or active theme).
3) Customizing the product item quantity (the best way):
add_filter( 'woocommerce_checkout_cart_item_quantity', 'customizing_checkout_item_quantity', 10, 3);
function customizing_checkout_item_quantity( $quantity_html, $cart_item, $cart_item_key ) {
$quantity_html = ' <br>
<span class="product-quantity">' . __('Quantity:') . ' <strong>' . $cart_item['quantity'] . '</strong></span>';
return $quantity_html;
}
Code goes in function.php file of the active child theme (or active theme).
All code is tested and works.
Related
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 : ....;
....
}
I thought this would have been easy, but I am stuck. All I am trying to do is add the word each after the variation price on a product page. The solution I have found adds it on the category page and in two places on the product page.
The code is:
/* Adds a text Each - after price */
function change_product_price( $price ) {
$price .= ' each';
return $price;
}
add_filter( 'woocommerce_get_price_html', 'change_product_price' );
From the picture above, I only need the each added to the price above the add to cart button, but not the other pacles like the crossed out section in the photo above.
Thank you for any guidance you can provide.
The following code will add a suffix to the product variations price:
add_filter('woocommerce_available_variation', 'variation_price_custom_suffix', 10, 3 );
function variation_price_custom_suffix( $variation_data, $product, $variation ) {
$variation_data['price_html'] .= ' <span class="price-suffix">' . __("each", "woocommerce") . '</span>';
return $variation_data;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
I am using this function so I get the default variation price but I want to show my price suffix after the price:
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
foreach($product->get_available_variations() as $pav){
$def=true;
foreach($product->get_variation_default_attributes() as $defkey=>$defval){
if($pav['attributes']['attribute_'.$defkey]!=$defval){
$def=false;
}
}
if($def){
$price = $pav['display_price'];
}
}
return woocommerce_price($price);
}
What I am getting out now is for example '€15,00' but what I want it to show is '€15,00 per kilo' and 'per kilo' is the price suffix
Your code is outdated since Woocommerce 3 (get_variation_default_attributes() and woocommerce_price() are deprecated).
The will display the selected variation price suffixed for Woocommerce 3:
add_filter('woocommerce_available_variation', 'display_variation_price_suffixed', 10, 3 );
function display_variation_price_suffixed( $variation_data, $product, $variation ) {
$variation_data['price_html'] .= ' <span class="price-suffix">' . __("per kilo", "woocommerce") . '</span>';
return $variation_data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If needed you can use $variation_data['attributes'] array to target specific product attributes term values in a foreach loop.
For information, the filter hook woocommerce_available_variation is located inside WC_Product_Variable get_available_variation() method.
I'm trying to find a way to place custom text before availability info (out of stock, in stock etc.) on woocommerce single product page.
I am using something like that:
add_filter( 'woocommerce_get_availability', 'change_product_availability_display' );
add_filter( 'unknown filter', 'change_product_availability_display' );
function change_product_availability_display( $availability ) {
// Additional text
$text = __('Availability:');
// returning the text before the availability
return $text . ' ' . $availability;
}
Its's based on: Add a custom text before the price display in WooCommerce.
In case of price filter named in my code "unknown filter" was woocommerce_cart_item_price. I've looked for this kind of filter for availability/stock item, but can't find it.
Maybe someone could review this code and help me to find this "unknown_filter" or have other idea how I can put custom text before availability info?
I have revisited your code as your are trying to merge a string with an array. Also I have added a 2nd hooked function that will allow to display your "labeled" availability to items in cart and checkout.
The code:
add_filter( 'woocommerce_get_availability', 'add_label_to_availability_display' );
function add_label_to_availability_display( $availability ) {
if( is_product() || is_cart() || is_checkout() ){
$label = __( 'Availability', 'woocommerce' ) . ': ';
$availability['availability'] = $label . $availability['availability'];
}
return $availability;
}
add_filter( 'woocommerce_cart_item_name', 'add_availability_below_cart_item_name', 10, 3);
function add_availability_below_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$availability = $cart_item['data']->get_availability();
return $item_name . '<br>' . $availability['availability'];
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.
I'm creating my new website with Wordpress and Woocommerce. I would like to display the short description in the order detail.
I found this code :
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_single_excerpt', 5);
But that shows me the description in home.
Is there a way to make it appear in the order detail?
It can be done with a custom unction hooked in woocommerce_order_item_name filter hook, this way:
add_filter( 'woocommerce_order_item_name', 'add_single_excerpt_to_order_item', 10, 3 );
function add_single_excerpt_to_order_item( $item_name, $item, $is_visible ){
$product_id = $item->get_product_id(); // Get the product Id
$excerpt = get_the_excerpt( $product_id ); // Get the short description
return $item_name . '<br><p class="item-description">' . $excerpt ; '</p>';
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works. It will display the short description in Order items below the item name.