Sort object as value with int and text value - php

I have a object and have a problem, this object here $product have this keys ( product_title, product_price, product_stock ) and more, I need to sort after product_title and if my object have this products in I can't sort it right.
test product 1
test product 11
test product 111
test product 2
test product 20
test product 3
test product 33
test product 333
The right way to sort it its like this:
test product 1
test product 2
test product 3
test product 11
test product 33
test product 20
test product 111
test product 333
I have tried sort and rsort without luck.
If it can help, my object looks like this:
array(8) {
[0]=>
object(stdClass)#12 (49) {
["product_id"]=>
string(3) "335"
["product_guid"]=>
string(36) "F0161D03-33EF-6F40-C816-AEDB33640E36"
["account_id"]=>
string(2) "28"
["category_id"]=>
string(3) "189"
["brand_guid"]=>
NULL
["product_title"]=>
string(10) "Fadestativ"
["push_date"]=>
string(19) "2011-01-18 13:02:11"
["product_price"]=>
string(3) "560"
["product_price_before"]=>
string(1) "0"
["on_stock"]=>
string(1) "0"
["product_weight"]=>
string(1) "0"
["product_int_number"]=>
string(0) ""
["product_ext_number"]=>
string(0) ""
["product_small_info"]=>
string(0) ""
["product_big_info"]=>
string(31) "3 benet fad stativ højde 19 cm"
["active"]=>
string(1) "1"
["meta_title"]=>
string(0) ""
["meta_keyword"]=>
string(0) ""
["meta_description"]=>
string(0) ""
["product_new"]=>
string(1) "0"
["indicative_price"]=>
string(1) "0"
["cost_price"]=>
string(1) "0"
["id"]=>
string(2) "28"
["domain"]=>
string(26) "eventhuset.schemecloud.com"
["password"]=>
string(32) "098f6bcd4621d373cade4e832627b4f6"
["resellerid"]=>
string(1) "2"
["defualt_lang"]=>
string(2) "dk"
["quota"]=>
string(3) "100"
["account_type"]=>
string(3) "cms"
["busniess_name"]=>
string(0) ""
["busniess_street"]=>
string(0) ""
["busniess_city"]=>
string(0) ""
["busniess_zipcode"]=>
string(0) ""
["busniess_contry"]=>
string(0) ""
["busniess_phone"]=>
string(0) ""
["busniess_contact_person"]=>
string(0) ""
["busniess_contact_phone"]=>
string(0) ""
["busniess_email"]=>
string(0) ""
["busniess_cvr"]=>
string(0) ""
["invoice_name"]=>
string(0) ""
["invoice_street"]=>
string(0) ""
["invoice_city"]=>
string(0) ""
["invoice_zipcode"]=>
string(0) ""
["invoice_contry"]=>
string(0) ""
["invoice_contact_person"]=>
string(0) ""
["invoice_cvr"]=>
string(0) ""
["payment_expires"]=>
string(19) "0000-00-00 00:00:00"
["next_order_number"]=>
string(5) "10001"
["order_prefix"]=>
string(0) ""
}
[1]=>
object(stdClass)#14 (49) {
["product_id"]=>
string(3) "306"
["product_guid"]=>
string(36) "8119C253-84C7-73AC-4125-DF4B1728A203"
["account_id"]=>
string(2) "28"
["category_id"]=>
string(3) "189"
["brand_guid"]=>
NULL
["product_title"]=>
string(15) "Kartoffel skål"
["push_date"]=>
string(19) "2011-01-17 13:40:08"
["product_price"]=>
string(3) "680"
["product_price_before"]=>
string(1) "0"
["on_stock"]=>
string(1) "0"
["product_weight"]=>
string(1) "0"
["product_int_number"]=>
string(0) ""
["product_ext_number"]=>
string(0) ""
["product_small_info"]=>
string(0) ""
["product_big_info"]=>
string(31) "Kartoffel skål i rustfri stål"
["active"]=>
string(1) "1"
["meta_title"]=>
string(0) ""
["meta_keyword"]=>
string(0) ""
["meta_description"]=>
string(0) ""
["product_new"]=>
string(1) "0"
["indicative_price"]=>
string(1) "0"
["cost_price"]=>
string(1) "0"
["id"]=>
string(2) "28"
["domain"]=>
string(26) "eventhuset.schemecloud.com"
["password"]=>
string(32) "098f6bcd4621d373cade4e832627b4f6"
["resellerid"]=>
string(1) "2"
["defualt_lang"]=>
string(2) "dk"
["quota"]=>
string(3) "100"
["account_type"]=>
string(3) "cms"
["busniess_name"]=>
string(0) ""
["busniess_street"]=>
string(0) ""
["busniess_city"]=>
string(0) ""
["busniess_zipcode"]=>
string(0) ""
["busniess_contry"]=>
string(0) ""
["busniess_phone"]=>
string(0) ""
["busniess_contact_person"]=>
string(0) ""
["busniess_contact_phone"]=>
string(0) ""
["busniess_email"]=>
string(0) ""
["busniess_cvr"]=>
string(0) ""
["invoice_name"]=>
string(0) ""
["invoice_street"]=>
string(0) ""
["invoice_city"]=>
string(0) ""
["invoice_zipcode"]=>
string(0) ""
["invoice_contry"]=>
string(0) ""
["invoice_contact_person"]=>
string(0) ""
["invoice_cvr"]=>
string(0) ""
["payment_expires"]=>
string(19) "0000-00-00 00:00:00"
["next_order_number"]=>
string(5) "10001"
["order_prefix"]=>
string(0) ""
}
and I need to sort after
array(8) {
[0]=>
object(stdClass)#12 (49) {
["product_title"]=>
string(10) "Fadestativ"
}

