WC_Order get_items() and their Quantity
When working with WooCommerce hooks, it is common to need to extract specific data points from an order object to format custom notifications. In your current implementation using WC_Order, you successfully retrieve the list of items via get_items(). However, the default iteration only exposes the product name, SKU, and price. To display quantities like "Item 1 x1, Item 2 x2", you need to access the quantity property directly within the item array returned by the method.
The get_items() function returns an array of objects that inherit from WC_Order_Item_Product. While these objects can be accessed as associative arrays in many contexts, relying on key names like 'name' is not always guaranteed across all WooCommerce versions or custom themes. The most robust approach is to use the property names defined in the class structure.
To include the quantity, you should iterate through the items and access the get_quantity() method or the ['qty'] key if treating the item as an array. Here is how you can modify your loop to capture this data:
function custom_woocommerce_complete_order_sms( $order_id ) {
global $woocommerce;
if ( !$order_id ) {
return;
}
$order = new WC_Order( $order_id );
// It is safer to use the items collection directly for modern code compatibility
$items = $order->get_items();
$product_list = '';
foreach ( $items as $item ) {
// Access quantity via method or array key depending on strictness needs.
// get_quantity() is the standard object-oriented approach.
$quantity = $item->get_quantity();
// Alternatively, if using array syntax: $quantity = $item['qty'];
$product_name = $item->get_name(); // Preferred over $item['name'] for consistency
// Format the string as requested: "Name x Quantity"
$product_list .= sprintf( "%s x%d\n", $product_name, $quantity );
}
// Clean up trailing newline if necessary before sending SMS
if ( ! empty( $product_list ) ) {
$product_list = trim( $product_list );
}
require "twilio-php-master/Services/Twilio.php";
$AccountSid = "xxxxxxxxx";
$AuthToken = "xxxxxxxxx";
$client = new Services_Twilio($AccountSid, $AuthToken);
$people = array(
"xxxxxxxxxx" => "Me",
);
foreach ( $people as $number => $name ) {
// Ensure the SMS body is not empty to avoid API errors
if ( ! empty( $product_list ) ) {
$sms = $client->account->messages->sendMessage(
"+44xxxxxxxxxx",
$number,
"Hey $name, there is a new Order.\n\n$product_list"
);
}
}
}
Understanding the Data Structure
The confusion often stems from how WooCommerce encapsulates order data. The WC_Order_Item_Product class holds the quantity in a protected property $qty. When you cast the object to an array (which happens implicitly when using bracket notation like $item['name']), it exposes specific keys defined in the get_array() method of that class.
One common misconception is looking for a key named 'quantity'. In standard WooCommerce implementations, the array key used internally is often 'qty', but relying on magic constants or specific internal array keys can break if your environment uses a different version of WooCommerce or a plugin that overrides these methods. Using $item->get_quantity() abstracts this away and ensures compatibility regardless of how the object is serialized.
Trade-offs in SMS Formatting
When formatting strings for SMS, brevity is key. The sprintf function used above creates a clean "Name x Quantity" pattern. However, you must be mindful of character limits imposed by carriers or Twilio's pricing tiers if messages exceed 160 characters (GSM 7-bit encoding). If the order contains many high-volume items, consider truncating long product names or grouping them differently to keep the message within a single SMS block.
Additionally, note that get_item_total returns the calculated subtotal for a specific item based on quantity and price. While useful for financial reports, it is rarely needed for simple notification logs unless you are sending receipts where the user needs to verify costs. For a "New Order" alert, the focus should remain on what was bought and how much, not necessarily the total cost per line item, unless specified by your business logic.
If you prefer managing complex order notifications or need deeper integration with Twilio's advanced features like read receipts or media attachments, platforms like LaravelCompany offer pre-built modules that simplify these webhook integrations without requiring raw Twilio SDK handling in custom PHP files.