functions - how to handle multiple forloop?

admin2025-06-07  23

I have and order_id as array but wc_get_order accepts only one id at a time how to loop through order_id and let the list of item id and item name? i am using this inside a class. it works fine if single id is passed as shown below

 public function getStuffDone()
            {
                order_id =array(358,368)

                $order = wc_get_order(358);//only single id is passed here
                $items = $order->get_items();
                foreach ($order->get_items() as $item_key => $item_values):
                    $item_id      = $item_values->get_id();
                    $item_name    = $item_values->get_name();
                endforeach;
                return $item_name;
            }

please help .thanks

I have and order_id as array but wc_get_order accepts only one id at a time how to loop through order_id and let the list of item id and item name? i am using this inside a class. it works fine if single id is passed as shown below

 public function getStuffDone()
            {
                order_id =array(358,368)

                $order = wc_get_order(358);//only single id is passed here
                $items = $order->get_items();
                foreach ($order->get_items() as $item_key => $item_values):
                    $item_id      = $item_values->get_id();
                    $item_name    = $item_values->get_name();
                endforeach;
                return $item_name;
            }

please help .thanks

Share Improve this question asked Oct 17, 2018 at 12:31 user145078user145078
Add a comment  | 

1 Answer 1

Reset to default 0

You can do something like this

public function getStuffDone() {

    $order_ids = array( 358,368 );

    $items = array(); // contains the names

    foreach ( $order_ids as $order_id ) {

        $order = wc_get_order( $order_id );

        foreach ( $order->get_items() as $item_values ) {

            $items[ $order_id ][] = $item_values->get_name(); // add the name to the array
        }
    }

    return $items; // array containing all the names
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749254974a317652.html

最新回复(0)