I’m trying to build several shortcodes that display some information like name and order number of all orders placed by a user.
These are the shortcodes. The first one returns 0 as a value, I don’t understand where I’m wrong. The second displays no value. Can anyone help me by pointing out the errors?
I need to work with shortcodes because I am recreating the orders.php page
// Get All Orders numbers of current user
add_shortcode( 'prcsed_order_numb' , 'prcsed_order_1' );
function prcsed_order_1(){
$customer = new WC_Customer( get_current_user_id() );
$order = new WC_Order( $customer_order );
if ( is_a( $order, 'WC_Order' ) ) {
return $order->get_order_number();
}
}
// Get All Orders Name of current user
add_shortcode( 'prcsed_order_name' , 'prcsed_order_2' );
function prcsed_order_2(){
// Get an instance of the WC_Order object
$order = new WC_Order( $customer_order );
foreach ( $customer_orders->orders as $customer_order ) {
return $order = $item->get_name();
}
}
2
Answers
I’d strongly advise taking a cautious approach to implementing this solution. The code you’ve posted contains a number of issues and it’s going to be handling sensitive customer data. Getting this wrong could result in a data leak.
In the callback,
prcsed_order_2()
, you’re attempting to use variables which don’t exist. You’re also returning in a foreach loop so it’ll never make it past the first loop iteration.Example:
The same principles can be applied to other related shortcodes that touch upon customer order data. In addition to the security issue previously raised, I’d question the performance impact of implementing things in this manner.
Huge amount of errors here, the return in the FOR loop would just return the 1st iteration, and variables that dont seem to be initialised etc…. but for testing
You may be calling the shortcode to early try; I tweaked methods just for my own testing