📖 Scenario: You are building a simple WordPress plugin to manage customer orders on a website. The plugin will store orders, set a status filter, display filtered orders, and finalize the order list display.
🎯 Goal: Create a WordPress plugin that stores a list of orders, filters them by status, loops through the filtered orders to display them, and completes the display with a summary message.
📋 What You'll Learn
Create an array called $orders with 3 orders, each order is an associative array with keys 'id', 'product', and 'status'
Create a variable called $filter_status and set it to the string 'completed'
Use array_filter with a callback function to create a new array $filtered_orders containing only orders with 'status' equal to $filter_status
Use a foreach loop to iterate over $filtered_orders and echo each order's 'id' and 'product' inside a paragraph tag
After the loop, echo a final paragraph with the text 'All completed orders are shown.'
💡 Why This Matters
🌍 Real World
Managing and displaying customer orders on a WordPress website helps store owners track order statuses and show relevant information to customers or admins.
💼 Career
Understanding how to manipulate arrays, filter data, and display dynamic content is essential for WordPress plugin development and backend PHP programming.
Progress0 / 4 steps
1
Create the orders array
Create an array called $orders with exactly these three orders: ['id' => 101, 'product' => 'T-shirt', 'status' => 'completed'], ['id' => 102, 'product' => 'Jeans', 'status' => 'pending'], and ['id' => 103, 'product' => 'Hat', 'status' => 'completed'].
Wordpress
Hint
Use a PHP array with three associative arrays inside, each with keys 'id', 'product', and 'status'.
2
Set the filter status
Create a variable called $filter_status and set it to the string 'completed'.
Wordpress
Hint
Just assign the string 'completed' to the variable $filter_status.
3
Filter orders by status
Use array_filter with a callback function to create a new array called $filtered_orders that contains only orders from $orders where the 'status' equals $filter_status.
Wordpress
Hint
Use array_filter with an anonymous function that checks if $order['status'] equals $filter_status.
4
Display filtered orders and final message
Use a foreach loop to iterate over $filtered_orders and echo each order's 'id' and 'product' inside a paragraph tag. After the loop, echo a paragraph with the text 'All completed orders are shown.'.
Wordpress
Hint
Use a foreach loop to echo each order's id and product inside <p> tags, then echo the final message in another <p> tag.
Practice
(1/5)
1. What is the primary purpose of the wc_get_orders() function in WooCommerce order management?
easy
A. To create a new product in the store
B. To retrieve a list of orders based on specified criteria
C. To delete a customer account
D. To update the site theme settings
Solution
Step 1: Understand the function name and context
The function wc_get_orders() is related to WooCommerce and order management, so it deals with orders.
Step 2: Identify the function's role
This function is used to retrieve orders, often filtered by criteria like status or date.
Final Answer:
To retrieve a list of orders based on specified criteria -> Option B
Quick Check:
Order retrieval = A [OK]
Hint: Remember 'get' means fetch or retrieve data [OK]
Common Mistakes:
Confusing order retrieval with product creation
Thinking it updates or deletes orders
Mixing it up with user account functions
2. Which of the following is the correct way to get all completed orders using wc_get_orders()?
easy
A. wc_get_orders(array('status' => 'completed'));
B. wc_get_orders('completed');
C. wc_get_orders(status='completed');
D. wc_get_orders(['state' => 'completed']);
Solution
Step 1: Check the function parameter format
wc_get_orders() expects an array of arguments, so the parameter must be an array.
Step 2: Verify the correct key for order status
The correct key to filter by order status is 'status', not 'state'.
Final Answer:
wc_get_orders(array('status' => 'completed')); -> Option A
Quick Check:
Array with 'status' key = B [OK]
Hint: Use array with 'status' key for filtering orders [OK]