我正在尝试获取 WordPress 的 wp_postmeta 数据库中的“键”的值。
我已在数据库中设置了此设置。
帖子 ID: xxxx
元 key : _sco_post_data
元值: a:5:{s:15:"billing_country";s:2:"SE";s:16:"billing_postcode";s:5:"99999";s :14:"order_comments";s:14:"fdsfdafasdfasf";s:18:"dtwc_delivery_date";s:10:"2020-10-06";s:18:"dtwc_delivery_time";s:5:"19: 30";}
现在,在我的functions.php 中,我只想获取“dtwc_delivery_date”和“dtwc_delivery_time”的特定值。我尝试过,但我唯一能得到的就是将它们包含在 foreach 循环中。
function my_function( $order_id ) {
$order = wc_get_order( $order_id );
$meta = $order->get_meta('_sco_post_data'); //this returns an array
foreach ( $meta as $item ) {
echo '<p><strong>'.__('Test Value').':</strong> ' . $item. '</p>';
}
上面代码的结果如下: 测试值:SE
测试值:99999
测试值:fdsfdafasdfasf
测试值:2020-10-06
测试值:19:30
我希望能够调用这样的值:
$meta['dtwc_delivery_date'] 但这不会返回任何内容。里面是空的。
最佳答案
通常 $meta['dtwc_delivery_date']
应该可以工作…现在您可以在数组上使用 print_r()
函数来查看如何构建以及带有索引的 foreach 循环(按键)能够检查按键并查看它是否会按您的预期工作:
function my_function( $order_id ) {
$order = wc_get_order( $order_id ); // Order object
$values = $order->get_meta('_sco_post_data'); // get meta value (array)
// Raw formatted output from the array to check how comes the data
echo '<pre>'. print_r( $obj, true ) . '</pre>';
// The loop
foreach ( $values as $key => $value ) {
// displaying array raw formatted output (and checking if you can get a value from its key
echo '<p>Key: '. $key . ' | Value: '. $value . ' | Value from the key: '. $values[$key] . '<p>';
}
}
这应该对你有帮助。
关于php - 从 WooCommerce 中的后元数组变量值获取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64246104/