$订购访问受保护阵列

时间:2019-02-19 00:45:55

标签: php wordpress hook-woocommerce

我正在尝试通过woocommerce订单系统以$ order访问元数据。系统具有额外的订单字段。我正在尝试访问这些字段。我尝试的每一种变化都会导致服务器错误,或者输出的数据为空白。我已经尝试过$ MetaData-> id,$ MetaData [0] ['id'],对每个记录使用foreach循环...等等。它没有区别,字符串形式的数据不会显示。我怀疑它与受保护的数组有关,显然我不知道如何进行管理。它不起作用。

这是代码和测试的一些结果。

$order        = new WC_Order( $order_id );
$order_data   = $order->get_data();  
$MetaData = $order_data['meta_data'];
DebugLog(json_encode($MetaData));

// OUTPUT of JSON_ENCODE:
// [{"id":2658,"key":"business-name","value":"BUSINESS NAME HERE"},{"id":2659,"key":"full-business-address","value":"1028 STREET Dr"},{"id":2660,"key":"city","value":"CITY NAME"},{"id":2661,"key":"state","value":"California"},{"id":2662,"key":"zip","value":"900XX"},{"id":2663,"key":"_subscription_switch_data","value":[]},{"id":2723,"key":"_stripe_customer_id","value":"XXXX"},{"id":2724,"key":"_stripe_source_id","value":"XXXX"},{"id":2727,"key":"_stripe_charge_captured","value":"yes"},{"id":2728,"key":"_stripe_fee","value":"0.45"},{"id":2729,"key":"_stripe_net","value":"4.55"},{"id":2730,"key":"_stripe_currency","value":"USD"},{"id":2736,"key":"_wc_memberships_access_granted","value":{"215":{"already_granted":"yes","granting_order_status":"processing"}}},{"id":2748,"key":"_wc_memberships_access_granted","value":{"215":{"already_granted":"yes","granting_order_status":"processing"}}}]

 foreach($MetaData as $index => $feature)
  {
   $MetaValue = $MetaData[$index ]['key']; 
   DebugLog($MetaValue);
  }

最后的foreach会导致服务器错误。我只是看不到它,而是尝试了各种组合。

我也尝试过:

foreach($MetaData as $feature)  
  {
   $MetaValue = $feature['key']; 
   DebugLog($MetaValue);
  }

$MetaValue = $MetaData[0]['key']

相同的结果。任何想法?在这种最基本的编程工具中我缺少什么?

1 个答案:

答案 0 :(得分:0)

更新!知道了 狂野的旅程。您必须创建指向行(MetaData的指针),然后将每行视为具有索引的自身数组。

解决方案

  $order        = new WC_Order( $order_id );
  $order_data   = $order->get_data();  
  $MetaData = $order_data['meta_data'];

  foreach($MetaData as $SubRow)
  {
  DebugLog($SubRow->id);
  DebugLog($SubRow->key);   
  DebugLog($SubRow->value);     
  }

感谢所有写信的人,让我有所思考。