Well, assuming that they are objects (which from your description and comments they appear to be), you can use a usort.
PHP 5.3+:
usort(
$array,
function($a, $b) {
return strnatcmp($a->title, $b->title);
}
);
PHP <= 5.2:
usort(
$array;
create_function('$a, $b', 'return strnatcmp($a->title, $b->title);`)
);
Change ->title to whatever property you want to sort by. And if you want case insensitivity, use strnatcasecmp instead of strnatcmp()...

Try sort() again but you will have to use a second parameter:
sort($array, SORT_STRING);

Related

PDO SELECT only selecting first in foreach loop

I am trying to select multiple products from my database and inserting the id into another table.
I have tried doing as below and if I just echo $name, $_POST["txtMaterial"][$key] and $_POST["txtSize"][$key] before the query, I get all selected names, materials and sizes, but when I bind the values in the query, I only get the first one.
foreach($_POST["txtName"] as $key => $name){
$sQuery = $db->prepare('SELECT id FROM products WHERE name = :sName AND material = :sMaterial AND size = :sSize');
$sQuery->bindValue(':sName', $name);
$sQuery->bindValue(':sMaterial', $_POST["txtMaterial"][$key]);
$sQuery->bindValue(':sSize', $_POST["txtSize"][$key]);
$sQuery->execute();
$aOrders = $sQuery->fetchAll();
foreach($aOrders as $aOrder){
echo print_r($aOrders);
}
}
The print_r of $aOrders is this:
"Array
(
[id] => 174
)
1"
Which is the first id that I need. Can anyone help me out how to get all the id's?
var_dump of $_POST["txtName"] before the foreach:
"array(50) {
[0]=>
string(0) ""
[1]=>
string(6) "BACURI"
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
[27]=>
string(0) ""
[28]=>
string(0) ""
[29]=>
string(6) "CAJARI"
[30]=>
string(0) ""
[31]=>
string(0) ""
[32]=>
string(0) ""
[33]=>
string(0) ""
[34]=>
string(0) ""
[35]=>
string(0) ""
[36]=>
string(0) ""
[37]=>
string(0) ""
[38]=>
string(0) ""
[39]=>
string(0) ""
[40]=>
string(0) ""
[41]=>
string(0) ""
[42]=>
string(0) ""
[43]=>
string(0) ""
[44]=>
string(0) ""
[45]=>
string(0) ""
[46]=>
string(0) ""
[47]=>
string(0) ""
[48]=>
string(0) ""
[49]=>
string(0) ""
}
"
var_dump of $_POST["txtMaterial"] before foreach:
"array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
}
"
var_dump of $_POST["txtSize"] before foreach:
"array(2) {
[0]=>
string(2) "20"
[1]=>
string(1) "4"
}
"
According provided information you getting one record ID because the only time when you prepare your statement and it is not empty is the second iteration of the loop. You have only 2 keys in associated arrays txtMaterial and txtSize, the any next ones will return null. Take a look here:
First loop
Your prepared statement will looks like this:
SELECT id FROM products WHERE name = "" AND material = 3 AND size = 20
this statement will return an empty array
Second loop
SELECT id FROM products WHERE name = "BACURI" AND material = 2 AND size = 4
This will end up with result what you get. Array with one record ID = 174.
Any next iteration...
SELECT id FROM products WHERE name = "" AND material = NULL AND size = NULL
This will return an empty array
Here is the test:
$name = ["", "BACURI", ""];
$material = ["3", "2"];
$size = ["20", "4"];
foreach($name as $key => $val){
echo "\n";
var_dump($val);
var_dump($material[$key]);
var_dump($size[$key]);
}
Output
string(0) ""
string(1) "3"
string(2) "20"
string(6) "BACURI"
string(1) "2"
string(1) "4"
string(0) ""
NULL
NULL
Hope it helps.

