Change Woocommerce order number to specific number of digits - php

I need to change the Woocommerce order number from 4 to 5 digits. I know I can a prefix or suffix with this
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number', 1, 2);
function change_woocommerce_order_number( $order_id, $order ) {
$prefix = 'WC';
$suffix = '-XY';
return $prefix . $order->id . $suffix;
}
But how can I change the number of digits?

Use str_pad() PHP function as follows:
add_filter( 'woocommerce_order_number', 'customize_order_number', 10, 2 );
function customize_order_number( $order_id, $order ) {
$digits = 5;
$prefix = '';
$suffix = '';
return $prefix . str_pad($order_id, $digits, '0', STR_PAD_LEFT) . $suffix;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related: Formatting a number with leading zeros in PHP

Related

Woocommerce | Add character to order number based on login

Is it possible when a registered user places an order that certain characters appear before the order number? Now I have found something that applies this to all orders. But it should only be on orders made from a user.
function filter_woocommerce_order_number( $order_number, $order ) {
if ( is_user_logged_in() ) {
$prefix = 'B2B-';
} else {
$prefix = '';
}
return $prefix . $order_number;
}
add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 );

Reduce product long description in Woocommerce

I have found the following code from this answer thread, but it only applied under the product title. So how can apply to the detailed description of the product.
add_action( 'woocommerce_after_shop_loop_item_title', 'shorten_product_excerpt', 35 );
function shorten_product_excerpt()
{
global $post;
$limit = 14;
$text = $post->post_excerpt;
if (str_word_count($text, 0) > $limit) {
$arr = str_word_count($text, 2);
$pos = array_keys($arr);
$text = substr($text, 0, $pos[$limit]) . '...';
// $text = force_balance_tags($text); // may be you dont need this…
}
echo '<span class="excerpt"><p>' . $text . '</p></span>';
}
In Woocommerce product single pages, the long description is displayed in the "description tab". If you look at the source code of single-product/tabs/description.php template, it use the_content() wordpress function to display that long description.
So you can use the_content dedicated Wordpress filter hook to reduce the product long description:
add_filter( 'the_content', 'shorten_product_long_descrition', 20 );
function shorten_product_long_descrition( $content ){
// Only for single product pages
if( ! is_product() ) return $content;
// Set the limit of words
$limit = 14;
if (str_word_count($content, 0) > $limit) {
$arr = str_word_count($content, 2);
$pos = array_keys($arr);
$text = '<p>' . substr($content, 0, $pos[$limit]) . '...</p>';
$content = force_balance_tags($text); // needded
}
return $content;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Before:
After:
Similar: Limit product short description length in Woocommerce

Adding suffix and prefix to WooCommerce order number without using a plugin

I want to add a suffix and a prefix to a Woo Commerce order number without using a plugin.
I tried to use this hook which is not working:
add_filter( 'woocommerce_order_number','my_woocommerce_order_number', 1, 2);
function my_woocommerce_order_number( $oldnumber, $order ) {
return 'VC'.$order->id;
}
How can I achieve this?
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number', 1, 2);
function change_woocommerce_order_number( $order_id, $order ) {
$prefix = '#SAM-';
$suffix = '-' . date(Y);
// You can use either one of $order->id (or) $order_id
// Both will work
return $prefix . $order->id . $suffix;
}
Refernce: https://docs.woocommerce.com/wc-apidocs/source-class-WC_Order.html#379

How to truncate characters in product description with WooCommerce

I have found out how to add a short description to the thumbnails for products in WooCommerce, but how do I truncate them to a certain length, say 30 characters.
All the answers to do with editing the functions.php file dont mention where in the file to put the code.
My code in my functions.php file is:
add_action('woocommerce_after_shop_loop_item_title','add_title_description',9);
function add_title_description(){
echo get_post_meta($product->id, 'title-description', true)? '<span class="title-description">' . get_post_meta($product->id, 'title-description', true) . '</span><br />' : '';
}
use substr()
add_action('woocommerce_after_shop_loop_item_title','add_title_description',9);
function add_title_description()
{
$titleDescription = get_post_meta($product->id, 'title-description', true);
if( !empty($titleDescription) )
{
if( strlen($titleDescription) > 30 )
$titleDescription = substr($titleDescription, 30);
printf('<span class="title-description">%s</span><br />', $titleDescription);
}
}
My implementation grabbing the first 2 sentences instead of a character count.
/**
* Echo Truncated String
*
* #param string $string
* #return string
*/
function add_title_description( $titleDescription ) {
global $product;
$titleDescription = get_post_meta( $product->id, 'title-description', true );
// combine first 2 sentences
$sentence = preg_split( '/(\.|!|\?)\s/', $titleDescription, 3, PREG_SPLIT_DELIM_CAPTURE );
echo $titleDescription ? '<span class="title-description">' . $sentence[0] . $sentence[3] . '</span><br />' : '';
}
add_action( 'woocommerce_after_shop_loop_item_title', 'add_title_description', 4, 1 );
I added this:
function wcs_excerpt_length( $length ) {
return 15;
}
add_filter( 'product_description_length', 'wcs_excerpt_length' );

execute function IF criteria is met - wordpress

At the moment i have a function and a filter which shortens titles if they are longer than 25characters and appends the triple dot '...'
However, it appends the dots to all the titles. How can i get the following code to run only if the title is longer than 25 characters?
function japanworm_shorten_title( $title ) {
$newTitle = substr( $title, 0, 25 ); // Only take the first 25 characters
return $newTitle . " …"; // Append the elipsis to the text (...)
}
add_filter( 'the_title', 'japanworm_shorten_title', 10, 1 );
You can use PHP's strlen: http://php.net/manual/en/function.strlen.php
Your code would go like:
function japanworm_shorten_title( $title ) {
if (strlen($title) > 25){
$title = substr( $title, 0, 25 ).'…';
}
return $title;
}
add_filter( 'the_title', 'japanworm_shorten_title', 10, 1 );

Categories