php WC_Order get_items() 及其数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30214457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:37:55  来源:igfitidea点击:

WC_Order get_items() and their Quantity

phpwoocommerce

提问by user2389087

I am trying to build an application which texts me my woo commerce order, order items and Quantity,

我正在尝试构建一个应用程序,它会向我发送我的 woo 商业订单、订单项目和数量,

I am 90% there,

我在那里 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"
        );


    }

    }

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

我的问题是我不知道如何获取项目 Quantity ,例如我的文本看起来像列表、项目 1、项目 2、项目 3,我希望它说项目 1 x1、项目 2 x2、项目 3 x3

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

我确实尝试深入研究抽象 woo commerce 文件夹中的电子邮件 php 文件,看看他们在电子邮件中发送数量时是如何做的,但有点迷失了

also in the class WC_Abstract_Order the only other thing I could find is get_item_totalwhich returns to the total of all items

同样在 WC_Abstract_Order 类中,我唯一能找到的另一件事是get_item_total返回所有项目的总数

回答by user2389087

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 );