13

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

I am 90% there,

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

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

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

Thomas
  • 1,069
  • 4
  • 18
  • 40
user2389087
  • 1,692
  • 3
  • 17
  • 39
  • I think you found the answer, but `var_dump($order->get_items())` to see what is available in the returned array. – helgatheviking May 13 '15 at 13:50
  • 1
    I should have done the var_dump 1st, would have saved me a lot of time, good advice for future challenges – user2389087 May 13 '15 at 13:58
  • It is indeed. I `var_dump()` a lot to see what is happening. There may be a better way, but that works pretty well. Thanks for coming back and improving your answer. – helgatheviking May 13 '15 at 14:16

1 Answers1

24

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 );
kakoma
  • 1,179
  • 13
  • 17
user2389087
  • 1,692
  • 3
  • 17
  • 39
  • 1
    Is this the answer? Some words to explain it would be helpful to someone else who has a similar issue. – helgatheviking May 13 '15 at 13:50
  • Very true, my apologies , if you look here -> http://docs.woothemes.com/wc-apidocs/source-class-WC_Abstract_Order.html#1047-1117 you can see there is a ['qty'] you can also extract, i added these two together and added to the array. Then imploded with a comma, now the text looks like product1x1, product2x6 etc, what I was looking for – user2389087 May 13 '15 at 13:56