WC_Order get_items() 及其数量 [英] WC_Order get_items() and their Quantity
问题描述
我正在尝试构建一个应用程序,它会向我发送我的 woo 商业订单、订单项目和数量,
I am trying to build an application which texts me my woo commerce order, order items and Quantity,
我 90% 在那里,
function custom_woocommerce_complete_order_sms( $order_id ) {
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$product_list = '';
$order_item = $order->get_items();
foreach( $order_item as $product ) {
$prodct_name[] = $product['name'];
}
$product_list = implode( ',\n', $prodct_name );
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) {
$sms = $client->account->messages->sendMessage(
"+44xxxxxxxxxx",
// the number we are sending to - Any phone number
$number,
// the sms body
"Hey $name, there is a new Order, the order is, $product_list"
);
}
}
我的问题是我不知道如何获取项目 Quantity ,例如我的文本看起来像列表、项目 1、项目 2、项目 3,我希望它说项目 1 x1、项目 2 x2、项目 3 x3
My problem is I do not know how to get the item Quantity , for example my text looks like list, item 1, item 2, item 3, I want it to say item 1 x1, item 2 x2, item3 x3
我确实尝试深入研究了抽象 woo commerce 文件夹中的电子邮件 php 文件,以了解他们在通过电子邮件发送数量时是如何做的,但有点迷失
I did try and dig into the email php file in abstract woo commerce folder to see how they do as they send Quantities in emails but got a little lost
也在 WC_Abstract_Order 类中,我唯一能找到的其他东西是 get_item_total
,它返回所有项目的总数
also in the class WC_Abstract_Order the only other thing I could find is get_item_total
which returns to the total of all items
推荐答案
从研究中你也可以从订单项中获取数量
From research you can also grab the qty from the order item
$product['qty'];
因此,循环并将数量添加到项目名称中很简单(下)
Therefore , it was simple to loop over and add the quantity to the item name (below)
$product_details = array();
$order_items = $order->get_items();
foreach( $order_items as $product ) {
$product_details[] = $product['name']."x".$product['qty'];
}
$product_list = implode( ',', $product_details );
这篇关于WC_Order get_items() 及其数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!