John Davidson

php - Getting values from meta_data

0 comments
Message:


I have a problem with getting some values from Woocommerce $item object.


I'm trying to extract data from "meta_data" -> "_advanced_woo_discount_item_total_discount" -> initial_price, discounted_price and "discount_value" from "total _discount_details".


I tried with $item->get_meta_data(); but got a blank return.


{
"id":55,
"order_id":11501,
"name":"test",
"product_id":6419,
"variation_id":6421,
"quantity":1,
"tax_class":"",
"subtotal":"182.491803",
"subtotal_tax":"40.15",
"total":"182.491803",
"total_tax":"40.15",
"taxes":{
"total":{"1":"40.148197"},
"subtotal":{"1":"40.148197"}
},
"meta_data":[{
"id":457,
"key":"pa_velikost",
"value":"180cm"
},
{
"id":458,
"key":"_advanced_woo_discount_item_total_discount",
"value":
{
"initial_price":278.3,
"discounted_price":222.64000000000001,
"total_discount_details":
{
"8e39099d383d7d50a8c4fce98e59cc79":
{
"1":
{
"set_discount":0,
"bulk_discount":0,
"simple_discount":
{
"discount_type":"percentage",
"discount_value":"20",
"discount_quantity":1,
"discount_price_per_quantity":55.660000000000004,
"discount_price":55.660000000000004
}
}
}
},
"cart_discount_details":[],
"apply_as_cart_rule":["no"],
"discount_lines":
{
"non_applied":{
"quantity":0,
"discount":0,
"price":278.3,
"calculate_discount_from":278.3
},
"0":
{
"quantity":1,
"discount":55.660000000000004,
"original_price":278.3,
"discounted_price":222.64000000000001
}
},
"cart_quantity":1,
"product_id":6421,
"initial_price_based_on_tax_settings":278.3,
"discounted_price_based_on_tax_settings":222.64000000000001
}
}
]
}


My Answer:
the meta_data array within the $item object in Woocommerce. Here's an example of how you can do this:

php
foreach ($item->get_meta_data() as $meta) {
$meta_key = $meta->key;
$meta_value = $meta->value;

// Do something with the meta key and value
echo "Meta Key: " . $meta_key . ", Meta Value: " . $meta_value . "
";
}


In this code snippet, we are looping through each meta data object within the meta_data array of the $item object. We then extract the key and value of each meta data object and do something with it, such as printing it out.

You can modify the code to suit your specific needs, such as checking for a specific meta key or value, or performing a specific action based on the meta data.

Rate this post

5 of 5 based on 4390 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us