Add a custom text before the price if the product has a sale price in WooCommerce
function add_custom_text_before_sale_prices( $price, $product ) {
// Text
$text_regular_price = __("Regular Price: ");
$text_final_price = __("Final Price: ");
if ( $product->is_on_sale() ) {
$has_sale_text = array(
'<del>' => '<del>' . $text_regular_price,
'<ins>' => '<br>'.$text_final_price.'<ins>'
);
$return_string = str_replace(
array_keys( $has_sale_text ),
array_values( $has_sale_text ),
$price
);
return $return_string;
}
return $price;
} `add_filter( 'woocommerce_get_price_html', 'add_custom_text_before_sale_prices', 100, 2 );`
But I am getting output -
USD100.00
Final Price: USD50.00
But I want
Regular Price: USD100.00
Final Price: USD50.00
you can use this code for it
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
function cw_change_product_price_display( $price ) {
// Your additional text in a translatable string
$text = __('TEXT');
// returning the text before the price
return $text . ' ' . $price;
}
Here are two snippets of code you can use, You can add in an active child theme function.php file.
Method 1: Add text before regular price and sale price
//Add text before regular price and sale price
function vh_rp_sale_price_html( $price, $product ) {
if ( $product->is_on_sale() ) :
$has_sale_text = array(
'<del>' => '<del>Regular Price: ',
'<ins>' => '<br>Sale Price: <ins>'
);
$return_string = str_replace(array_keys( $has_sale_text ), array_values( $has_sale_text ), $price);
else :
$return_string = 'Regular Price: ' . $price;
endif;
return $return_string;
}
add_filter( 'woocommerce_get_price_html', 'vh_rp_sale_price_html', 100, 2 );
Method 2: Add text before regular price only
//Add text before regular price only
function vh_rp_price_html( $price, $product ) {
$return_string = 'Regular Price: ' . $price;
return $return_string;
}
add_filter( 'woocommerce_get_price_html', 'vh_rp_price_html', 100, 2 );
See the attachment Output
Related
I am using a css to change the price view of the product with the code below. I found this to be the only advantageous working code. However, this code also appears on the product archive page and other components. I just want it to affect the price on the product page. And in any case, prices should appear in this view.
add_filter( 'woocommerce_get_price_html', 'bbloomer_simple_product_price_format', 10, 2 );
function bbloomer_simple_product_price_format( $price, $product ) {
if ( $product->is_on_sale() && $product->is_type('simple') ) {
$price = sprintf( __( '<div class="featured-price-container clickable"><div class="featured-price-box"><div class="featured-price-info"><div class="featured-price-discount"></div>Son 30 Günün En Düşük Fiyatı!</div><div class="featured-prices"><span class="prc-org">%1$s</span><span class="prc-dsc">%2$s</span></div></div></div>', 'woocommerce' ), wc_price ( $product->get_regular_price() ), wc_price( $product->get_sale_price() ), wc_price( $product->get_regular_price() - $product->get_sale_price() ) );
}
return $price;
}```
add_filter( 'woocommerce_get_price_html', 'bbloomer_simple_product_price_format', 10, 2 );
function bbloomer_simple_product_price_format( $price, $product ) {
if ( $product->is_on_sale() && $product->is_type('simple') ) {
$price = sprintf( __( '<div class="featured-price-container clickable"><div class="featured-price-box"><div class="featured-price-info"><div class="featured-price-discount"></div>Son 30 Günün En Düşük Fiyatı!</div><div class="featured-prices"><span class="prc-org">%1$s</span><span class="prc-dsc">%2$s</span></div></div></div>', 'woocommerce' ), wc_price ( $product->get_regular_price() ), wc_price( $product->get_sale_price() ), wc_price( $product->get_regular_price() - $product->get_sale_price() ) );
}
return $price;
}
I have already inserted this snippet to show an additional column with an icon if there are customer notes in the order. it works.
add_action( 'wp_enqueue_scripts', 'mini_enqueue_scripts' );
add_filter( 'manage_shop_order_posts_columns', 'woocommerce_add_order_notes_column', 99 );
function woocommerce_add_order_notes_column( $columns ) {
$columns['order_notes'] = __('Customer note', 'woocommerce');
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'woocommerce_show_order_notes_column', 10, 2 );
function woocommerce_show_order_notes_column( $column_name, $order_id ) {
switch ( $column_name ) {
case 'order_notes':
$order = wc_get_order( $order_id );
$note = $order->get_customer_note();
if ( !empty($note) ) {
echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( $note ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
} else {
echo '<span class="na">–</span>';
}
break;
}
}
How can I create another column (if the VAT number has been entered in the order) on the side that does the same thing?
For that I have two additional fields on the checkout page: "vat number" (billing_piva) and "CID number" (billing_cid). I would like to have them together on the same column with two headers first, like:
"Vat Number: xxx" and "CID Number: xxx"
Any advice?
To add 2 columns versus 1 you can actually apply the same as you already did for adding 1 column
Adding the VAT number to the order can be done in different ways, from your question I understand that this is a custom checkout field with the meta key: billing_piva
Note: the use of add_action( 'wp_enqueue_scripts', 'mini_enqueue_scripts' ); is not necessary
So you get:
// Display on order admin list (header)
function filter_manage_edit_shop_order_columns( $columns ) {
// Add columns
$columns['order_notes'] = __( 'Customer note', 'woocommerce' );
$columns['order_vat'] = __( 'VAT number', 'woocommerce' );
return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );
// Display on order admin list (populate the column)
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
// Get order
$order = wc_get_order( $post_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Compare
switch ( $column ) {
case 'order_notes':
// Get customer note
$note = $order->get_customer_note();
// NOT empty
if ( ! empty( $note ) ) {
echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( $note ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
} else {
echo '<span class="na">–</span>';
}
break;
case 'order_vat':
// Get VAT (if necessary, adjust to the correct meta key)
$vat_number = $order->get_meta( 'billing_piva' );
// NOT empty
if ( ! empty( $vat_number ) ) {
// Output
$output = '<span>' . sprintf( __( 'VAT Number: %s', 'woocommerce' ), $vat_number ) . '</span>';
// Get CID number
$cid_number = $order->get_meta( 'billing_cid' );
// NOT empty
if ( ! empty ( $cid_number ) ) {
// Concatenation
$output .= '<br><span>' . sprintf( __( 'CID Number: %s', 'woocommerce' ), $cid_number ) . '</span>';
}
// Print
echo $output;
} else {
echo '<span class="na">–</span>';
}
}
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );
I have advanced custom field plugin and i have added a custom field to my product the label name is (stock_number) my question is how do it display/show this field data in the new order email.
The below code should do the trick.
Option1:
add_action( 'woocommerce_order_item_meta_start', 'ts_order_item_meta_start', 10, 4 );
function ts_order_item_meta_start( $item_id, $item, $order, $plain_text ) {
if( $stock_number = get_field( 'stock_number', $item->get_product_id() ) ;
echo $stock_number;
}
Option2:
add_action( 'woocommerce_email_order_details', 'display_stock_email_order_details', 10, 4 );
function display_stock_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
foreach( $order->get_items() as $item ) {
if( $stock_number = get_field( "stock_number", $item->get_product_id() ) ){
echo '<p><strong>'.__('Stock Number').': </strong>'.$stock_number.'</p>';
}
}
}
Option3:
The below code will replace the Product Title with the ACF Custom Value.
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
// Targeting email notifications only
if( is_wc_endpoint_url() )
return $item_name;
// Get the WC_Product object (from order item)
$product = $item->get_product();
if( $stock_number = get_field('stock_number', $product->get_id()) ) {
$item_name = '<p class="item-stck" style="margin:12px 0 0;">
<strong>' . __( 'Stock Number', 'woocommerce' ) . ': </strong>' . $stock_number . '</p>';
}
return $item_name;
}
Option4:
The below code will replace the Product Name with your custom field.
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
// Get the WC_Product object (from order item)
$product = $item->get_product();
if( $stock_number = get_field('stock_number', $product->get_id()) ) {
$item_name = '<p class="item-stck" style="margin:12px 0 0;">
<strong>' . __( 'Stock Number', 'woocommerce' ) . ': </strong>' . $stock_number . '</p>';
}
return $item_name;
}
i want to change the "out of stock" text with html element woocommerce.
Currently, I'm using:
add_filter( 'woocommerce_get_availability', 'change_out_of_stock_text', 1, 2);
function change_out_of_stock_text( $availability, $_product ) {
global $post;
if ( ! $_product->is_in_stock() ) {
$out_of_stock = "Out of Stock";
if(get_post_meta( $post->ID, '_text_field', true )!="") {
$out_of_stock = get_post_meta( $post->ID, '_text_field', true );
}
$availability['availability'] = __($out_of_stock, 'woocommerce');
}
return $availability;
}
It can change the "Out of Stock" text with the value from custom input field i made in product page but i want to show the html element from that value to shop page as well. For example if the value is 'Sold out', it should output the link text as well "Sold out".
I also tried woocommerce_stock_html but it got the same problem:
function change_out_of_stock_text( $html, $_availability, $_product ){
global $post;
$out_of_stock = "Out of Stock";
$_availability = $_product->get_availability();
if(get_post_meta( $post->ID, '_text_field', true )!="") {
$out_of_stock = get_post_meta( $post->ID, '_text_field', true );
}
$html = '<p class="stock ' . esc_attr( $_availability['class'] ) . '">' . $out_of_stock . '</p>';
return $html;
}
add_filter( 'woocommerce_stock_html', 'change_out_of_stock_text', 10, 3 );
Seems like you have the right idea but you are't targeting the appropriate filter which is woocommerce_get_availability
function so_34126704_availability( $array, $product ){
if ( ! $_product->is_in_stock() ) && ( $status = get_post_meta( $product->id, '_text_field', true ) ) != "" ){
$availability['availability'] = $status;
}
return $array;
}
add_filter( 'woocommerce_get_availability', 'so_34126704_availability', 10, 2 );
to filter the output html you need to filter woocommerce_stock_html. it's a bit half-baked because I don't know what you'd want to do with the classes or where you are linking to, but here's a shell example of that:
function so_34126704_availability_html( $html, $_availability, $product ){
$availability = $product->get_availability(); // for some reason the $_availability does not pass the entire array, only the availability key
$status = get_post_meta( $product->id, '_text_field', true );
$html = '<p class="stock ' . esc_attr( $availability['class'] ) . '"><a href="#somewhere"/>' . esc_html( $status ) . '</a></p>';
return $html;
}
add_filter( 'woocommerce_stock_html', 'so_34126704_availability_html', 10, 3 );
I am working on woocommerce cart page with add_filter "woocommerce_in_cart_product_title", There I want to show price with title.
Like "Product title ($price)"
i have tried this code, but i can not get price.
add_filter( 'woocommerce_in_cart_product_title', 'cart_product_title', 20, 3);
function cart_product_title( $title, $values, $cart_item_key ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
print_r($items);
foreach ($items as $cart_item_key => $values) {
echo $name = $values['prodaddons'][0]['name'].' --- ';
echo $price = $values['prodaddons'][0]['price'].'<br>';
}
echo 'aaaa-'.$title.'-'.$pricees;
}
try this :
add_filter( 'woocommerce_cart_item_name', 'cart_product_title', 20, 3);
function cart_product_title( $title, $values, $cart_item_key ) {
return $title . ' - ' . $values[ 'line_total' ] . '€';
}
I don't know your version of woocommerce but woocommerce_in_cart_product_title is depreciated, it's why I use woocommerce_cart_item_name, so you should try both ;)