Extract $item code from array in k2store

I try to extract and to show on the page with echo product code field in K2store cart page (store extension for K2 for Joomla) but without success. The right value is "product_code" or "code" (is the same value): 26910.
Both "echo $item->product_name" and "echo $item->product_id" works, but "echo $item->code" and "echo $item->product_code" return NULL.
var_dump($item) show me this:
object(stdClass)#381 (13) {
["key"]=>
int(9928417)
["product_id"]=>
string(7) "9928417"
["product_name"]=>
string(71) "some title"
["product_model"]=>
NULL
["product_total"]=>
int(1)
["product_options"]=>
object(stdClass)#382 (0) {
}
["quantity"]=>
int(1)
["stock"]=>
object(JObject)#378 (18) {
["_errors":protected]=>
array(0) {
}
["item_enabled"]=>
string(1) "1"
["item_sku"]=>
NULL
["item_price"]=>
NULL
["special_price"]=>
NULL
["item_tax_id"]=>
NULL
["item_shipping"]=>
NULL
["item_qty"]=>
NULL
["item_cart_text"]=>
NULL
["product_id"]=>
string(7) "9928417"
["product_name"]=>
string(71) "some title"
["product_code"]=>
string(5) "26910"
["price"]=>
NULL
["product_sku"]=>
NULL
["tax_profile_id"]=>
NULL
["item_minimum"]=>
string(1) "1"
["stock"]=>
*RECURSION*
["product"]=>
object(stdClass)#354 (38) {
["id"]=>
string(7) "9928417"
["code"]=>
string(5) "26910"
["title"]=>
string(71) "some title"
["alias"]=>
string(71) "some title"
["catid"]=>
string(3) "694"
["published"]=>
string(1) "1"
["introtext"]=>
string(0) ""
["fulltext"]=>
string(0) ""
["video"]=>
NULL
["gallery"]=>
NULL
["extra_fields"]=>
string(29) "[{"id":"1","value":"25\/40"}]"
["extra_fields_search"]=>
string(6) "25/40 "
["created"]=>
string(19) "2012-09-14 10:20:09"
["created_by"]=>
string(2) "52"
["created_by_alias"]=>
string(0) ""
["checked_out"]=>
string(1) "0"
["checked_out_time"]=>
string(19) "0000-00-00 00:00:00"
["modified"]=>
string(19) "2012-09-14 10:30:46"
["modified_by"]=>
string(2) "52"
["publish_up"]=>
string(19) "2012-09-14 10:20:09"
["publish_down"]=>
string(19) "0000-00-00 00:00:00"
["trash"]=>
string(1) "0"
["access"]=>
string(1) "1"
["ordering"]=>
string(1) "1"
["featured"]=>
string(1) "0"
["featured_ordering"]=>
string(1) "0"
["image_caption"]=>
string(0) ""
["image_credits"]=>
string(0) ""
["video_caption"]=>
string(0) ""
["video_credits"]=>
string(0) ""
["hits"]=>
string(5) "17852"
["params"]=>
string(1190) "catItemTitle=
catItemTitleLinked=
catItemFeaturedNotice=
catItemAuthor=
catItemDateCreated=
catItemRating=
catItemImage=
catItemIntroText=
catItemExtraFields=
catItemHits=
catItemCategory=
catItemTags=
catItemAttachments=
catItemAttachmentsCounter=
catItemVideo=
catItemVideoWidth=
catItemVideoHeight=
catItemVideoAutoPlay=
catItemImageGallery=
catItemDateModified=
catItemReadMore=
catItemCommentsAnchor=
catItemK2Plugins=
itemDateCreated=
itemTitle=
itemFeaturedNotice=
itemAuthor=
itemFontResizer=
itemPrintButton=
itemEmailButton=
itemSocialButton=
itemVideoAnchor=
itemImageGalleryAnchor=
itemCommentsAnchor=
itemRating=
itemImage=
itemImgSize=
itemImageMainCaption=
itemImageMainCredits=
itemIntroText=
itemFullText=
itemExtraFields=
itemDateModified=
itemHits=
itemTwitterLink=
itemCategory=
itemTags=
itemShareLinks=
itemAttachments=
itemAttachmentsCounter=
itemRelated=
itemRelatedLimit=
itemVideo=
itemVideoWidth=
itemVideoHeight=
itemVideoAutoPlay=
itemVideoCaption=
itemVideoCredits=
itemImageGallery=
itemNavigation=
itemComments=
itemAuthorBlock=
itemAuthorImage=
itemAuthorDescription=
itemAuthorURL=
itemAuthorEmail=
itemAuthorLatest=
itemAuthorLatestLimit=
itemK2Plugins=
"
["metadesc"]=>
string(0) ""
["metadata"]=>
string(15) "robots=
author="
["metakey"]=>
string(0) ""
["plugins"]=>
string(27) "{"k2storeitem_enabled":"1"}"
["code_search"]=>
string(0) ""
["language"]=>
string(0) ""
}
}
["tax_amount"]=>
int(0)
["price"]=>
int(0)
["price_without_tax"]=>
int(0)
["total"]=>
int(0)
["total_without_tax"]=>
int(0)
}
The solution is: $item->stock->product_code

