John Davidson

php - Woocommerce | Add character to order number based on login

0 comments
Message:


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 );


My Answer:
Yes, it is possible to add certain characters to the order number based on whether the user is logged in or not. You can modify the code snippet you provided to check if the order is made by a logged-in user before adding the prefix. Here is an updated version of the code:

php
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 );


This code will add the prefix 'B2B-' to the order number only if the order is made by a logged-in user. If the order is made by a guest user, the prefix will not be added.

Rate this post

3 of 5 based on 3621 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us