How to access protective array? [duplicate] - php

This question already has answers here:
Get Order items and WC_Order_Item_Product in WooCommerce 3
(2 answers)
Closed 5 years ago.
I have woocommrece order detail array which had protective element so when like normal array you can't access them via their key .This array is result of the plugin file code which i have purchased.
I have customize this code so many time but the protective array do not let me access into it. Any idea
Array
(
[1251] => WC_Order_Item_Product Object
(
[extra_data:protected] => Array
(
[product_id] => 0
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 0
[subtotal_tax] => 0
[total] => 0
[total_tax] => 0
[taxes] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
)
[data:protected] => Array
(
[order_id] => 2040
[name] => Vanilla Pastries
[product_id] => 776
[variation_id] => 0
[quantity] => 5
[tax_class] => gst_18
[subtotal] => 296.6
[subtotal_tax] => 53.388
[total] => 296.6
[total_tax] => 53.388
[taxes] => Array
(
[total] => Array
(
[5] => 26.694
[10] => 26.694
)
[subtotal] => Array
(
[5] => 26.694
[10] => 26.694
)
)
)
[cache_group:protected] => order-items
[meta_type:protected] => order_item
[object_type:protected] => order_item
[id:protected] => 1251
[changes:protected] => Array
(
)
[object_read:protected] => 1
[default_data:protected] => Array
(
[order_id] => 0
[name] =>
[product_id] => 0
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 0
[subtotal_tax] => 0
[total] => 0
[total_tax] => 0
[taxes] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
)
[data_store:protected] => WC_Data_Store Object
(
[instance:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store Object
(
[internal_meta_keys:protected] => Array
(
[0] => _order_id
[1] => _name
[2] => _product_id
[3] => _variation_id
[4] => _quantity
[5] => _tax_class
[6] => _subtotal
[7] => _subtotal_tax
[8] => _total
[9] => _total_tax
[10] => _taxes
[11] => _product_id
[12] => _variation_id
[13] => _qty
[14] => _tax_class
[15] => _line_subtotal
[16] => _line_subtotal_tax
[17] => _line_total
[18] => _line_tax
[19] => _line_tax_data
)
[meta_type:protected] => order_item
[object_id_field_for_meta:protected] => order_item_id
)
[stores:WC_Data_Store:private] => Array
(
[coupon] => WC_Coupon_Data_Store_CPT
[customer] => WC_Customer_Data_Store
[customer-download] => WC_Customer_Download_Data_Store
[customer-session] => WC_Customer_Data_Store_Session
[order] => WC_Order_Data_Store_CPT
[order-refund] => WC_Order_Refund_Data_Store_CPT
[order-item] => WC_Order_Item_Data_Store
[order-item-coupon] => WC_Order_Item_Coupon_Data_Store
[order-item-fee] => WC_Order_Item_Fee_Data_Store
[order-item-product] => WC_Order_Item_Product_Data_Store
[order-item-shipping] => WC_Order_Item_Shipping_Data_Store
[order-item-tax] => WC_Order_Item_Tax_Data_Store
[payment-token] => WC_Payment_Token_Data_Store
[product] => WC_Product_Data_Store_CPT
[product-grouped] => WC_Product_Grouped_Data_Store_CPT
[product-variable] => WC_Product_Variable_Data_Store_CPT
[product-variation] => WC_Product_Variation_Data_Store_CPT
[shipping-zone] => WC_Shipping_Zone_Data_Store
)
[current_class_name:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store
[object_type:WC_Data_Store:private] => order-item-product
)
[meta_data:protected] => Array
(
)
)
)

The object you want to access has the type WC_Order_Item_Product. Use its public methods to query it for data or tell it to do something.
This is how OOP works. You don't even need to know how the object properties are named.

I guess
global $post;
echo '<pre>';
print_r($post->data);
echo '</pre>';
echo $post->data->order_id;

Related

Looping a Complex Multidimensional Array

I'm trying to extract all the variables from a complex array of nutrition results from Neutronix API.
The array is as follows:
Food Array:
Array ( [foods] =>
Array ( [0] => Array (
[food_name] => kale
[brand_name] =>
[serving_qty] => 1
[serving_unit] => cup, chopped
[serving_weight_grams] => 130
[nf_calories] => 36.4
[nf_total_fat] => 0.52
[nf_saturated_fat] => 0.07
[nf_cholesterol] => 0
[nf_sodium] => 29.9
[nf_total_carbohydrate] => 7.32
[nf_dietary_fiber] => 2.6
[nf_sugars] => 1.63
[nf_protein] => 2.47
[nf_potassium] => 296.4
[nf_p] => 36.4
[full_nutrients] => Array (
[0] => Array (
[attr_id] => 203
[value] => 2.47
)
[1] => Array (
[attr_id] => 204
[value] => 0.52
)
[2] => Array (
[attr_id] => 205
[value] => 7.319
)
[3] => Array (
[attr_id] => 207
[value] => 1.131
)
[4] => Array (
[attr_id] => 208
[value] => 36.4
)
[5] => Array (
[attr_id] => 221
[value] => 0
)
[6] => Array (
[attr_id] => 255
[value] => 118.56
)
[7] => Array (
[attr_id] => 262
[value] => 0
)
[8] => Array (
[attr_id] => 263
[value] => 0
)
[9] => Array (
[attr_id] => 268
[value] => 152.1
)
[10] => Array (
[attr_id] => 269
[value] => 1.625
)
[11] => Array (
[attr_id] => 291
[value] => 2.6
)
[12] => Array (
[attr_id] => 301
[value] => 93.6
)
[13] => Array (
[attr_id] => 303
[value] => 1.17
)
[14] => Array (
[attr_id] => 304
[value] => 23.4
)
[15] => Array (
[attr_id] => 305
[value] => 36.4
)
[16] => Array (
[attr_id] => 306
[value] => 296.4
)
[17] => Array (
[attr_id] => 307
[value] => 29.9
)
[18] => Array (
[attr_id] => 309
[value] => 0.312
)
[19] => Array (
[attr_id] => 312
[value] => 0.2028
)
[20] => Array (
[attr_id] => 315
[value] => 0.5408
)
[21] => Array (
[attr_id] => 317
[value] => 1.17
)
)
[nix_brand_name] =>
[nix_brand_id] =>
[nix_item_name] =>
[nix_item_id] =>
[upc] =>
[consumed_at] => 2017-09-08T22:44:54+00:00
[metadata] => Array ( )
[source] => 1
[ndb_no] => 11234
[tags] => Array (
[item] => kale
[measure] =>
[quantity] => 1.0
[tag_id] => 644
)
[alt_measures] => Array (
[0] => Array (
[serving_weight] => 130
[measure] => cup, chopped
[seq] => 1
[qty] => 1 )
[1] => Array (
[serving_weight] => 130
[measure] => cup
[seq] => 80
[qty] => 1 )
[2] => Array (
[serving_weight] => 2.71
[measure] => tsp
[seq] => 101
[qty] => 1 )
[3] => Array (
[serving_weight] => 8.13
[measure] => tbsp
[seq] => 102
[qty] => 1 )
)
[lat] =>
[lng] =>
[meal_type] => 5
[photo] => Array (
[thumb] => https://d2xdmhkmkbyw75.cloudfront.net/644_thumb.jpg
[highres] => https://d2xdmhkmkbyw75.cloudfront.net/644_highres.jpg
)
[sub_recipe] =>
)
)
)
My PHP Code so far is below. I've inserted comments where I am stuck as to how to get the variables.
I'm also not sure if I'm looping the segments of the array correctly.
foreach ($foods as $food){
foreach($food as $key => $val) {
//get values for each variable
foreach($full_nutrients as $nutrient){
foreach($nutrient as $key => $val){
//get values for each variable
}
}
foreach($metadata as $meta){
$source = $meta['source'];
$ndb_no = $meta['ndb_no'];
foreach($tags as $tag){
$tag_item = $tag['item'];
$tag_measure = $tag['measure'];
$tag_quantity = $tag['quantity'];
$tag_id = $tag['tag_id'];
}
}
foreach($alt_measures as $alt_meaasure){
foreach($alt_meaasure as $key => $val){
//get alt_measures
}
}
foreach($photo as $image){
$image_thumb = $image['thumb'];
$image_highres = $image['highres'];
}
}
}

How to get specific array values from moltin get a product

I just started using moltin and learn to developing it.
In the getting started there is code that to show product
<?php
$product = \Product::Find(['slug' => 'baju']);
?>
documentation link https://moltin.com/getting-started/php
and the result is when I print array
<?php
print_r($product);
?>
It shows like this
Array ( [status] => 1 [result] => Array ( [0] => Array ( [id] => 1207658536885027482 [order] => [created_at] => 2016-03-17 03:08:58 [updated_at] => 2016-03-17 03:08:58 [sku] => baju-1 [title] => baju [slug] => baju [sale_price] => 0 [status] => Array ( [value] => Live [data] => Array ( [key] => 1 [value] => Live ) ) [category] => Array ( [value] => Uncategorized [data] => Array ( [1134518259857490806] => Array ( [id] => 1134518259857490806 [order] => [created_at] => 2015-12-07 05:12:15 [updated_at] => 2015-12-07 05:12:15 [parent] => [slug] => uncategorized [status] => Array ( [value] => Live [data] => Array ( [key] => 1 [value] => Live ) ) [title] => Uncategorized [description] => Products that do not fit into another category ) ) ) [stock_level] => 10 [stock_status] => Array ( [value] => In Stock [data] => Array ( [key] => 1 [value] => In Stock ) ) [description] => baju [requires_shipping] => Array ( [value] => Yes [data] => Array ( [key] => 1 [value] => Yes ) ) [weight] => 0 [height] => 0 [width] => 0 [depth] => 0 [catalog_only] => Array ( [value] => No [data] => Array ( [key] => 0 [value] => No ) ) [tax_band] => Array ( [value] => Default [data] => Array ( [id] => 1134518260142703561 [title] => Default [description] => [rate] => 20 [created_at] => [updated_at] => ) ) [collection] => [brand] => [price] => Array ( [value] => £1.20 [data] => Array ( [formatted] => Array ( [with_tax] => £1.20 [without_tax] => £1.00 [tax] => £0.20 ) [rounded] => Array ( [with_tax] => 1.2 [without_tax] => 1 [tax] => 0.2 ) [raw] => Array ( [with_tax] => 1.2 [without_tax] => 1 [tax] => 0.2 ) ) ) [is_variation] => [modifiers] => Array ( ) [images] => Array ( ) ) ) [pagination] => Array ( [total] => 1 [current] => 1 [limit] => 10 [offset] => 0 [from] => 1 [to] => 1 [offsets] => Array ( [first] => [previous] => [next] => [last] => ) [links] => Array ( [first] => [previous] => [next] => [last] => ) ) ) status = 1
How to get specific array from this list?
I already try
<?php
echo $product[0]['id'];
?>
it didn't work, show error Notice: Undefined offset: 0
you have a multidimensional array under the result key
access it the following way:
$product['result'][0]['id']
Give this a try:
echo $product['result'][0]['id'];

Store a variable from the array of object php

I have a php variabl $purchase_data which when I did
print_r('purchase_data');
I got the output as
Array
(
[downloads] => Array
(
[0] => Array
(
[id] => 28
[options] => Array
(
)
[quantity] => 1
)
)
[fees] => Array
(
)
[subtotal] => 1
[discount] => 0
[tax] => 0
[price] => 1
[purchase_key] => a8d14e34ba425f9de6afe3ad4809587e
[user_email] => esh#test.com
[date] => 2014-05-11 20:07:22
[user_info] => Array
(
[id] => 1
[email] => eshtest.com
[first_name] => esh
[last_name] =>
[discount] => none
[address] =>
)
[post_data] => Array
(
[edd_email] => esh#test.com
[edd_first] => esh
[edd_last] =>
[edd_phone] => 919995871693
[edd-user-id] => 1
[edd_action] => purchase
[edd-gateway] => sample_gateway
)
[cart_details] => Array
(
[0] => Array
(
[name] => Shirt
[id] => 28
[item_number] => Array
(
[id] => 28
[options] => Array
(
)
[quantity] => 1
)
[item_price] => 1
[quantity] => 1
[discount] => 0
[subtotal] => 1
[tax] => 0
[price] => 1
)
)
[gateway] => sample_gateway
[card_info] => Array
(
[card_name] =>
[card_number] =>
[card_cvc] =>
[card_exp_month] =>
[card_exp_year] =>
[card_address] =>
[card_address_2] =>
[card_city] =>
[card_state] =>
[card_country] =>
[card_zip] =>
)
)
How can i store the value to edd_phone [edd_phone] into variable $mobileNo ?
Sorry for ths kind of a question.But badly need help
If you want to assign the value of [edd_phone] to $mobileNo , use this :
$mobileNo = $purchase_data['post_data']['edd_phone'];
As a side note : Your array is very complexly nested. If I were you, I would avoid such complex arrays.

How to get current prdocut category ID in Virtuemart [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Does anyone know what is conditional statement to detect a category id?
I need something like this:
If product is in category ID = 64 show some HTML
print_r($this->category);
TableCategories Object ( [virtuemart_category_id] => 16 [virtuemart_vendor_id] => 1 [category_name] => Chiloți, boxeri, indispensabili [slug] => chiloti-boxeri-indispensabili [category_description] => [category_template] => default [category_layout] => 0 [category_product_layout] => 0 [products_per_row] => 0 [ordering] => 3 [shared] => 0 [limit_list_step] => 0 [limit_list_initial] => 0 [metadesc] => [customtitle] => [metakey] => [metarobot] => [metaauthor] => [published] => 1 [_pkey:protected] => virtuemart_category_id [_pkeyForm:protected] => [_obkeys:protected] => Array ( [category_name] => Category in record is missing ! Can't save the record with no Category. [slug] => The given Sef Alias already exists. ) [_unique:protected] => 1 [_unique_name:protected] => Array ( [slug] => The given Sef Alias already exists. ) [_orderingKey:protected] => ordering [_slugAutoName:protected] => category_name [_slugName:protected] => slug [_loggable:protected] => 1 [_xParams:protected] => 0 [_varsToPushParam:protected] => Array ( ) [_translatable] => 1 [_translatableFields:protected] => Array ( [0] => category_name [1] => category_description [2] => metadesc [3] => metakey [4] => customtitle [slug] => slug ) [_langTag:protected] => en_gb [_tbl_lang:protected] => #__virtuemart_categories_en_gb [_updateNulls:protected] => [_tablePreFix] => c. [_tbl:protected] => #__virtuemart_categories [_tbl_key:protected] => virtuemart_category_id [_db:protected] => JFDatabase Object ( [mlTableList] => Array ( [0] => content [1] => modules [2] => menu ) [skipSetRefTables] => [orig_limit] => 0 [orig_offset] => 0 [skipjf] => 0 [translate] => 1 [tableFields:JFDatabase:private] => [profileData] => Array ( [JFDatabase::JFDatabase] => Array ( [total] => 0.0002288818359375 [count] => 1 [start] => Array ( ) ) [interceptDB::loadObjectList] => Array ( [total] => 0.0227241516113281 [count] => 62 [start] => Array ( [0] => 1397209811.22307 [1] => 1397209811.22406 [2] => 1397209811.22585 [3] => 1397209811.22628 [4] => 1397209811.23031 [5] => 1397209811.23254 [6] => 1397209811.24075 [7] => 1397209811.24117 [8] => 1397209811.24466 [9] => 1397209811.24608 [10] => 1397209811.27806 [11] => 1397209811.28038 [12] => 1397209811.28142 [13] => 1397209811.28238 [14] => 1397209811.28439 [15] => 1397209811.28616 [16] => 1397209811.28671 [17] => 1397209811.28773 [18] => 1397209811.28882 [19] => 1397209811.28975 [20] => 1397209811.29121 [21] => 1397209811.29171 [22] => 1397209811.29234 [23] => 1397209811.29328 [24] => 1397209811.29369 [25] => 1397209811.29412 [26] => 1397209811.2953 [27] => 1397209811.29915 [28] => 1397209811.29965 [29] => 1397209811.30009 [30] => 1397209811.30052 [31] => 1397209811.30295 [32] => 1397209811.304 [33] => 1397209811.30527 [34] => 1397209811.3057 [35] => 1397209811.30631 [36] => 1397209811.3067 [37] => 1397209811.30721 [38] => 1397209811.3123 [39] => 1397209811.32571 [40] => 1397209811.32638 [41] => 1397209811.32792 [42] => 1397209811.32891 [43] => 1397209811.32985 [44] => 1397209811.3308 [45] => 1397209811.33191 [46] => 1397209811.33233 [47] => 1397209811.333 [48] => 1397209811.33367 [49] => 1397209811.33744 [50] => 1397209811.33793 [51] => 1397209811.3384 [52] => 1397209811.33884 [53] => 1397209811.33928 [54] => 1397209811.34905 [55] => 1397209811.3519 [56] => 1397209811.35311 [57] => 1397209811.35398 [58] => 1397209811.35685 [59] => 1397209811.35732 ) ) [JFDatabase::getTableName] => Array ( [total] => 0.00406312942504883 [count] => 59 [start] => Array ( ) ) [JFDatabase::setLanguage] => Array ( [total] => 0.000144720077514648 [count] => 2 [start] => Array ( ) ) [JFDatabase::loadResult] => Array ( [total] => 0.000228643417358398 [count] => 13 [start] => Array ( ) ) [JFDatabase::loadAssoc] => Array ( [total] => 0.000162124633789062 [count] => 7 [start] => Array ( ) ) [JFDatabase::loadAssocList] => Array ( [total] => 0.000132083892822266 [count] => 4 [start] => Array ( ) ) [JFDatabase::loadResultArray] => Array ( [total] => 0.000250816345214844 [count] => 14 [start] => Array ( ) ) [JFDatabase::loadRow] => Array ( [total] => 0 [count] => 2 [start] => Array ( [0] => 1397209811.30335 [1] => 1397209811.30605 ) ) ) [name] => mysqli [nameQuote:protected] => ` [nullDate:protected] => 0000-00-00 00:00:00 [dbMinimum:protected] => 5.0.4 [_database:JDatabase:private] => 7624-magazin [connection:protected] => mysqli Object ( [affected_rows] => 0 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 1 [host_info] => ns13.host-md.net via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.5.32-MariaDB-log [server_version] => 50532 [stat] => Uptime: 346321 Threads: 6 Questions: 164660555 Slow queries: 35 Opens: 1823800 Flush tables: 293 Open tables: 1024 Queries per second avg: 475.456 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 2168966 [warning_count] => 0 ) [count:protected] => 0 [cursor:protected] => mysqli_result Object ( [current_field] => [field_count] => [lengths] => [num_rows] => [type] => ) [debug:protected] => [limit:protected] => 0 [log:protected] => Array ( ) [offset:protected] => 0 [sql:protected] => SELECT `virtuemart_vendor_id` FROM `#__virtuemart_vmusers` `au` WHERE `au`.`virtuemart_user_id`="687" AND `au`.`user_is_vendor` = "1" [tablePrefix:protected] => l50ti_ [utf:protected] => 1 [errorNum:protected] => 0 [errorMsg:protected] => [hasQuoted:protected] => [quoted:protected] => Array ( ) ) [_trackAssets:protected] => [_rules:protected] => [_locked:protected] => [_errors:protected] => Array ( ) [created_on] => 2014-03-16 21:10:24 [created_by] => 685 [modified_on] => 2014-04-08 13:29:22 [modified_by] => 685 [virtuemart_media_id] => Array ( ) [haschildren] => [children] => Array ( ) [productcount] => 6 [parents] => [images] => Array ( [0] => VmMediaHandler Object ( [media_attributes] => 0 [setRole] => [file_name] => [file_extension] => [virtuemart_media_id] => 0 [_foldersToTest:VmMediaHandler:private] => Array ( [0] => /var/www/vhosts/7624/domains/maicom.md/public_html/images/stories/virtuemart/category/ [1] => /var/www/vhosts/7624/domains/maicom.md/public_html/images/stories/virtuemart/category/resized/ ) [_actions:VmMediaHandler:private] => Array ( ) [_mLocation:VmMediaHandler:private] => Array ( ) [_hidden:VmMediaHandler:private] => Array ( ) [theme_url] => http://maicom.md/components/com_virtuemart/ [virtuemart_vendor_id] => 0 [file_title] => [file_description] => [file_meta] => [file_mimetype] => [file_type] => categories [file_url] => images/stories/virtuemart/category/ [file_url_thumb] => [published] => 0 [file_is_downloadable] => 0 [file_is_forSale] => 0 [file_is_product_image] => 0 [shared] => 0 [file_params] => 0 [file_lang] => [_translatable] => [_tablePreFix] => [created_on] => [created_by] => 0 [modified_on] => [modified_by] => 0 [file_url_folder] => images/stories/virtuemart/category/ [file_path_folder] => images/stories/virtuemart/category/ [file_url_folder_thumb] => images/stories/virtuemart/category/resized/ [media_role] => file_is_displayable ) ) [file_url_thumb] => [file_url] => images/stories/virtuemart/category/ ) </div>
What are you trying so far ?
did you need it in details page ?
if yes then try following method. I assume you are in any of the product details page layout.
if($this->product->virtuemart_category_id == 64){
echo 'your Custom HTML';
}
else{
//nothing
}
Also you need to get more details about current product just use.
echo "<pre/>";
print_r($this->product);
Hope its works..

Removing an array result

[5] => Array
(
[id] => 29372
[product_id] => Array
(
[0] => stdClass Object
(
[id] => 1469
[type_id] => 1
[title] => Hearth 2 Hearth
[cover] => 21cf9d7d09d403251ba5d01ff33cd089.jpg
[coverid] => 1178
[inserted_by] => 0
[inserted_date] => 2011-02-11 13:55:23
[status_id] => 0
)
)
[variable_id] => 9
[variable_value] => 2011-02-11
[master_value] =>
[released_date] => 2011-02-11
[price] => Array
(
[0] => Array
(
[product_id] => 1469
[media_format] => VCD
[price] => 29000
[discount] => 5
)
)
[media_format] => VCD
)
[6] => Array
(
[id] => 30074
[product_id] => Array
(
[0] => stdClass Object
(
[id] => 1470
[type_id] => 1
[title] => Hearth 2 Hearth
[cover] => 149ddd4d1d5e567c1300d4831323e1c5.jpg
[coverid] => 1177
[inserted_by] => 6
[inserted_date] => 2011-02-16 15:18:58
[status_id] => 0
)
)
[variable_id] => 9
[variable_value] => 2011-02-11
[master_value] =>
[released_date] => 2011-02-11
[price] => Array
(
[0] => Array
(
[product_id] => 1470
[media_format] => DVD
[price] => 39000
[discount] => 0
)
)
[media_format] => DVD
)
I want the result by media_format = DVD so the whole array is gone, is there anyway to delete an array or remove it?
The same way you "delete" any variable:
unset($array[$index]);

Categories