Removing a value from a cookie array

I am trying to remove a value from a cookie array:-
if (!empty($_GET['job_unfav'])) {
$value_to_delete = $_GET['job_fav']; // Cookie value to delete from array
$favouriteIDs = explode('|', $_COOKIE['job_fav']);
var_dump($favouriteIDs);
unset($favouriteIDs[$value_to_delete]);
$favouriteIDs = array_values($favouriteIDs);
setcookie('job_fav', implode('|', $favouriteIDs));
}
$favouriteIDs returns:-
array(90) {
[0]=> string(3) "542"
[1]=> string(0) ""
[2]=> string(3) "538"
[3]=> string(0) ""
[4]=> string(3) "534"
[5]=> string(0) ""
[6]=> string(3) "524"
[7]=> string(0) ""
[8]=> string(3) "516"
[9]=> string(9) "undefined"
[10]=> string(9) "undefined"
[11]=> string(9) "undefined"
[12]=> string(3) "468"
[13]=> string(0) ""
[14]=> string(0) ""
[15]=> string(0) "" [16]=> string(0) "" [17]=> string(0) "" [18]=> string(0) "" [19]=> string(0) "" [20]=> string(0) "" [21]=> string(0) "" [22]=> string(3) "468" [23]=> string(0) "" [24]=> string(3) "235" [25]=> string(3) "231" [26]=> string(0) "" [27]=> string(0) "" [28]=> string(0) "" [29]=> string(3) "235" [30]=> string(3) "231" [31]=> string(3) "228" [32]=> string(0) "" [33]=> string(0) "" [34]=> string(0) "" [35]=> string(0) "" [36]=> string(3) "235" [37]=> string(3) "231" [38]=> string(0) "" [39]=> string(0) "" [40]=> string(0) "" [41]=> string(3) "231" [42]=> string(3) "228" [43]=> string(3) "225" [44]=> string(0) "" [45]=> string(0) "" [46]=> string(0) "" [47]=> string(0) "" [48]=> string(3) "235" [49]=> string(0) "" [50]=> string(0) "" [51]=> string(0) "" [52]=> string(0) "" [53]=> string(0) "" [54]=> string(0) "" [55]=> string(3) "235" [56]=> string(3) "235" [57]=> string(0) "" [58]=> string(3) "235" [59]=> string(0) "" [60]=> string(0) "" [61]=> string(3) "235" [62]=> string(0) "" [63]=> string(0) "" [64]=> string(0) "" [65]=> string(9) "undefined" [66]=> string(0) "" [67]=> string(9) "undefined" [68]=> string(0) "" [69]=> string(3) "502" [70]=> string(0) "" [71]=> string(0) "" [72]=> string(0) "" [73]=> string(0) "" [74]=> string(0) "" [75]=> string(0) "" [76]=> string(0) "" [77]=> string(0) "" [78]=> string(0) "" [79]=> string(0) "" [80]=> string(0) "" [81]=> string(0) "" [82]=> string(0) "" [83]=> string(0) "" [84]=> string(0) "" [85]=> string(0) "" [86]=> string(0) "" [87]=> string(0) "" [88]=> string(0) "" [89]=> string(3) "514" }
At the moment this isn't removing anything, any idea how to do this?
i.e. I would like to remove all occurences of '542' from job_fav cookie but keep all other values
Here is a simple method using the array_search() function.
<?php
$favoriteIds = array("542", "","538", "", "534", "", "524" );
$value_to_delete = '542'; //$_GET['job_fav']
$idx = array_search($value_to_delete, $favoriteIds);
if ( $idx !== FALSE ) {
//if the value was found
unset($favoriteIds[$idx]);
}
print_r($favoriteIds);
The results would be
Array
(
[1] =>
[2] => 538
[3] =>
[4] => 534
[5] =>
[6] => 524
)
In your code your correctly remove the value you want from the array.
But,
In order to override the cookie you already have (so the value you remove is actually removed by exchanging the cookie) you need to add more params to the setcookie php function call you make:
From the docs:
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
Make sure the params:
path
domain
Are equal than the cookie you already have, otherwise you will just create another one or the setcookie will be ignored.
Then the expire field (which can be left as 0 if the cookie does not expire) must be set with the current timestamp, you can get using the time() function plus, the number of seconds you want the cookie to be active:
$expire = time() + NUMBER_OF_SECONDS
I hope this helps.

