I’m trying to modify the word “reservado” or “backordered” in the order details page.
I use the following code, unfortunately without the desired result. The "backordered" text does not change, any advice?
function custom_backorder_message( $text, $product ){
if ($product->is_on_backorder( 0 ) ) {
$text = __( 'This item may take 3-4 weeks to deliver' );
}
return $text;
}
add_filter( 'woocommerce_get_availability_text', 'custom_backorder_message', 10, 2 );
If you want to change it via code you can use the woocommerce_backordered_item_meta_name filter hook.
So you get:
function filter_woocommerce_backordered_item_meta_name( $string, $item ) {
// Replace with new text
$string = 'My new text';
return $string;
}
add_filter( 'woocommerce_backordered_item_meta_name', 'filter_woocommerce_backordered_item_meta_name', 10, 2 );
But you could also change it in the language file.
Related
Currently I'm using Woocommerce Short_Description in Details Order answer code to show the product excerpt on WooCommerce checkout page:
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 $excerpt . $item_name ;
}
Then I try to sort the order items alphabetically, based on the product excerpt. Via How to sort WooCommerce order items alphabetically i was able to write following code:
/*
* Filters the $order->get_items() method results to order all line items by
* product name
*/
add_filter( 'woocommerce_order_get_items', function( $items, $order ) {
uasort( $items,
function( $a, $b ) {
return strnatcmp( $a['excerpt'], $b['excerpt'] );
}
);
return $items;
}, 10, 2 );
With this code I don't get any error messages, but I don't get the desired result either. Any advice how to sort excerpt by alphabetical order?
To display the short product description you can use:
function filter_woocommerce_order_item_name( $item_name, $item, $is_visible ) {
// Get the product ID
$product_id = $item->get_product_id();
// Get the short description
$excerpt = get_the_excerpt( $product_id );
// NOT empty
if ( ! empty ($excerpt ) ) {
return $excerpt . ' - ' . $item_name;
}
return $item_name;
}
add_filter( 'woocommerce_order_item_name', 'filter_woocommerce_order_item_name', 10, 3 );
Note: because the woocommerce_order_item_name hook is executed on multiple pages (as well as WooCommerce email notifications) you could use conditional tags
Regarding sorting, $a['excerpt'] does not exist, you can replace it with get_the_excerpt( $a['product_id'] )
So you get:
function filter_woocommerce_order_get_items( $items, $order, $types ) {
// Sort an array with a user-defined comparison function and maintain index association
uasort( $items, function( $a, $b ) {
// String comparisons using a "natural order" algorithm
return strnatcmp( get_the_excerpt( $a['product_id'] ), get_the_excerpt( $b['product_id'] ) );
} );
return $items;
}
add_filter( 'woocommerce_order_get_items', 'filter_woocommerce_order_get_items', 10, 3 );
Is there a hook to change the Total title from the WooCommerce order received page's order overview? Please see the below image to understand better
Yes, you can use the gettext WordPress filter to "translate" (rename in your case) that string of text.
Your string is inside the thankyou.php WooCommerce template:
esc_html_e( 'Total:', 'woocommerce' );
Based on WooCommerce: How to Translate / Rename Any String tutorial, the right code should be the following:
add_filter( 'gettext', 'bbloomer_translate_woocommerce_strings', 9999, 3 );
function bbloomer_translate_woocommerce_strings( $translated, $untranslated, $domain ) {
if ( ! is_admin() && 'woocommerce' === $domain ) {
switch ( $translated ) {
case 'Total:':
$translated = 'Whatever:';
break;
}
}
return $translated;
}
I've been trying to get rid of our exact stock numbers on product pages on our wordpress/woocommerce website. Ideally it shows In stock, Only 1 or 2 left in stock when theres only 1 or 2 available, or just sold out. I've tried many of the code snippets found here on stack overflow but none of them seem to change anything. Also the standard woocommerce settings at Products > Inventory to don't show any stock numbers doesn't do anything. On the front end it keeps showing Availability: Exact stock amount. Can anyone help me out?
Adding these kind of snippets to my child theme's functions.php or even in a code snippet plugin doesn't seem to do anything for me:
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
// Change In Stock Text
if ( $_product->is_in_stock() ) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change Out of Stock Text
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('Sold Out', 'woocommerce');
}
return $availability;
}
A better way is to use woocommerce_get_availability_text instead of woocommerce_get_availability. This filters only the availability text and is called after woocommerce_get_availability.
add_filter( 'woocommerce_get_availability_text', 'set_custom_availability_text', 10, 2 );
function set_custom_availability_text( $availability, $product ) {
if ( $product->is_in_stock() ) {
$availability = __( 'Available!', 'woocommerce' );
} elseif ( ! $product->is_in_stock() ) {
$availability = __( 'Out of stock', 'woocommerce' );
}
return $availability;
}
If the above code doesn't do anything, test if the filter is being called by using the die() function. This will stop the execution of any code that comes after it and prints whatever string you put in between the parentheses:
add_filter( 'woocommerce_get_availability_text', 'set_custom_availability_text_test', 10, 2 );
function set_custom_availability_text_test( $availability, $product ) {
die('Filter is called');
}
So if the filter is called correctly your page should render up to the availabitly text and display 'Filter is called' where the availabilty text should be. If nothing happens this means the filter isn't called. So then check if your functions.php is being loaded, or use a plugin like Code Snippets.
This snippet help you to change text for all product or particular one:
<?php
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability',1,2);
function wcs_custom_get_availability($availability,$_product ) {
global $product;
switch($_product->slug){
// Enter product slug for case
case "test-1":
// text for in stock mode
if($_product->manage_stock = true){
$availability['availability'] = str_replace($availability['availability'],"Enter your custom text here $_product->stock_quantity",$availability['availability']);
}
// text for out of stock mode
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('Enter your custom text here', 'woocommerce');
}
break;
case "test2":
if($_product->manage_stock = true){
$availability['availability'] = str_replace($availability['availability'],"Enter your custom text here $_product->stock_quantity",$availability['availability']);
}
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __(' Enter your custom text here', 'woocommerce');
}
break;
case "test3":
if($_product->manage_stock = true){
$availability['availability'] = str_replace($availability['availability'],"Enter your custom text here $_product->stock_quantity نفر",$availability['availability']);
}
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __(' Enter your custom text here', 'woocommerce');
}
break;
}
return $availability;
}
I have a wordpress website where customers make an image with text and icons, once processed thru woocommerce and payed for that image name 12345.png is saved as Customer_product_image
function add_order_item_meta($item_id, $values) {
$key = 'customer_product_image'; // Define your key here
$value = $values['user_img']; // Get your value here
woocommerce_add_order_item_meta($item_id, $key, $value);
}
And i works great, but now i'm banning my head against the wall! When the purchased image is displayed on the Order admin detail page, it shows up as CUSTOMER_PRODUCT_IMAGE: 1234.png how on earth would i go about wrapping that within an image tag so the image is displayed there?
I've searched high and low on google but haven't been able to find anything, its probably that i dont know what do actually search for....
This did the trick for me!
First i added this snippet for removing the custom meta item on order detail render:
add_filter( 'woocommerce_hidden_order_itemmeta', 'hide_order_item_meta_fields' );
function hide_order_item_meta_fields( $fields ) {
$fields[] = 'current_view';
$fields[] = 'custom_image';//Add all meta keys to this array,so that it will not be displayed in order meta box
return $fields;
}
second i added it back with this, and with the desired text and image tag:
add_action( 'woocommerce_after_order_itemmeta', 'order_meta_customized_display',10, 3 );
function order_meta_customized_display( $item_id, $item, $product ){
$all_meta_data=get_metadata( 'order_item', $item_id, "", "");
$useless = array(
"_qty","_tax_class","_variation_id","_product_id","_line_subtotal","_line_total","_line_subtotal_tax","_line_tax","_line_tax_data"
);// Add key values that you want to ignore
$customized_array= array();
foreach($all_meta_data as $data_meta_key => $value) {
if(!in_array($data_meta_key,$useless)){
$newKey = ucwords(str_replace('_'," ",$data_meta_key ));//To remove underscrore and capitalize
$customized_array[$newKey]=ucwords(str_replace('_'," ",$value[0])); // Pushing each value to the new array
}
}
if (!empty($customized_array)) {
foreach($customized_array as $data_meta_key => $value){
echo "<div class='product_container'><span>Produkt Billede: </span><img src='".wp_upload_dir()['baseurl'].'/flapper/'. $value ."' /> </div>";
}
}
}
i found the answer to this question on this page
You can use the filter woocommerce_order_item_display_meta_value to output the image. Place this code in your functions.php file, you'll need to modify the src attribute of the img tag to include the appropriate URL before the filename value. You can also modify the display label with the filter woocommerce_order_item_display_meta_key
add_filter( 'woocommerce_order_item_display_meta_value', 'modify_order_item_display_value' , 10, 3 );
function modify_order_item_display_value( $display_value, $meta, $wc_order_item ) {
$meta_data = $meta->get_data();
if( $meta_data['key'] === 'customer_product_image' ) {
return '<img src="' . $meta_data['value'] . '">';
}
return $display_value;
}
add_filter('woocommerce_order_item_display_meta_key', 'modify_order_item_display_key', 10, 3);
function modify_order_item_display_key( $display_key, $meta, $wc_order_item ) {
$meta_data = $meta->get_data();
if( $meta_data['key'] === 'customer_product_image' ) {
return 'Customer Image';
}
return $display_key;
}
I want to add a product short description by default whenever is the new product is being created. All the products will have the same short description, so there is no point keep copying and pasting it. So it should just be there when I click on the add a new product.
I would appreciate any help.
add_filter( 'woocommerce_short_description',
'single_product_short_description', 10, 1 );
function single_product_short_description( $post_excerpt )
{
global $product;
if ( is_single( $product->id ) )
$post_excerpt = '<div class="product-message"><p>' . __( "Article only
available in the store.", "woocommerce" ) . '</p></div>' . $post_excerpt;
return $post_excerpt;
}
I found the above code but couldn't get it to work :(
Thank you.
Regards,
Emre.
Add this code inside your themes function.php
Change the short description content as per your need - "Here goes your short desc."
add_filter( 'wp_insert_post_data' , 'cdx_add_product_short_desc' , '99', 1 );
function cdx_add_product_short_desc( $data )
{
//only for product post type
if($data['post_type'] == 'product' ) {
//only if short description is not present
if( '' == trim($data['post_excerpt']) ):
$short_desc = 'Here goes your short desc.';
$data['post_excerpt'] = $short_desc ;
endif;
}
// Returns the modified data.
return $data;
}
This will work to automatically override anything put into a product's short description field on the front-end only. It will not add the text to the backend field itself, which is good because it keeps it globalized if you need to change it later.
add_filter( 'woocommerce_short_description', 'filter_woocommerce_short_description', 10, 1 );
function filter_woocommerce_short_description( $post_excerpt ) {
$post_excerpt = '<div class="product-message"><p>' . __( "Article only available in the store.", "woocommerce" ) . '</p></div>';
return $post_excerpt;
}