Adding custom text to the variation price in Woocommerce - php

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.

Related

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 a text under item name based on checkbox custom field in Woocommerce checkout

I have a custom function that checks if a checkbox is checked and if so, it adds 'with vat relief' next to the price. If it isn't checked, it adds 'inc vat' next to the price. That works fine and my code is:
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
$isTaxRelefe = get_post_meta($product->id, 'disability_exemption', true);
if ($isTaxRelefe == 'yes')
$price .= ' ' . __('with vat relief');
else $price .= ' ' . __('inc vat');
return $price;
}
What I need to do now is add another function targeting the checkout page that says if the checkbox is checked show some text underneath the product title but I'm struggling. My initial thought was to edit the /checkout/review-order so I added an if else statement to output something next to the product title is. I added:
$isTaxRelefe = get_post_meta($product->id, 'disability_exemption', true);
if ($isTaxRelefe == 'yes') {
$content .= 'VAT RELIEF AVAILABLE';
}
but this does nothing, I have tried various variations, changing to echo statements etc. but no luck. I'm sure I am just writing this incorrectly. Can anyone advise? What I'm not very up on are WordPress functions as in if I could write one to target the checkout page only, Im not sure how it determines where to output your. an if else statement seemed like the obvious choice but not having any luck.
Your code is a bit outdated and you should use $product->get_id() since Woocommerce 3 in your first function instead of $product->id in the get_post_meta() function.
You can also use instead the WC_Data method get_meta() from the product object directly.
Below is your revisited code with the additional hooked function that will display conditionally "VAT RELIEF AVAILABLE" text under the product title in checkout page: (without overriding the template review-order.php)
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
if ( $product->get_meta('disability_exemption') === 'yes')
$price .= ' ' . __('with vat relief');
else
$price .= ' ' . __('inc vat');
return $price;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_text_below_checkout_product_title', 20, 3 );
function custom_text_below_checkout_product_title( $quantity_html, $cart_item, $cart_item_key ){
if ( $cart_item['data']->get_meta('disability_exemption') === 'yes' )
$quantity_html .= '<br>' . __('VAT RELIEF AVAILABLE');
return $quantity_html;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.

Add an extra text that appears on selected variations in WooCommerce

I would like to add a function that fires when the customer selects a product variant. Just as the price appears (after choosing variants).
It is a simple function (from functions.php) that reads additional information about productions from the database.
However, I do not know if there is such a hook?
To add a text on selecting a variation you can use the following:
add_filter('woocommerce_available_variation', 'variation_custom_text', 10, 3 );
function variation_custom_text( $variation_data, $product, $variation ) {
// Here your custom text
$custom_text = __("This is my custom text…", "woocommerce");
$variation_data['availability_html'] .= '<p>' . $custom_text . '</p>';
return $variation_data;
}
Code goes in function.php file of the active child theme (or active theme). Tested and works.

Display selected variation price suffixed in Woocommerce variable products

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.

Change Woocommerce product quantity in checkout table

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.

Categories