how to fetch value from array_chunk function

I'm using array_chunk function:
<?php
$chunks = array_chunk($coupons, 2,true);
?>
#foreach($chunks as $k => $c )
#if(2 == sizeof($c))
$c[0]['tr']->coupon_code
but the blow code not works fine :
$c[0]['tr']->coupon_code
or
$c[0]['tr']['coupon_code']
the content of var_dump($c):
array(2) { [217517]=> array(3) { ["tr"]=> object(stdClass)#71 (30) { ["id"]=> string(6) "217517" ["price"]=> string(5) "14400" ["date"]=> string(19) "2016-01-16 11:13:13" ["user_id"]=> string(5) "16433" ["ip"]=> string(14) "46.225.196.181" ["code"]=> string(13) "5699f4917b9c3" ["succ"]=> string(1) "1" ["admin_seen"]=> string(1) "0" ["user_seen"]=> string(1) "0" ["coupon_id"]=> string(5) "15373" ["coupon_parent"]=> string(5) "15315" ["coupon_code"]=> string(20) "24_1117C1_4644(1453)" ["coupon_code_user"]=> string(4) "1453" ["coupon_code_partner"]=> string(14) "24_1117C1_4644" ["shop_id"]=> string(4) "1117" ["payment_type"]=> string(1) "7" ["merchent_type"]=> string(1) "3" ["merchent_id"]=> string(1) "0" ["cradit_start_date"]=> string(19) "2015-12-29 05:05:05" ["cradit_end_date"]=> string(19) "2016-02-19 05:05:05" ["expired"]=> string(1) "0" ["pay_data"]=> NULL ["seri"]=> string(1) "C" ["to_friend"]=> string(1) "0" ["finance_id"]=> string(1) "0" ["app"]=> string(3) "web" ["expire_date"]=> string(19) "0000-00-00 00:00:00" ["expire_app"]=> string(0) "" ["buy_id"]=> string(13) "5699f4913103e" ["coupon_property_id"]=> string(3) "195" } ["offer"]=> object(stdClass)#67 (46) { ["id"]=> string(5) "15373" ["title"]=> string(21) "طرح دخترانه" ["slider_title"]=> string(0) "" ["short_title"]=> string(0) "" ["pic"]=> string(25) "Bracelets-off-Greek31.jpg" ["text"]=> string(0) "" ["attrs"]=> string(0) "" ["jest"]=> string(0) "" ["terms_of_use"]=> string(0) "" ["how_to_use"]=> string(0) "" ["city"]=> string(0) "" ["price"]=> string(5) "45000" ["off"]=> string(2) "68" ["min_users"]=> string(1) "0" ["max_users"]=> string(1) "0" ["current_users"]=> string(1) "2" ["expire"]=> string(1) "0" ["sell_start_date"]=> string(19) "2015-12-29 01:00:00" ["sell_end_date"]=> string(19) "2016-01-26 15:00:00" ["cradit_start_date"]=> string(19) "0000-00-00 00:00:00" ["cradit_end_date"]=> string(19) "0000-00-00 00:00:00" ["admin_expire_date"]=> string(19) "0000-00-00 00:00:00" ["showin_other"]=> string(1) "0" ["showin_slider"]=> string(1) "0" ["showin_slider_cat"]=> string(1) "0" ["moment"]=> string(1) "0" ["seri"]=> string(1) "C" ["shop_id"]=> string(4) "1117" ["tractions"]=> string(1) "0" ["status"]=> string(1) "0" ["url"]=> string(0) "" ["shadyab_profit"]=> string(4) "6400" ["fake_sell"]=> string(1) "3" ["confirm"]=> string(1) "0" ["adviser"]=> string(1) "0" ["parent"]=> string(5) "15315" ["description"]=> string(0) "" ["old"]=> string(1) "0" ["email_text"]=> string(0) "" ["zone"]=> string(0) "" ["views"]=> string(1) "0" ["max_buy"]=> string(1) "0" ["likes"]=> string(1) "0" ["alt"]=> string(70) "فروشگاه گیوا گالری, خرید اینترنتی کالا" ["sizes"]=> string(0) "" ["name"]=> string(11) "مدل ها" } ["details"]=> object(stdClass)#66 (22) { ["id"]=> string(6) "205585" ["tr_id"]=> string(6) "217517" ["title"]=> string(59) "دستبند چرم پلاک استیل طرح فانتزی" ["short_title"]=> string(0) "" ["pic"]=> string(17) "dastband_(2)1.jpg" ["text"]=> string(627) "
Problem here is that you preserve keys form original array. So, you don't have 0 or 1 key in a chunk $c.
Either you do not preserve keys $chunks = array_chunk($coupons, 2) as you have id in a tr object within id property and use $c[0]['tr']->coupon_code
Or you should get keys of a chunk $c with array_keys for example:
foreach($chunks as $k => $c) {
$keys = array_keys($c);
echo $c[$keys[0]]['tr']->coupon_code;
}
$c[0]['tr']['coupon_code'] is an object of type object(stdClass) .
I suppose you use json_decode before the array_chunk.
If you use json_decode($yourjson), you have to use json_decode($yourjson, true); to convert the json into array instead of stdClass.
EDIT:
Check too the keys of $c with vardump(array_keys($c)). I'm not sure $c[0] is defined.

Woocommerce WC_Order::get_items() not returning correct information

I don't know if I am doing it the wrong way or is it some bug in woocommerce but I can't get product information through the WC_Order::get_items() function as expected
My code is :
$order = new WC_Order($order_id); //$order_id = 114
$user_id = $order->user_id;
print_r($order->get_items());
die();
Response I am getting is :
Array
(
[3373] => Array
(
[name] => CLASSIC CLEANSE
[type] => line_item
[item_meta] =>
)
)
Please tell me if I am doing something wrong, as I can see in Wordpress Admin panel complete information corresponding to this order_id
output of var_dump($order); is :
object(WC_Order)#2381 (14) {
["id"]=>
int(5466)
["post"]=>
object(WP_Post)#2384 (24) {
["ID"]=>
int(5466)
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2015-02-23 14:15:42"
["post_date_gmt"]=>
string(19) "2015-02-23 13:15:42"
["post_content"]=>
string(0) ""
["post_title"]=>
string(41) "Order – Februar 23, 2015 # 02:15 PM"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(10) "wc-on-hold"
["comment_status"]=>
string(6) "closed"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(19) "order_54eb27feb27c7"
["post_name"]=>
string(32) "bestellung-23-feb-2015-um-1315-2"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2015-02-23 14:15:42"
["post_modified_gmt"]=>
string(19) "2015-02-23 13:15:42"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(56) "https:/my_site_url/?post_type=shop_order&p=5466"
["menu_order"]=>
int(0)
["post_type"]=>
string(10) "shop_order"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "1"
["filter"]=>
string(3) "raw"
}
["order_type"]=>
string(6) "simple"
["order_date"]=>
string(19) "2015-02-23 14:15:42"
["modified_date"]=>
string(19) "2015-02-23 14:15:42"
["customer_message"]=>
string(0) ""
["customer_note"]=>
string(0) ""
["post_status"]=>
string(10) "wc-on-hold"
["prices_include_tax"]=>
bool(true)
["tax_display_cart"]=>
string(4) "incl"
["display_totals_ex_tax"]=>
bool(false)
["display_cart_ex_tax"]=>
bool(false)
["formatted_billing_address":protected]=>
string(0) ""
["formatted_shipping_address":protected]=>
string(0) ""
}
Oh! It was my mistake.
I was calling this code from a custom function to test it, even before the woocommerce was initialized, so the code was not working

Categories