skip to Main Content

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


  1. 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:

    // Give the callback function a clear and descriptive name.
    function wpse_get_customer_order_names() {
      
        // Can you be certain the user is logged in at this stage...
        // Consider how you might want to validate the user.
    
        // Get all orders for the current user.
        $customer_orders = wc_get_orders([
            'customer_id' => get_current_user_id(),
        ]);
    
        // Transform the array of order objects into an array of order names.
        $order_names = array_map( function( $order ) {
            return $order->name;
        }, $customer_orders );
    
        // Return as a string, ready for output,
        return implode( ', ', $order_names );
    }
    add_shortcode( 'prcsed_order_name' , 'wpse_get_customer_order_names' );
    

    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.

    Login or Signup to reply.
  2. 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

    add_action('init', 'xp33221_add_custom_shortcode');
    
    function xp33221_add_custom_shortcode()
    {
        add_shortcode( 'prcsed_order_numb' , 'prcsed_order_1' );
        add_shortcode( 'prcsed_order_name' , 'prcsed_order_2' );
    }
    
    function getCustOrder(int $orderNum): ?WC_Order
    {
        $order = wc_get_order($orderNum);
    
        if ($order instanceof WC_Order)) {
           return $order;
        }  
    
        return null;
    }
    
    function prcsed_order_1(): ?int
    {
        $order = getCustOrder($customer_order);
        
        return $order instanceof WC_Order 
            ? $order->get_order_number() 
            : $order;
    }
    
    function prcsed_order_2() null|mixed
    {
        $order = getCustOrder($customer_order);
        
        return $order instanceof WC_Order 
            ? $order->get_order_key() 
            : $order;   
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search